package handlers

import (
	"net/http"

	"github.com/calendarapi/internal/middleware"
	"github.com/calendarapi/internal/models"
	"github.com/calendarapi/internal/service"
	"github.com/calendarapi/internal/utils"
	"github.com/go-chi/chi/v5"
)

type CalendarHandler struct {
	calSvc *service.CalendarService
}

func NewCalendarHandler(calSvc *service.CalendarService) *CalendarHandler {
	return &CalendarHandler{calSvc: calSvc}
}

func (h *CalendarHandler) List(w http.ResponseWriter, r *http.Request) {
	userID, _ := middleware.GetUserID(r.Context())

	calendars, err := h.calSvc.List(r.Context(), userID)
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	utils.WriteList(w, calendars, models.PageInfo{Limit: utils.DefaultLimit})
}

func (h *CalendarHandler) Create(w http.ResponseWriter, r *http.Request) {
	userID, _ := middleware.GetUserID(r.Context())

	var req struct {
		Name  string `json:"name"`
		Color string `json:"color"`
	}
	if err := utils.DecodeJSON(r, &req); err != nil {
		utils.WriteError(w, err)
		return
	}

	cal, err := h.calSvc.Create(r.Context(), userID, req.Name, req.Color)
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	utils.WriteJSON(w, http.StatusOK, map[string]interface{}{"calendar": cal})
}

func (h *CalendarHandler) Get(w http.ResponseWriter, r *http.Request) {
	userID, _ := middleware.GetUserID(r.Context())
	calID, err := utils.ValidateUUID(chi.URLParam(r, "id"))
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	cal, err := h.calSvc.Get(r.Context(), userID, calID)
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	utils.WriteJSON(w, http.StatusOK, map[string]interface{}{"calendar": cal})
}

func (h *CalendarHandler) Update(w http.ResponseWriter, r *http.Request) {
	userID, _ := middleware.GetUserID(r.Context())
	calID, err := utils.ValidateUUID(chi.URLParam(r, "id"))
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	var req struct {
		Name                   *string `json:"name"`
		Color                  *string `json:"color"`
		IsPublic               *bool   `json:"is_public"`
		CountForAvailability   *bool   `json:"count_for_availability"`
		DefaultReminderMinutes *int    `json:"default_reminder_minutes"`
		SortOrder              *int    `json:"sort_order"`
	}
	if err := utils.DecodeJSON(r, &req); err != nil {
		utils.WriteError(w, err)
		return
	}

	cal, err := h.calSvc.Update(r.Context(), userID, calID, req.Name, req.Color, req.IsPublic, req.CountForAvailability, req.DefaultReminderMinutes, req.SortOrder)
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	utils.WriteJSON(w, http.StatusOK, map[string]interface{}{"calendar": cal})
}

func (h *CalendarHandler) Delete(w http.ResponseWriter, r *http.Request) {
	userID, _ := middleware.GetUserID(r.Context())
	calID, err := utils.ValidateUUID(chi.URLParam(r, "id"))
	if err != nil {
		utils.WriteError(w, err)
		return
	}

	if err := h.calSvc.Delete(r.Context(), userID, calID); err != nil {
		utils.WriteError(w, err)
		return
	}

	utils.WriteOK(w)
}
