package cms

import (
	"tm/pkg/response"

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

// Handler handles HTTP requests for CMS operations
type Handler struct {
	service Service
}

// NewHandler creates a new CMS handler
func NewHandler(service Service) *Handler {
	return &Handler{
		service: service,
	}
}

// GetByKey handles retrieving CMS content by key (public API)
// @Summary Get CMS content by key
// @Description Retrieve CMS content for public display by its key
// @Tags CMS
// @Accept json
// @Produce json
// @Param key path string true "CMS key"
// @Success 200 {object} response.APIResponse{data=CMSResponse}
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /api/v1/cms/{key} [get]
func (h *Handler) GetByKey(c echo.Context) error {
	key := c.Param("key")

	cms, err := h.service.GetByKey(c.Request().Context(), key)
	if err != nil {
		return response.NotFound(c, "CMS content not found")
	}

	return response.Success(c, cms, "CMS content retrieved successfully")
}

// Create handles creating a new CMS entry (admin only)
// @Summary Create a new CMS entry
// @Description Create a new CMS entry for content management (admin only)
// @Tags Admin-CMS
// @Accept json
// @Produce json
// @Param cms body CMSForm true "CMS information"
// @Success 201 {object} response.APIResponse{data=CMSResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 409 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/cms [post]
func (h *Handler) Create(c echo.Context) error {
	form, err := ParseForm(c)
	if err != nil {
		return handleParseError(c, err)
	}

	cms, err := h.service.Create(c.Request().Context(), form)
	if err != nil {
		return handleServiceError(c, err, "Failed to create marketing page")
	}

	return response.Created(c, cms, "Marketing page created successfully")
}

// GetByID handles retrieving a CMS entry by ID (admin only)
// @Summary Get CMS by ID
// @Description Retrieve a CMS entry by its ID for editing (admin only)
// @Tags Admin-CMS
// @Accept json
// @Produce json
// @Param id path string true "CMS ID"
// @Success 200 {object} response.APIResponse{data=CMSResponse}
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/cms/{id} [get]
func (h *Handler) GetByID(c echo.Context) error {
	id := c.Param("id")

	cms, err := h.service.GetByID(c.Request().Context(), id)
	if err != nil {
		return handleServiceError(c, err, "Failed to get marketing page")
	}

	return response.Success(c, cms, "Marketing page retrieved successfully")
}

// Update handles updating a CMS entry (admin only)
// @Summary Update CMS entry
// @Description Update an existing CMS entry by its ID (admin only)
// @Tags Admin-CMS
// @Accept json
// @Produce json
// @Param id path string true "CMS ID"
// @Param cms body CMSForm true "Updated CMS information"
// @Success 200 {object} response.APIResponse{data=CMSResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 409 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/cms/{id} [put]
func (h *Handler) Update(c echo.Context) error {
	id := c.Param("id")

	form, err := ParseForm(c)
	if err != nil {
		return handleParseError(c, err)
	}

	cms, err := h.service.Update(c.Request().Context(), id, form)
	if err != nil {
		return handleServiceError(c, err, "Failed to update marketing page")
	}

	return response.Success(c, cms, "Marketing page updated successfully")
}

// Delete handles deleting a CMS entry (admin only)
// @Summary Delete CMS entry
// @Description Delete a CMS entry by its ID (admin only)
// @Tags Admin-CMS
// @Accept json
// @Produce json
// @Param id path string true "CMS ID"
// @Success 200 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/cms/{id} [delete]
func (h *Handler) Delete(c echo.Context) error {
	id := c.Param("id")

	err := h.service.Delete(c.Request().Context(), id)
	if err != nil {
		return handleServiceError(c, err, "Failed to delete marketing page")
	}

	return response.Success(c, nil, "Marketing page deleted successfully")
}

// Search handles searching CMS entries (admin only)
// @Summary Search CMS entries
// @Description Search CMS entries with filters and pagination (admin only)
// @Tags Admin-CMS
// @Accept json
// @Produce json
// @Param q query string false "Search query"
// @Param key query string false "CMS key filter"
// @Param limit query integer false "Number of CMS entries per page (1-100)" minimum(1) maximum(100) default(20)
// @Param offset query integer false "Number of CMS entries to skip for pagination" minimum(0) default(0)
// @Param sort_by query string false "Field to sort by" Enums(key, created_at, updated_at) default(created_at)
// @Param sort_order query string false "Sort order" Enums(asc, desc) default(desc)
// @Success 200 {object} response.APIResponse{data=CMSListResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/cms [get]
func (h *Handler) Search(c echo.Context) error {
	form, err := response.Parse[SearchCMSForm](c)
	if err != nil {
		return response.ValidationError(c, "Invalid request data", err.Error())
	}

	pagination, err := response.NewPagination(c)
	if err != nil {
		return response.PaginationBadRequest(c, err)
	}

	result, err := h.service.Search(c.Request().Context(), form, pagination)
	if err != nil {
		if response.IsListPaginationError(err) {
			return response.PaginationBadRequest(c, err)
		}
		return response.InternalServerError(c, "Failed to search CMS entries")
	}

	return response.SuccessWithMeta(c, result, result.Meta, "CMS entries retrieved successfully")
}
