package handlers

import (
	"net/http"
	"strings"

	"gauth-central/internal/services"

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

type ExperienceHandler struct {
	experienceService *services.ExperienceService
}

func NewExperienceHandler(experienceService *services.ExperienceService) *ExperienceHandler {
	return &ExperienceHandler{experienceService: experienceService}
}

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

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

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

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

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

func (h *ExperienceHandler) CreateExperience(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.experienceService.CreateExperience(
		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 *ExperienceHandler) UpdateExperience(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.experienceService.UpdateExperience(
		id,
		req.BetweenYears,
		req.Title,
		req.Content,
		resumeUUID,
		req.IsActive,
	)
	if err != nil {
		status := http.StatusInternalServerError
		if err.Error() == "experience not found" {
			status = http.StatusNotFound
		}
		c.JSON(status, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, item)
}

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