package handler

import (
	"errors"
	"net/http"
	"strings"

	"github.com/gofrs/uuid/v5"
)

// GetDashboard returns the dashboard data based on user role (Admin or Seller).
// @Summary Get dashboard data
// @Description Get dashboard data for the authenticated user (Admin or Seller)
// @Tags Dashboard
// @Security BearerAuth
// @Produce json
// @Success 200 {object} map[string]interface{}
// @Failure 401 {object} map[string]string
// @Failure 403 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /api/v1/dashboard [get]
func (h *Handler) GetDashboard(w http.ResponseWriter, r *http.Request) {
	req, err := getRequester(r)
	if err != nil {
		writeError(w, http.StatusUnauthorized, errors.New("Unauthorized"))
		return
	}

	if strings.EqualFold(req.Role, "Admin") {
		stats, err := h.svc.GetAdminDashboard(r.Context())
		if err != nil {
			writeError(w, http.StatusInternalServerError, err)
			return
		}
		writeJSON(w, http.StatusOK, stats)
		return
	}

	// Assume Seller/User - verify they have a company
	if req.CompanyID == nil || *req.CompanyID == uuid.Nil {
		writeError(w, http.StatusBadRequest, errors.New("user has no associated company"))
		return
	}

	stats, err := h.svc.GetSellerDashboard(r.Context(), *req.CompanyID)
	if err != nil {
		writeError(w, http.StatusInternalServerError, err)
		return
	}
	writeJSON(w, http.StatusOK, stats)
}
