package apotek

import (
	"net/http"

	dphoService "service/internal/bpjs/apotek/reference/dpho"
	poliService "service/internal/bpjs/apotek/reference/poli"
	"service/pkg/errors"
	"service/pkg/response"

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

type ReferenceHandler struct {
	dphoService dphoService.Service
	poliService poliService.Service
}

func NewReferenceHandler(dpho dphoService.Service, poli poliService.Service) *ReferenceHandler {
	return &ReferenceHandler{
		dphoService: dpho,
		poliService: poli,
	}
}

func (h *ReferenceHandler) RegisterRoutes(router *gin.RouterGroup) {
	group := router.Group("/bpjs/apotek/reference")
	{
		group.GET("/dpho", h.GetDPHO)
		group.GET("/poli/:param", h.GetPoli)
	}
}

func (h *ReferenceHandler) GetDPHO(c *gin.Context) {
	res, err := h.dphoService.GetDPHO(c.Request.Context())
	if err != nil {
		appErr := errors.FromError(err)
		response.Error(c, appErr.HTTPStatus(), appErr.Error(), appErr.Metadata())
		return
	}
	response.Success(c, http.StatusOK, "Successfully retrieved BPJS Apotek DPHO reference", res)
}

func (h *ReferenceHandler) GetPoli(c *gin.Context) {
	param := c.Param("param")
	res, err := h.poliService.GetPoli(c.Request.Context(), param)
	if err != nil {
		appErr := errors.FromError(err)
		response.Error(c, appErr.HTTPStatus(), appErr.Error(), appErr.Metadata())
		return
	}
	response.Success(c, http.StatusOK, "Successfully retrieved BPJS Apotek Poli reference", res)
}
