package handlers

import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type EducationHandler struct {
	educationService *services.EducationService
}

func NewEducationHandler(educationService *services.EducationService) *EducationHandler {
	return &EducationHandler{educationService: educationService}
}

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

	// This is public endpoint, returns only active
	items, err := h.educationService.GetAllEducations(resumeID, true)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

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

	// Admin sees all
	items, err := h.educationService.GetAllEducations(resumeID, false)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, items)
}

func (h *EducationHandler) GetEducationByID(c *gin.Context) {
	id := c.Param("id")
	item, err := h.educationService.GetEducationByID(id)
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

func (h *EducationHandler) CreateEducation(c *gin.Context) {
	var req struct {
		BetweenYears string `json:"between_years"`
		Title        string `json:"title"`
		Content      string `json:"content"`
		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.educationService.CreateEducation(
		req.BetweenYears,
		req.Title,
		req.Content,
		resumeUUID,
		isActive,
	)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusCreated, item)
}

func (h *EducationHandler) UpdateEducation(c *gin.Context) {
	id := c.Param("id")
	var req struct {
		BetweenYears *string `json:"between_years"`
		Title        *string `json:"title"`
		Content      *string `json:"content"`
		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.educationService.UpdateEducation(
		id,
		req.BetweenYears,
		req.Title,
		req.Content,
		resumeUUID,
		req.IsActive,
	)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "education not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

func (h *EducationHandler) DeleteEducation(c *gin.Context) {
	id := c.Param("id")
	if err := h.educationService.DeleteEducation(id); err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "Education deleted successfully"})
}
