package admin

import (
	"net/http"
	"strconv"

	"github.com/go-chi/chi/v5"

	"github.com/ultisuite/ulti-backend/internal/api/apiresponse"
	"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
	"github.com/ultisuite/ulti-backend/internal/api/middleware"
)

func (h *Handler) GetAIUsage(w http.ResponseWriter, r *http.Request) {
	scope := r.URL.Query().Get("scope")
	if scope == "" {
		scope = "org"
	}
	summary, err := h.svc.GetAIUsageSummary(r.Context(), scope)
	if err != nil {
		h.logger.Error("get ai usage", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, summary)
}

func (h *Handler) GetUserAIUsage(w http.ResponseWriter, r *http.Request) {
	userID := chi.URLParam(r, "userID")
	limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
	offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
	detail, err := h.svc.GetUserAIUsage(r.Context(), userID, limit, offset)
	if err != nil {
		if err.Error() == "user not found" {
			apiresponse.WriteError(w, r, http.StatusNotFound, apiresponse.CodeNotFound, err.Error(), nil)
			return
		}
		h.logger.Error("get user ai usage", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, detail)
}

func (h *Handler) GetAIPricing(w http.ResponseWriter, r *http.Request) {
	prices, err := h.svc.ListAIPricing(r.Context())
	if err != nil {
		h.logger.Error("list ai pricing", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"prices": prices})
}

func (h *Handler) PutAIPricing(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	var req putAIPricingRequest
	if err := apivalidate.DecodeJSON(w, r, maxQuotaRequestBody, &req); err != nil {
		return
	}
	prices, err := h.svc.PutAIPricing(r.Context(), claims.Sub, req)
	if err != nil {
		h.logger.Error("put ai pricing", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"prices": prices})
}

func (h *Handler) GetAICostPolicies(w http.ResponseWriter, r *http.Request) {
	policies, err := h.svc.ListAICostPolicies(r.Context())
	if err != nil {
		h.logger.Error("list ai cost policies", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"policies": policies})
}

func (h *Handler) PutUserAICostPolicy(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	userID := chi.URLParam(r, "userID")
	if verr := validateUserID(userID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}
	var req putAICostPolicyRequest
	if err := apivalidate.DecodeJSON(w, r, maxQuotaRequestBody, &req); err != nil {
		return
	}
	if err := h.svc.PutUserAICostPolicy(r.Context(), claims.Sub, userID, req); err != nil {
		h.logger.Error("put user ai cost policy", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
}

func (h *Handler) PutGroupAICostPolicy(w http.ResponseWriter, r *http.Request) {
	claims := middleware.ClaimsFromContext(r.Context())
	groupID := chi.URLParam(r, "groupID")
	if verr := validateGroupID(groupID); verr != nil {
		apivalidate.WriteValidationError(w, r, verr)
		return
	}
	var req putAICostPolicyRequest
	if err := apivalidate.DecodeJSON(w, r, maxQuotaRequestBody, &req); err != nil {
		return
	}
	if err := h.svc.PutGroupAICostPolicy(r.Context(), claims.Sub, groupID, req); err != nil {
		h.logger.Error("put group ai cost policy", "error", err)
		apiresponse.WriteError(w, r, http.StatusInternalServerError, apiresponse.CodeInternal, err.Error(), nil)
		return
	}
	apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
}
