package handler

import (
	"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 AccountHandler struct {
	service   contract.AccountContract
	validator validator.AccountValidator
}

func NewAccountHandler(service contract.AccountContract, validator validator.AccountValidator) *AccountHandler {
	return &AccountHandler{
		service:   service,
		validator: validator,
	}
}

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

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

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

func (h *AccountHandler) GetAccountByID(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"}}), "AccountHandler")
		return
	}

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

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

func (h *AccountHandler) UpdateAccount(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"}}), "AccountHandler")
		return
	}

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

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

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

func (h *AccountHandler) DeleteAccount(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"}}), "AccountHandler")
		return
	}

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

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(gin.H{"message": "Account deleted successfully"}), "AccountHandler")
}

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

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

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

func (h *AccountHandler) GetAccountsByOrganization(c *gin.Context) {
	organizationIDStr := c.Param("organization_id")
	organizationID, err := uuid.Parse(organizationIDStr)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: "Invalid organization ID format"}}), "AccountHandler")
		return
	}

	var outletID *uuid.UUID
	if outletIDStr := c.Query("outlet_id"); outletIDStr != "" {
		if parsedOutletID, err := uuid.Parse(outletIDStr); err == nil {
			outletID = &parsedOutletID
		}
	}

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

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

func (h *AccountHandler) GetAccountsByChartOfAccount(c *gin.Context) {
	chartOfAccountIDStr := c.Param("chart_of_account_id")
	chartOfAccountID, err := uuid.Parse(chartOfAccountIDStr)
	if err != nil {
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{{Cause: "Invalid chart of account ID format"}}), "AccountHandler")
		return
	}

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

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

func (h *AccountHandler) UpdateAccountBalance(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"}}), "AccountHandler")
		return
	}

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

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

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(gin.H{"message": "Account balance updated successfully"}), "AccountHandler")
}

func (h *AccountHandler) GetAccountBalance(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"}}), "AccountHandler")
		return
	}

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

	util.HandleResponse(c.Writer, c.Request, contract.BuildSuccessResponse(gin.H{"balance": balance}), "AccountHandler")
}
