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

type CreateResumeRequest 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"`
}

type UpdateResumeRequest 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"`
}

// GetAllResumes godoc
// @Summary Get all active resumes
// @Description Retrieve a list of active resumes
// @Tags resume
// @Produce json
// @Success 200 {array} models.Resume
// @Failure 500 {object} map[string]string
// @Router /resumes [get]
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 godoc
// @Summary Get active resume
// @Description Retrieve the newest active resume entry
// @Tags resume
// @Produce json
// @Success 200 {object} models.Resume
// @Failure 404 {object} map[string]string
// @Router /resumes/active [get]
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 godoc
// @Summary Get all resumes (Admin)
// @Description Retrieve a list of all resumes including inactive ones
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {array} models.Resume
// @Failure 500 {object} map[string]string
// @Router /admin/resumes [get]
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 godoc
// @Summary Get a resume by ID (Admin)
// @Description Retrieve details of a specific resume
// @Tags admin
// @Security ApiKeyAuth
// @Produce json
// @Param id path string true "Resume ID"
// @Success 200 {object} models.Resume
// @Failure 404 {object} map[string]string
// @Router /admin/resumes/{id} [get]
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 godoc
// @Summary Create a new resume (Admin)
// @Description Create a new resume entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param request body CreateResumeRequest true "Resume Request"
// @Success 201 {object} models.Resume
// @Failure 400 {object} map[string]string
// @Router /admin/resumes [post]
func (h *ResumeHandler) CreateResume(c *gin.Context) {
	var req CreateResumeRequest
	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.resumeService.CreateResume(
		req.Title,
		req.TitleSub,
		strings.TrimSpace(req.Education),
		strings.TrimSpace(req.Experience),
		strings.TrimSpace(req.CodingSkills),
		strings.TrimSpace(req.Knowledge),
		isActive,
	)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, item)
}

// UpdateResume godoc
// @Summary Update a resume (Admin)
// @Description Update an existing resume entry
// @Tags admin
// @Security ApiKeyAuth
// @Accept json
// @Produce json
// @Param id path string true "Resume ID"
// @Param request body UpdateResumeRequest true "Resume Request"
// @Success 200 {object} models.Resume
// @Failure 400 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/resumes/{id} [put]
func (h *ResumeHandler) UpdateResume(c *gin.Context) {
	id := c.Param("id")
	var req UpdateResumeRequest
	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
	}
	if req.Education != nil {
		trimmed := strings.TrimSpace(*req.Education)
		req.Education = &trimmed
	}
	if req.Experience != nil {
		trimmed := strings.TrimSpace(*req.Experience)
		req.Experience = &trimmed
	}
	if req.CodingSkills != nil {
		trimmed := strings.TrimSpace(*req.CodingSkills)
		req.CodingSkills = &trimmed
	}
	if req.Knowledge != nil {
		trimmed := strings.TrimSpace(*req.Knowledge)
		req.Knowledge = &trimmed
	}

	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 godoc
// @Summary Delete a resume (Admin)
// @Description Delete a resume by ID
// @Tags admin
// @Security ApiKeyAuth
// @Param id path string true "Resume ID"
// @Success 200 {object} map[string]string
// @Failure 404 {object} map[string]string
// @Router /admin/resumes/{id} [delete]
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"})
}
