// Code generated by generate-dynamic-handler.go; DO NOT EDIT.
// Generated at: 2025-08-29 13:02:57
// Service: VClaim (vclaim)
// Description: BPJS VClaim service for eligibility and SEP management

package handlers

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"api-service/internal/config"
	"api-service/internal/models/reference"
	services "api-service/internal/services/bpjs"
	"api-service/pkg/logger"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
)

// VClaimHandler handles VClaim BPJS services
type VClaimHandler struct {
	service   services.VClaimService
	validator *validator.Validate
	logger    logger.Logger
	config    config.BpjsConfig
}

// VClaimHandlerConfig contains configuration for VClaimHandler
type VClaimHandlerConfig struct {
	BpjsConfig config.BpjsConfig
	Logger     logger.Logger
	Validator  *validator.Validate
}

// NewVClaimHandler creates a new VClaimHandler
func NewVClaimHandler(cfg VClaimHandlerConfig) *VClaimHandler {
	return &VClaimHandler{
		service:   services.NewService(cfg.BpjsConfig),
		validator: cfg.Validator,
		logger:    cfg.Logger,
		config:    cfg.BpjsConfig,
	}
}

// GetPESERTA retrieves Peserta data
// @Summary Get Peserta data
// @Description Get participant eligibility information
// @Tags vclaim,peserta
// @Accept json
// @Produce json
// @Param nokartu path string true "nokartu"
// @Success 200 {object} reference.PesertaResponse
// @Failure 400 {object} reference.ErrorResponse
// @Failure 500 {object} reference.ErrorResponse
// @Router /peserta/:nokartu [get]

func (h *VClaimHandler) GetPESERTA(c *gin.Context) {
	ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
	defer cancel()

	// Generate request ID if not present
	requestID := c.GetHeader("X-Request-ID")
	if requestID == "" {
		requestID = uuid.New().String()
		c.Header("X-Request-ID", requestID)
	}

	h.logger.Info("Processing GetPeserta request", map[string]interface{}{
		"request_id": requestID,
		"endpoint":   "/peserta/:nokartu",

		"nokartu": c.Param("nokartu"),
	})

	// Extract path parameters

	nokartu := c.Param("nokartu")
	if nokartu == "" {

		h.logger.Error("Missing required parameter nokartu", map[string]interface{}{
			"request_id": requestID,
		})

		c.JSON(http.StatusBadRequest, reference.ErrorResponse{
			Status:    "error",
			Message:   "Missing required parameter nokartu",
			RequestID: requestID,
		})
		return
	}

	// Call service method
	var response reference.PesertaResponse

	result, err := h.GetPeserta(ctx, nokartu)
	if err != nil {
		h.logger.Error("Failed to get Peserta", map[string]interface{}{
			"error":      err.Error(),
			"request_id": requestID,
		})

		c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
			Status:    "error",
			Message:   "Internal server error",
			RequestID: requestID,
		})
		return
	}

	// Assign result to response
	response.Status = "success"
	response.RequestID = requestID
	response.Data = result
	c.JSON(http.StatusOK, response)
}

// GetSEP retrieves Sep data

// @Summary Get Sep data
// @Description Manage SEP (Surat Eligibilitas Peserta)
// @Tags vclaim,sep
// @Accept json
// @Produce json
// @Param nosep path string true "nosep"
// @Success 200 {object} reference.SEPResponse
// @Failure 400 {object} reference.ErrorResponse
// @Failure 500 {object} reference.ErrorResponse
// @Router /sep/:nosep [get]

func (h *VClaimHandler) GetSEPHandler(c *gin.Context) {
	ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
	defer cancel()

	// Generate request ID if not present
	requestID := c.GetHeader("X-Request-ID")
	if requestID == "" {
		requestID = uuid.New().String()
		c.Header("X-Request-ID", requestID)
	}

	h.logger.Info("Processing GetSep request", map[string]interface{}{
		"request_id": requestID,
		"endpoint":   "/sep/:nosep",

		"nosep": c.Param("nosep"),
	})

	// Extract path parameters

	nosep := c.Param("nosep")
	if nosep == "" {

		h.logger.Error("Missing required parameter nosep", map[string]interface{}{
			"request_id": requestID,
		})

		c.JSON(http.StatusBadRequest, reference.ErrorResponse{
			Status:    "error",
			Message:   "Missing required parameter nosep",
			RequestID: requestID,
		})
		return
	}

	// Call service method
	var response reference.SEPResponse

	result, err := h.GetSEP(ctx, nosep)
	if err != nil {
		h.logger.Error("Failed to get Sep", map[string]interface{}{
			"error":      err.Error(),
			"request_id": requestID,
		})

		c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
			Status:    "error",
			Message:   "Internal server error",
			RequestID: requestID,
		})
		return
	}

	// Assign result to response
	response.Status = "success"
	response.RequestID = requestID
	response.Data = result
	c.JSON(http.StatusOK, response)
}

// CreateSEP creates new Sep

// @Summary Create Sep
// @Description Manage SEP (Surat Eligibilitas Peserta)
// @Tags vclaim,sep
// @Accept json
// @Produce json
// @Param request body reference.SEPRequest true "Sep data"
// @Success 201 {object} reference.SEPResponse
// @Failure 400 {object} reference.ErrorResponse
// @Failure 500 {object} reference.ErrorResponse
// @Router /sep [post]

