package vclaim

import (
	"net/http"
	sepService "service/internal/bpjs/vclaim/sep"
	"service/pkg/errors"
	"service/pkg/response"

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

// SepHandler menangani semua request HTTP terkait VClaim.
type SepHandler struct {
	sepService sepService.Service
}

// NewSepHandler membuat instance SepHandler baru.
func NewSepHandler(sepService sepService.Service) *SepHandler {
	return &SepHandler{
		sepService: sepService,
	}
}

// RegisterRoutes mendaftarkan semua rute untuk BPJS VClaim.
func (h *SepHandler) RegisterRoutes(router *gin.RouterGroup) {
	bpjsGroup := router.Group("/bpjs/vclaim")
	{
		sepGroup := bpjsGroup.Group("/sep")
		{
			sepGroup.POST("", h.CreateSEP)
			sepGroup.PUT("", h.UpdateSEP)
			// BPJS API untuk delete menggunakan method POST, tapi kita ekspos sebagai DELETE untuk konsistensi RESTful.
			sepGroup.DELETE("", h.DeleteSEP)
			sepGroup.GET("/:nosep", h.GetSEPDetail)
		}
	}
}

// CreateSEP menangani request untuk membuat SEP baru.
func (h *SepHandler) CreateSEP(c *gin.Context) {
	var req sepService.CreateSEPRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid request body", err.Error())
		return
	}

	result, err := h.sepService.Create(c.Request.Context(), req)
	if err != nil {
		appErr := errors.FromError(err)
		response.Error(c, appErr.HTTPStatus(), appErr.Error(), nil)
		return
	}

	response.Success(c, http.StatusCreated, "Successfully created SEP", result)
}

// UpdateSEP menangani request untuk memperbarui SEP.
func (h *SepHandler) UpdateSEP(c *gin.Context) {
	var req sepService.UpdateSEPRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid request body", err.Error())
		return
	}

	result, err := h.sepService.Update(c.Request.Context(), req)
	if err != nil {
		appErr := errors.FromError(err)
		response.Error(c, appErr.HTTPStatus(), appErr.Error(), nil)
		return
	}

	response.Success(c, http.StatusOK, "Successfully updated SEP", result)
}

// GetSEPDetail menangani request untuk mendapatkan detail SEP.
func (h *SepHandler) GetSEPDetail(c *gin.Context) {
	noSEP := c.Param("no_sep")

	result, err := h.sepService.GetDetail(c.Request.Context(), noSEP)
	if err != nil {
		appErr := errors.FromError(err)
		response.Error(c, appErr.HTTPStatus(), appErr.Error(), nil)
		return
	}

	response.Success(c, http.StatusOK, "Successfully retrieved SEP detail", result)
}

// DeleteSEP menangani request untuk menghapus SEP.
func (h *SepHandler) DeleteSEP(c *gin.Context) {
	var req sepService.DeleteSEPRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid request body for SEP deletion", err.Error())
		return
	}

	result, err := h.sepService.Delete(c.Request.Context(), req)
	if err != nil {
		appErr := errors.FromError(err)
		response.Error(c, appErr.HTTPStatus(), appErr.Error(), nil)
		return
	}

	response.Success(c, http.StatusOK, "Successfully deleted SEP", result)
}
