package handlers

import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type ServiceTitleHandler struct {
	serviceTitleService *services.ServiceTitleService
}

func NewServiceTitleHandler(serviceTitleService *services.ServiceTitleService) *ServiceTitleHandler {
	return &ServiceTitleHandler{serviceTitleService: serviceTitleService}
}

// GetAllServiceTitles godoc
// @Summary Get all active service titles
// @Description Retrieve a list of active service titles
// @Tags services
// @Produce json
// @Success 200 {array} models.ServiceTitle
// @Failure 500 {object} map[string]string
// @Router /service-titles [get]
func (h *ServiceTitleHandler) GetAllServiceTitles(c *gin.Context) {
	items, err := h.serviceTitleService.GetAllServiceTitles(true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// GetActiveServiceTitle godoc
// @Summary Get active service title
// @Description Retrieve the newest active service title
// @Tags services
// @Produce json
// @Success 200 {object} models.ServiceTitle
// @Failure 404 {object} map[string]string
// @Router /service-titles/active [get]
func (h *ServiceTitleHandler) GetActiveServiceTitle(c *gin.Context) {
	item, err := h.serviceTitleService.GetFirstActiveServiceTitle()
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// AdminGetAllServiceTitles godoc
// @Summary Get all service titles (Admin)
// @Description Retrieve a list of all service titles including inactive ones
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {array} models.ServiceTitle
// @Failure 500 {object} map[string]string
// @Router /admin/service-titles [get]
func (h *ServiceTitleHandler) AdminGetAllServiceTitles(c *gin.Context) {
	items, err := h.serviceTitleService.GetAllServiceTitles(false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// AdminGetServiceTitleByID godoc
// @Summary Get a service title by ID (Admin)
// @Description Retrieve details of a specific service title
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Service Title ID"
// @Success 200 {object} models.ServiceTitle
// @Failure 404 {object} map[string]string
// @Router /admin/service-titles/{id} [get]
func (h *ServiceTitleHandler) AdminGetServiceTitleByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.serviceTitleService.GetServiceTitleByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// CreateServiceTitle godoc
// @Summary Create a new service title (Admin)
// @Description Create a new service title entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param request body map[string]string true "Service Title Request"
// @Success 201 {object} models.ServiceTitle
// @Failure 400 {object} map[string]string
// @Router /admin/service-titles [post]
func (h *ServiceTitleHandler) CreateServiceTitle(c *gin.Context) {
	var req struct {
		Title    string `json:"title"`
		TitleSub string `json:"title_sub"`
		IsActive *bool  `json:"is_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	req.Title = strings.TrimSpace(req.Title)
	req.TitleSub = strings.TrimSpace(req.TitleSub)
	if req.Title == "" || req.TitleSub == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "title and title_sub are required"})
		return
	}

	isActive := false
	if req.IsActive != nil {
		isActive = *req.IsActive
	}

	item, err := h.serviceTitleService.CreateServiceTitle(req.Title, req.TitleSub, isActive)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, item)
}

// UpdateServiceTitle godoc
// @Summary Update a service title (Admin)
// @Description Update an existing service title entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param id path string true "Service Title ID"
// @Param request body map[string]string true "Service Title Request"
// @Success 200 {object} models.ServiceTitle
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/service-titles/{id} [put]
func (h *ServiceTitleHandler) UpdateServiceTitle(c *gin.Context) {
	id := c.Param("id")
	var req struct {
		Title    *string `json:"title"`
		TitleSub *string `json:"title_sub"`
		IsActive *bool   `json:"is_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if req.Title != nil {
		trimmed := strings.TrimSpace(*req.Title)
		req.Title = &trimmed
	}
	if req.TitleSub != nil {
		trimmed := strings.TrimSpace(*req.TitleSub)
		req.TitleSub = &trimmed
	}

	item, err := h.serviceTitleService.UpdateServiceTitle(id, req.Title, req.TitleSub, req.IsActive)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "service title not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, item)
}

// DeleteServiceTitle godoc
// @Summary Delete a service title (Admin)
// @Description Delete a service title by ID
// @Tags admin
// @Security ApiKeyAuth
// @Param id path string true "Service Title ID"
// @Success 200 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/service-titles/{id} [delete]
func (h *ServiceTitleHandler) DeleteServiceTitle(c *gin.Context) {
	id := c.Param("id")
	if err := h.serviceTitleService.DeleteServiceTitle(id); err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Service title deleted successfully"})
}
