package handlers

import (
	"net/http"

	"cmr-backend/internal/apperr"
	"cmr-backend/internal/httpapi/middleware"
	"cmr-backend/internal/httpx"
	"cmr-backend/internal/service"
)

type OpsAuthHandler struct {
	service *service.OpsAuthService
}

func NewOpsAuthHandler(service *service.OpsAuthService) *OpsAuthHandler {
	return &OpsAuthHandler{service: service}
}

func (h *OpsAuthHandler) SendSMSCode(w http.ResponseWriter, r *http.Request) {
	var req service.OpsSendSMSCodeInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}
	result, err := h.service.SendSMSCode(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *OpsAuthHandler) Register(w http.ResponseWriter, r *http.Request) {
	var req service.OpsRegisterInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}
	result, err := h.service.Register(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *OpsAuthHandler) LoginSMS(w http.ResponseWriter, r *http.Request) {
	var req service.OpsLoginSMSInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}
	result, err := h.service.LoginSMS(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *OpsAuthHandler) Refresh(w http.ResponseWriter, r *http.Request) {
	var req service.OpsRefreshTokenInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}
	result, err := h.service.Refresh(r.Context(), req)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}

func (h *OpsAuthHandler) Logout(w http.ResponseWriter, r *http.Request) {
	var req service.OpsLogoutInput
	if err := httpx.DecodeJSON(r, &req); err != nil {
		httpx.WriteError(w, apperr.New(http.StatusBadRequest, "invalid_json", "invalid request body"))
		return
	}
	if err := h.service.Logout(r.Context(), req); err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": map[string]any{"loggedOut": true}})
}

func (h *OpsAuthHandler) Me(w http.ResponseWriter, r *http.Request) {
	auth := middleware.GetOpsAuthContext(r.Context())
	if auth == nil {
		httpx.WriteError(w, apperr.New(http.StatusUnauthorized, "unauthorized", "missing ops auth context"))
		return
	}
	result, err := h.service.GetMe(r.Context(), auth.OpsUserID)
	if err != nil {
		httpx.WriteError(w, err)
		return
	}
	httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
}
