package assets

import (
	"net/http"

	"tm/pkg/logger"

	"github.com/labstack/echo/v4"
)

// Handler handles HTTP requests for flag operations
type Handler struct {
	service Service
	logger  logger.Logger
}

// NewHandler creates a new flag handler
func NewHandler(service Service, logger logger.Logger) *Handler {
	return &Handler{
		service: service,
		logger:  logger,
	}
}

// **************************** PUBLIC ENDPOINTS ****************************
// GetFlagSVG returns only the SVG content of a flag
// @Summary Get flag SVG
// @Description Retrieve only the SVG content of a country flag by its 3-letter country code
// @Tags Flags
// @Accept json
// @Produce image/svg+xml
// @Param country_code path string true "3-letter country code (e.g., POL, GRC)"
// @Success 200 {string} string "SVG content"
// @Router /api/v1//{country_code} [get]
func (h *Handler) GetFlagSVG(c echo.Context) error {
	countryCode := c.Param("country_code")

	svgData := h.service.GetFlagSVG(c.Request().Context(), countryCode)

	c.Response().Header().Set("Content-Type", "image/svg+xml")
	c.Response().Header().Set("Cache-Control", "public, max-age=86400") // Cache for 24 hours
	return c.String(http.StatusOK, svgData)
}

// **************************** ADMIN ENDPOINTS ****************************

// AdminGetFlag returns a flag by country code (Admin)
// @Summary Get flag by country code (Admin)
// @Description Retrieve a country flag by its 3-letter country code for admin panel
// @Tags Admin-Flags
// @Accept json
// @Produce json
// @Param country_code path string true "3-letter country code (e.g., POL, GRC)"
// @Success 200 {string} string "SVG content"
// @Security BearerAuth
// @Router /admin/v1/flags/{country_code} [get]
func (h *Handler) AdminGetFlagSVG(c echo.Context) error {
	countryCode := c.Param("country_code")

	svgData := h.service.GetFlagSVG(c.Request().Context(), countryCode)

	c.Response().Header().Set("Content-Type", "image/svg+xml")
	c.Response().Header().Set("Cache-Control", "public, max-age=86400") // Cache for 24 hours
	return c.String(http.StatusOK, svgData)
}
