package handlers

import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type SkillHandler struct {
	skillService *services.SkillService
}

func NewSkillHandler(skillService *services.SkillService) *SkillHandler {
	return &SkillHandler{skillService: skillService}
}

type CreateSkillRequest struct {
	Title    string `json:"title"`
	Degree   int    `json:"degree"`
	ResumeID string `json:"resume_id"`
	IsActive *bool  `json:"is_active"`
}

type UpdateSkillRequest struct {
	Title    *string `json:"title"`
	Degree   *int    `json:"degree"`
	ResumeID *string `json:"resume_id"`
	IsActive *bool   `json:"is_active"`
}

// GetAllSkills godoc
// @Summary Get all active skills
// @Description Retrieve a list of active skill entries
// @Tags resume
// @Produce json
// @Param resume_id query string false "Resume ID"
// @Success 200 {array} models.Skill
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /skills [get]
func (h *SkillHandler) GetAllSkills(c *gin.Context) {
	resumeID := strings.TrimSpace(c.Query("resume_id"))
	var resumeIDPtr *string
	if resumeID != "" {
		resumeIDPtr = &resumeID
	}
	items, err := h.skillService.GetAllSkills(resumeIDPtr, true)
	if err != nil {
		status := http.StatusInternalServerError
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// AdminGetAllSkills godoc
// @Summary Get all skills (Admin)
// @Description Retrieve a list of all skill entries including inactive ones
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param resume_id query string false "Resume ID"
// @Success 200 {array} models.Skill
// @Failure 400 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /admin/skills [get]
func (h *SkillHandler) AdminGetAllSkills(c *gin.Context) {
	resumeID := strings.TrimSpace(c.Query("resume_id"))
	var resumeIDPtr *string
	if resumeID != "" {
		resumeIDPtr = &resumeID
	}
	items, err := h.skillService.GetAllSkills(resumeIDPtr, false)
	if err != nil {
		status := http.StatusInternalServerError
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// AdminGetSkillByID godoc
// @Summary Get a skill by ID (Admin)
// @Description Retrieve details of a specific skill entry
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Skill ID"
// @Success 200 {object} models.Skill
// @Failure 404 {object} map[string]string
// @Router /admin/skills/{id} [get]
func (h *SkillHandler) AdminGetSkillByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.skillService.GetSkillByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

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

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

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

	resumeUUID, parseErr := parseUUIDPtr(req.ResumeID)
	if parseErr != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid resume id"})
		return
	}
	item, err := h.skillService.CreateSkill(req.Title, req.Degree, resumeUUID, isActive)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, item)
}

// UpdateSkill godoc
// @Summary Update a skill (Admin)
// @Description Update an existing skill entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param id path string true "Skill ID"
// @Param request body UpdateSkillRequest true "Skill Request"
// @Success 200 {object} models.Skill
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/skills/{id} [put]
func (h *SkillHandler) UpdateSkill(c *gin.Context) {
	id := c.Param("id")
	var req UpdateSkillRequest
	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.ResumeID != nil {
		trimmed := strings.TrimSpace(*req.ResumeID)
		req.ResumeID = &trimmed
	}

	var resumeUUIDPtr *uuid.UUID
	if req.ResumeID != nil {
		parsed, parseErr := parseUUIDPtr(*req.ResumeID)
		if parseErr != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "invalid resume id"})
			return
		}
		resumeUUIDPtr = parsed
	}
	item, err := h.skillService.UpdateSkill(id, req.Title, req.Degree, resumeUUIDPtr, req.IsActive)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "skill not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, item)
}

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