package handler

import (
	"strconv"

	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/util"
	"apskel-pos-be/internal/validator"

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

type ChartOfAccountTypeHandler struct {
	service   contract.ChartOfAccountTypeContract
	validator validator.ChartOfAccountTypeValidator
}

func NewChartOfAccountTypeHandler(service contract.ChartOfAccountTypeContract, validator validator.ChartOfAccountTypeValidator) *ChartOfAccountTypeHandler {
	return &ChartOfAccountTypeHandler{
		service:   service,
		validator: validator,
	}
}

func (h *ChartOfAccountTypeHandler) CreateChartOfAccountType(c *gin.Context) {
	var req contract.CreateChartOfAccountTypeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	response, err := h.service.CreateChartOfAccountType(c, &req)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "ChartOfAccountTypeHandler")
}

func (h *ChartOfAccountTypeHandler) GetChartOfAccountTypeByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := uuid.Parse(idStr)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: "Invalid ID format"}}), "ChartOfAccountTypeHandler")
		return
	}

	response, err := h.service.GetChartOfAccountTypeByID(c, id)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "ChartOfAccountTypeHandler")
}

func (h *ChartOfAccountTypeHandler) UpdateChartOfAccountType(c *gin.Context) {
	idStr := c.Param("id")
	id, err := uuid.Parse(idStr)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: "Invalid ID format"}}), "ChartOfAccountTypeHandler")
		return
	}

	var req contract.UpdateChartOfAccountTypeRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	response, err := h.service.UpdateChartOfAccountType(c, id, &req)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(response), "ChartOfAccountTypeHandler")
}

func (h *ChartOfAccountTypeHandler) DeleteChartOfAccountType(c *gin.Context) {
	idStr := c.Param("id")
	id, err := uuid.Parse(idStr)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: "Invalid ID format"}}), "ChartOfAccountTypeHandler")
		return
	}

	err = h.service.DeleteChartOfAccountType(c, id)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(gin.H{"message": "Chart of account type deleted successfully"}), "ChartOfAccountTypeHandler")
}

func (h *ChartOfAccountTypeHandler) ListChartOfAccountTypes(c *gin.Context) {
	// Parse query parameters
	filters := make(map[string]interface{})
	
	if isActive := c.Query("is_active"); isActive != "" {
		if isActiveBool, err := strconv.ParseBool(isActive); err == nil {
			filters["is_active"] = isActiveBool
		}
	}

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))

	response, total, err := h.service.ListChartOfAccountTypes(c, filters, page, limit)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: err.Error()}}), "ChartOfAccountTypeHandler")
		return
	}

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(gin.H{
		"data":  response,
		"total": total,
		"page":  page,
		"limit": limit,
	}), "ChartOfAccountTypeHandler")
}