func (h *VClaimHandler) CreateSEPHandler(c *gin.Context) {
	ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
	defer cancel()

	requestID := c.GetHeader("X-Request-ID")
	if requestID == "" {
		requestID = uuid.New().String()
		c.Header("X-Request-ID", requestID)
	}

	h.logger.Info("Processing CreateSep request", map[string]interface{}{
		"request_id": requestID,
	})

	var req reference.SEPRequest
	if err := c.ShouldBindJSON(&req); err != nil {

		h.logger.Error("Invalid request body", map[string]interface{}{
			"error":      err.Error(),
			"request_id": requestID,
		})

		c.JSON(http.StatusBadRequest, reference.ErrorResponse{
			Status:    "error",
			Message:   "Invalid request body: " + err.Error(),
			RequestID: requestID,
		})
		return
	}

	// Validate request
	if err := h.validator.Struct(&req); err != nil {

		h.logger.Error("Validation failed", map[string]interface{}{
			"error":      err.Error(),
			"request_id": requestID,
		})

		c.JSON(http.StatusBadRequest, reference.ErrorResponse{
			Status:    "error",
			Message:   "Validation failed: " + err.Error(),
			RequestID: requestID,
		})
		return
	}

	// Call service method
	var response reference.SEPResponse
	result, err := h.CreateSEP(ctx, &req)
	if err != nil {
		h.logger.Error("Failed to create Sep", map[string]interface{}{
			"error":      err.Error(),
			"request_id": requestID,
		})

		c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
			Status:    "error",
			Message:   "Internal server error",
			RequestID: requestID,
		})
		return
	}

	// Assign result to response
	response.Status = "success"
	response.RequestID = requestID
	response.Data = result
	c.JSON(http.StatusCreated, response)
}

// Handler for Helpers

// GetPeserta implements reference.VClaimService.GetPeserta
func (h *VClaimHandler) GetPeserta(ctx context.Context, noKartu string) (*reference.PesertaData, error) {
	var response struct {
		MetaData struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"metaData"`
		Response reference.PesertaData `json:"response"`
	}

	endpoint := fmt.Sprintf("/Peserta/%s", noKartu)
	err := h.service.Get(ctx, endpoint, &response)
	if err != nil {
		return nil, err
	}

	if response.MetaData.Code != "200" {
		return nil, fmt.Errorf("BPJS API error: %s - %s", response.MetaData.Code, response.MetaData.Message)
	}

	return &response.Response, nil
}

// GetSEP implements reference.VClaimService.GetSEP
func (h *VClaimHandler) GetSEP(ctx context.Context, noSep string) (*reference.SEPData, error) {
	var response struct {
		MetaData struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"metaData"`
		Response reference.SEPData `json:"response"`
	}

	endpoint := fmt.Sprintf("/SEP/%s", noSep)
	err := h.service.Get(ctx, endpoint, &response)
	if err != nil {
		return nil, err
	}

	if response.MetaData.Code != "200" {
		return nil, fmt.Errorf("BPJS API error: %s - %s", response.MetaData.Code, response.MetaData.Message)
	}

	return &response.Response, nil
}

// CreateSEP implements reference.VClaimService.CreateSEP
func (h *VClaimHandler) CreateSEP(ctx context.Context, req *reference.SEPRequest) (*reference.SEPData, error) {
	var response struct {
		MetaData struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"metaData"`
		Response reference.SEPData `json:"response"`
	}

	endpoint := "/SEP/2.0/insert"
	err := h.service.Post(ctx, endpoint, req, &response)
	if err != nil {
		return nil, err
	}

	if response.MetaData.Code != "200" {
		return nil, fmt.Errorf("BPJS API error: %s - %s", response.MetaData.Code, response.MetaData.Message)
	}

	return &response.Response, nil
}

// UpdateSEP implements reference.VClaimService.UpdateSEP
func (h *VClaimHandler) UpdateSEP(ctx context.Context, noSep string, req *reference.SEPRequest) (*reference.SEPData, error) {
	var response struct {
		MetaData struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"metaData"`
		Response reference.SEPData `json:"response"`
	}

	endpoint := fmt.Sprintf("/SEP/%s", noSep)
	err := h.service.Put(ctx, endpoint, req, &response)
	if err != nil {
		return nil, err
	}

	if response.MetaData.Code != "200" {
		return nil, fmt.Errorf("BPJS API error: %s - %s", response.MetaData.Code, response.MetaData.Message)
	}

	return &response.Response, nil
}

// DeleteSEP implements reference.VClaimService.DeleteSEP
func (h *VClaimHandler) DeleteSEP(ctx context.Context, noSep string) error {
	var response struct {
		MetaData struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"metaData"`
	}

	endpoint := fmt.Sprintf("/SEP/%s", noSep)
	err := h.service.Delete(ctx, endpoint, &response)
	if err != nil {
		return err
	}

	if response.MetaData.Code != "200" {
		return fmt.Errorf("BPJS API error: %s - %s", response.MetaData.Code, response.MetaData.Message)
	}

	return nil
}

// GetRujukan implements reference.VClaimService.GetRujukan
func (h *VClaimHandler) GetRujukan(ctx context.Context, noRujukan string) (*reference.RujukanData, error) {
	var response struct {
		MetaData struct {
			Code    string `json:"code"`
			Message string `json:"message"`
		} `json:"metaData"`
		Response reference.RujukanData `json:"response"`
	}

	endpoint := fmt.Sprintf("/Rujukan/%s", noRujukan)
	err := h.service.Get(ctx, endpoint, &response)
	if err != nil {
		return nil, err
	}

	if response.MetaData.Code != "200" {
		return nil, fmt.Errorf("BPJS API error: %s - %s", response.MetaData.Code, response.MetaData.Message)
	}

	return &response.Response, nil
}
