package handlers


import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type MainMenuHandler struct {
	mainMenuService *services.MainMenuService
}

func NewMainMenuHandler(mainMenuService *services.MainMenuService) *MainMenuHandler {
	return &MainMenuHandler{mainMenuService: mainMenuService}
}

type CreateMainMenuRequest struct {
	Home      string `json:"home"`
	About     string `json:"about"`
	Services  string `json:"services"`
	Resume    string `json:"resume"`
	Portfolio string `json:"portfolio"`
	Contact   string `json:"contact"`
	IsActive  *bool  `json:"is_active"`
}

type UpdateMainMenuRequest struct {
	Home      *string `json:"home"`
	About     *string `json:"about"`
	Services  *string `json:"services"`
	Resume    *string `json:"resume"`
	Portfolio *string `json:"portfolio"`
	Contact   *string `json:"contact"`
	IsActive  *bool   `json:"is_active"`
}

// GetAllMainMenus godoc
// @Summary Get all active main menus
// @Description Retrieve a list of active main menu entries
// @Tags menu
// @Produce json
// @Success 200 {array} models.MainMenu
// @Failure 500 {object} map[string]string
// @Router /main-menu [get]
func (h *MainMenuHandler) GetAllMainMenus(c *gin.Context) {
	items, err := h.mainMenuService.GetAllMainMenus(true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// GetActiveMainMenu godoc
// @Summary Get active main menu
// @Description Retrieve the newest active main menu entry
// @Tags menu
// @Produce json
// @Success 200 {object} models.MainMenu
// @Failure 404 {object} map[string]string
// @Router /main-menu/active [get]
func (h *MainMenuHandler) GetActiveMainMenu(c *gin.Context) {
	item, err := h.mainMenuService.GetFirstActiveMainMenu()
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

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

// AdminGetMainMenuByID godoc
// @Summary Get a main menu by ID (Admin)
// @Description Retrieve details of a specific main menu entry
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Main Menu ID"
// @Success 200 {object} models.MainMenu
// @Failure 404 {object} map[string]string
// @Router /admin/main-menu/{id} [get]
func (h *MainMenuHandler) AdminGetMainMenuByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.mainMenuService.GetMainMenuByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// CreateMainMenu godoc
// @Summary Create a new main menu (Admin)
// @Description Create a new main menu entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param request body CreateMainMenuRequest true "Main Menu Request"
// @Success 201 {object} models.MainMenu
// @Failure 400 {object} map[string]string
// @Router /admin/main-menu [post]
func (h *MainMenuHandler) CreateMainMenu(c *gin.Context) {
	var req CreateMainMenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	req.Home = strings.TrimSpace(req.Home)
	req.About = strings.TrimSpace(req.About)
	req.Services = strings.TrimSpace(req.Services)
	req.Resume = strings.TrimSpace(req.Resume)
	req.Portfolio = strings.TrimSpace(req.Portfolio)
	req.Contact = strings.TrimSpace(req.Contact)
	if req.Home == "" || req.About == "" || req.Services == "" || req.Resume == "" || req.Portfolio == "" || req.Contact == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "home, about, services, resume, portfolio, and contact are required"})
		return
	}

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

	item, err := h.mainMenuService.CreateMainMenu(req.Home, req.About, req.Services, req.Resume, req.Portfolio, req.Contact, isActive)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, item)
}

// UpdateMainMenu godoc
// @Summary Update a main menu (Admin)
// @Description Update an existing main menu entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param id path string true "Main Menu ID"
// @Param request body UpdateMainMenuRequest true "Main Menu Request"
// @Success 200 {object} models.MainMenu
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/main-menu/{id} [put]
func (h *MainMenuHandler) UpdateMainMenu(c *gin.Context) {
	id := c.Param("id")
	var req UpdateMainMenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if req.Home != nil {
		trimmed := strings.TrimSpace(*req.Home)
		req.Home = &trimmed
	}
	if req.About != nil {
		trimmed := strings.TrimSpace(*req.About)
		req.About = &trimmed
	}
	if req.Services != nil {
		trimmed := strings.TrimSpace(*req.Services)
		req.Services = &trimmed
	}
	if req.Resume != nil {
		trimmed := strings.TrimSpace(*req.Resume)
		req.Resume = &trimmed
	}
	if req.Portfolio != nil {
		trimmed := strings.TrimSpace(*req.Portfolio)
		req.Portfolio = &trimmed
	}
	if req.Contact != nil {
		trimmed := strings.TrimSpace(*req.Contact)
		req.Contact = &trimmed
	}

	item, err := h.mainMenuService.UpdateMainMenu(id, req.Home, req.About, req.Services, req.Resume, req.Portfolio, req.Contact, req.IsActive)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "main menu not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, item)
}

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