package handlers

import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type ResumeHandler struct {
	resumeService *services.ResumeService
}

func NewResumeHandler(resumeService *services.ResumeService) *ResumeHandler {
	return &ResumeHandler{resumeService: resumeService}
}

// GetAllResumes retrieves all active resumes.
func (h *ResumeHandler) GetAllResumes(c *gin.Context) {
	items, err := h.resumeService.GetAllResumes(true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// GetActiveResume retrieves the newest active resume.
func (h *ResumeHandler) GetActiveResume(c *gin.Context) {
	item, err := h.resumeService.GetFirstActiveResume()
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// AdminGetAllResumes retrieves all resumes (for admin).
func (h *ResumeHandler) AdminGetAllResumes(c *gin.Context) {
	items, err := h.resumeService.GetAllResumes(false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

// AdminGetResumeByID retrieves a resume by ID (for admin).
func (h *ResumeHandler) AdminGetResumeByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.resumeService.GetResumeByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

// CreateResume creates a new resume.
func (h *ResumeHandler) CreateResume(c *gin.Context) {
	var req struct {
		Title        string `json:"title"`
		TitleSub     string `json:"title_sub"`
		Education    string `json:"education"`
		Experience   string `json:"experience"`
		CodingSkills string `json:"coding_skills"`
		Knowledge    string `json:"knowledge"`
		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
	}

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

	item, err := h.resumeService.CreateResume(
		req.Title,
		req.TitleSub,
		req.Education,
		req.Experience,
		req.CodingSkills,
		req.Knowledge,
		isActive,
	)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, item)
}

// UpdateResume updates an existing resume.
func (h *ResumeHandler) UpdateResume(c *gin.Context) {
	id := c.Param("id")
	var req struct {
		Title        *string `json:"title"`
		TitleSub     *string `json:"title_sub"`
		Education    *string `json:"education"`
		Experience   *string `json:"experience"`
		CodingSkills *string `json:"coding_skills"`
		Knowledge    *string `json:"knowledge"`
		IsActive     *bool   `json:"is_active"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	item, err := h.resumeService.UpdateResume(
		id,
		req.Title,
		req.TitleSub,
		req.Education,
		req.Experience,
		req.CodingSkills,
		req.Knowledge,
		req.IsActive,
	)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "resume not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, item)
}

// DeleteResume deletes a resume.
func (h *ResumeHandler) DeleteResume(c *gin.Context) {
	id := c.Param("id")
	if err := h.resumeService.DeleteResume(id); err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Resume deleted successfully"})
}
