// Filename: internal/handlers/log_handler.go
package handlers

import (
	"gemini-balancer/internal/errors"
	"gemini-balancer/internal/response"
	"gemini-balancer/internal/service"

	"github.com/gin-gonic/gin"
)

type LogHandler struct {
	logService *service.LogService
}

func NewLogHandler(logService *service.LogService) *LogHandler {
	return &LogHandler{logService: logService}
}

func (h *LogHandler) GetLogs(c *gin.Context) {
	queryParams := make(map[string]string)
	for key, values := range c.Request.URL.Query() {
		if len(values) > 0 {
			queryParams[key] = values[0]
		}
	}
	params, err := service.ParseLogQueryParams(queryParams)
	if err != nil {
		response.Error(c, errors.ErrBadRequest)
		return
	}
	logs, total, err := h.logService.GetLogs(c.Request.Context(), params)
	if err != nil {
		response.Error(c, errors.ErrDatabase)
		return
	}
	response.Success(c, gin.H{
		"items":     logs,
		"total":     total,
		"page":      params.Page,
		"page_size": params.PageSize,
	})
}
