package cursos

import (
	"net/http"
	"strings"

	"photum-backend/internal/db/generated"

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

type Handler struct {
	service *Service
}

func NewHandler(service *Service) *Handler {
	return &Handler{service: service}
}

type CursoResponse struct {
	ID   string `json:"id"`
	Nome string `json:"nome"`
}

type CreateCursoRequest struct {
	Nome string `json:"nome" binding:"required"`
}

func toResponse(c generated.Curso) CursoResponse {
	return CursoResponse{
		ID:   uuid.UUID(c.ID.Bytes).String(),
		Nome: c.Nome,
	}
}

// Create godoc
// @Summary      Create a new course
// @Tags         cursos
// @Accept       json
// @Produce      json
// @Security     BearerAuth
// @Param        request body CreateCursoRequest true "Curso Name"
// @Success      201  {object}  CursoResponse
// @Failure      400  {object}  map[string]string
// @Failure      409  {object}  map[string]string
// @Router       /api/cursos [post]
func (h *Handler) Create(c *gin.Context) {
	var req CreateCursoRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	regiao := c.GetString("regiao")
	curso, err := h.service.Create(c.Request.Context(), req.Nome, regiao)
	if err != nil {
		if strings.Contains(err.Error(), "duplicate key value") {
			c.JSON(http.StatusConflict, gin.H{"error": "Curso already exists"})
			return
		}
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, toResponse(*curso))
}

// List godoc
// @Summary      List all courses
// @Tags         cursos
// @Accept       json
// @Produce      json
// @Security     BearerAuth
// @Success      200  {array}  CursoResponse
// @Router       /api/cursos [get]
func (h *Handler) List(c *gin.Context) {
	regiao := c.GetString("regiao")
	cursos, err := h.service.List(c.Request.Context(), regiao)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	var response []CursoResponse
	for _, curso := range cursos {
		response = append(response, toResponse(curso))
	}
	c.JSON(http.StatusOK, response)
}

// Update godoc
// @Summary      Update a course
// @Tags         cursos
// @Accept       json
// @Produce      json
// @Security     BearerAuth
// @Param        id   path      string  true  "Curso ID"
// @Param        request body CreateCursoRequest true "Curso Name"
// @Success      200  {object}  CursoResponse
// @Router       /api/cursos/{id} [put]
func (h *Handler) Update(c *gin.Context) {
	id := c.Param("id")
	var req CreateCursoRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	regiao := c.GetString("regiao")
	curso, err := h.service.Update(c.Request.Context(), id, req.Nome, regiao)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, toResponse(*curso))
}

// Delete godoc
// @Summary      Delete a course
// @Tags         cursos
// @Accept       json
// @Produce      json
// @Security     BearerAuth
// @Param        id   path      string  true  "Curso ID"
// @Success      204  {object}  nil
// @Router       /api/cursos/{id} [delete]
func (h *Handler) Delete(c *gin.Context) {
	id := c.Param("id")
	regiao := c.GetString("regiao")
	if err := h.service.Delete(c.Request.Context(), id, regiao); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.Status(http.StatusNoContent)
}
