package escalas

import (
	"net/http"

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

type Handler struct {
	service *Service
}

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

// Create godoc
// @Summary      Create a schedule entry
// @Description  Assign a professional to a time block in an event
// @Tags         escalas
// @Accept       json
// @Produce      json
// @Param        request body CreateEscalaInput true "Create Escala"
// @Success      201  {object}  map[string]string
// @Router       /api/escalas [post]
func (h *Handler) Create(c *gin.Context) {
	var req CreateEscalaInput
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// Security: Block RESEARCHER
	if c.GetString("role") == "RESEARCHER" {
		c.JSON(http.StatusForbidden, gin.H{"error": "Acesso negado: Somente leitura"})
		return
	}

	escala, err := h.service.Create(c.Request.Context(), req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusCreated, gin.H{"id": uuid.UUID(escala.ID.Bytes).String()})
}

// ListByAgenda godoc
// @Summary      List schedules for an event
// @Description  Get all schedule entries for a specific agenda_id
// @Tags         escalas
// @Accept       json
// @Produce      json
// @Param        agenda_id query string true "Agenda ID"
// @Success      200  {array}   map[string]interface{}
// @Router       /api/escalas [get]
func (h *Handler) ListByAgenda(c *gin.Context) {
	agendaID := c.Query("agenda_id")
	if agendaID == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "agenda_id is required"})
		return
	}

	escalas, err := h.service.ListByAgenda(c.Request.Context(), agendaID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	resp := make([]map[string]interface{}, len(escalas))
	for i, e := range escalas {
		var funcao string
		if e.FuncaoEspecifica.Valid {
			funcao = e.FuncaoEspecifica.String
		}
		var profNome string
		profNome = e.ProfissionalNome
		var avatar string
		if e.AvatarUrl.Valid {
			avatar = e.AvatarUrl.String
		}
		var phone string
		if e.Whatsapp.Valid {
			if c.GetString("role") == "RESEARCHER" {
				phone = "Bloqueado"
			} else {
				phone = e.Whatsapp.String
			}
		}
		var profFuncao string
		if e.FuncaoNome.Valid {
			profFuncao = e.FuncaoNome.String
		}

		resp[i] = map[string]interface{}{
			"id":                uuid.UUID(e.ID.Bytes).String(),
			"agenda_id":         uuid.UUID(e.AgendaID.Bytes).String(),
			"profissional_id":   uuid.UUID(e.ProfissionalID.Bytes).String(),
			"profissional_nome": profNome,
			"avatar_url":        avatar,
			"phone":             phone,
			"profissional_role": profFuncao,
			"start":             e.DataHoraInicio.Time,
			"end":               e.DataHoraFim.Time,
			"role":              funcao, // Specific role in this slot
		}
	}

	c.JSON(http.StatusOK, resp)
}

// Delete godoc
// @Summary      Delete a schedule entry
// @Description  Remove a professional from a time block
// @Tags         escalas
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Escala ID"
// @Success      200  {object}  map[string]string
// @Router       /api/escalas/{id} [delete]
func (h *Handler) Delete(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "id required"})
		return
	}

	// Security: Block RESEARCHER
	if c.GetString("role") == "RESEARCHER" {
		c.JSON(http.StatusForbidden, gin.H{"error": "Acesso negado: Somente leitura"})
		return
	}

	err := h.service.Delete(c.Request.Context(), id)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}

// Update godoc
// @Summary      Update a schedule entry
// @Description  Update time or professional for a slot
// @Tags         escalas
// @Accept       json
// @Produce      json
// @Param        id   path      string  true  "Escala ID"
// @Param        request body UpdateEscalaInput true "Update Escala"
// @Success      200  {object}  map[string]string
// @Router       /api/escalas/{id} [put]
func (h *Handler) Update(c *gin.Context) {
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "id required"})
		return
	}

	var req UpdateEscalaInput
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// Security: Block RESEARCHER
	if c.GetString("role") == "RESEARCHER" {
		c.JSON(http.StatusForbidden, gin.H{"error": "Acesso negado: Somente leitura"})
		return
	}

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

	c.JSON(http.StatusOK, gin.H{"message": "updated"})
}
