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}
}

func (h *SkillHandler) GetAllSkills(c *gin.Context) {
	resumeIDStr := c.Query("resume_id")
	var resumeID *string
	if resumeIDStr != "" {
		resumeID = &resumeIDStr
	}

	items, err := h.skillService.GetAllSkills(resumeID, true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

func (h *SkillHandler) AdminGetAllSkills(c *gin.Context) {
	resumeIDStr := c.Query("resume_id")
	var resumeID *string
	if resumeIDStr != "" {
		resumeID = &resumeIDStr
	}

	items, err := h.skillService.GetAllSkills(resumeID, false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

func (h *SkillHandler) GetSkillByID(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)
}

func (h *SkillHandler) CreateSkill(c *gin.Context) {
	var req struct {
		Title    string `json:"title"`
		Degree   int    `json:"degree"`
		ResumeID string `json:"resume_id"`
		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)
	if req.Title == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "title is required"})
		return
	}

	resumeUUID, err := uuid.Parse(req.ResumeID)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid resume_id"})
		return
	}

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

	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)
}

func (h *SkillHandler) UpdateSkill(c *gin.Context) {
	id := c.Param("id")
	var req struct {
		Title    *string `json:"title"`
		Degree   *int    `json:"degree"`
		ResumeID *string `json:"resume_id"`
		IsActive *bool   `json:"is_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var resumeUUID *uuid.UUID
	if req.ResumeID != nil {
		parsed, err := uuid.Parse(*req.ResumeID)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "invalid resume_id"})
			return
		}
		resumeUUID = &parsed
	}

	item, err := h.skillService.UpdateSkill(
		id,
		req.Title,
		req.Degree,
		resumeUUID,
		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)
}

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"})
}
