package store

type TokenStats struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
}

func (s *Store) InsertTokenUsage(roomID, agent string, prompt, completion, total int) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	_, err := s.db.Exec(
		`INSERT INTO token_usage (room_id, agent, prompt_tokens, completion_tokens, total_tokens) VALUES (?, ?, ?, ?, ?)`,
		roomID, agent, prompt, completion, total,
	)
	return err
}

func (s *Store) GetRoomTokenStats(roomID string) (*TokenStats, error) {
	var ts TokenStats
	err := s.db.QueryRow(
		`SELECT COALESCE(SUM(prompt_tokens),0), COALESCE(SUM(completion_tokens),0), COALESCE(SUM(total_tokens),0)
		 FROM token_usage WHERE room_id = ?`, roomID,
	).Scan(&ts.PromptTokens, &ts.CompletionTokens, &ts.TotalTokens)
	if err != nil {
		return nil, err
	}
	return &ts, nil
}
