package handler

import (
	"apskel-pos-be/internal/appcontext"
	"apskel-pos-be/internal/constants"
	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/logger"
	"apskel-pos-be/internal/service"
	"apskel-pos-be/internal/util"
	"apskel-pos-be/internal/validator"

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

type ProductVariantHandler struct {
	productVariantService   service.ProductVariantService
	productVariantValidator validator.ProductVariantValidator
}

func NewProductVariantHandler(
	productVariantService service.ProductVariantService,
	productVariantValidator validator.ProductVariantValidator,
) *ProductVariantHandler {
	return &ProductVariantHandler{
		productVariantService:   productVariantService,
		productVariantValidator: productVariantValidator,
	}
}

func (h *ProductVariantHandler) CreateProductVariant(c *gin.Context) {
	ctx := c.Request.Context()
	contextInfo := appcontext.FromGinContext(ctx)

	var req contract.CreateProductVariantRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.FromContext(c.Request.Context()).WithError(err).Error("ProductVariantHandler::CreateProductVariant -> request binding failed")
		validationResponseError := contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, err.Error())
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::CreateProductVariant")
		return
	}

	validationError, validationErrorCode := h.productVariantValidator.ValidateCreateProductVariantRequest(&req)
	if validationError != nil {
		validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::CreateProductVariant")
		return
	}

	variantResponse := h.productVariantService.CreateProductVariant(ctx, contextInfo, &req)
	if variantResponse.HasErrors() {
		errorResp := variantResponse.GetErrors()[0]
		logger.FromContext(ctx).WithError(errorResp).Error("ProductVariantHandler::CreateProductVariant -> Failed to create product variant from service")
	}

	util.HandleResponse(c.Writer, c.Request, variantResponse, "ProductVariantHandler::CreateProductVariant")
}

func (h *ProductVariantHandler) UpdateProductVariant(c *gin.Context) {
	ctx := c.Request.Context()

	variantIDStr := c.Param("id")
	variantID, err := uuid.Parse(variantIDStr)
	if err != nil {
		logger.FromContext(ctx).WithError(err).Error("ProductVariantHandler::UpdateProductVariant -> Invalid variant ID")
		validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid variant ID")
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::UpdateProductVariant")
		return
	}

	var req contract.UpdateProductVariantRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		logger.FromContext(ctx).WithError(err).Error("ProductVariantHandler::UpdateProductVariant -> request binding failed")
		validationResponseError := contract.NewResponseError(constants.MissingFieldErrorCode, constants.RequestEntity, "Invalid request body")
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::UpdateProductVariant")
		return
	}

	validationError, validationErrorCode := h.productVariantValidator.ValidateUpdateProductVariantRequest(&req)
	if validationError != nil {
		validationResponseError := contract.NewResponseError(validationErrorCode, constants.RequestEntity, validationError.Error())
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::UpdateProductVariant")
		return
	}

	variantResponse := h.productVariantService.UpdateProductVariant(ctx, variantID, &req)
	if variantResponse.HasErrors() {
		errorResp := variantResponse.GetErrors()[0]
		logger.FromContext(ctx).WithError(errorResp).Error("ProductVariantHandler::UpdateProductVariant -> Failed to update product variant from service")
	}

	util.HandleResponse(c.Writer, c.Request, variantResponse, "ProductVariantHandler::UpdateProductVariant")
}

func (h *ProductVariantHandler) DeleteProductVariant(c *gin.Context) {
	ctx := c.Request.Context()

	variantIDStr := c.Param("id")
	variantID, err := uuid.Parse(variantIDStr)
	if err != nil {
		logger.FromContext(ctx).WithError(err).Error("ProductVariantHandler::DeleteProductVariant -> Invalid variant ID")
		validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid variant ID")
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::DeleteProductVariant")
		return
	}

	variantResponse := h.productVariantService.DeleteProductVariant(ctx, variantID)
	if variantResponse.HasErrors() {
		errorResp := variantResponse.GetErrors()[0]
		logger.FromContext(ctx).WithError(errorResp).Error("ProductVariantHandler::DeleteProductVariant -> Failed to delete product variant from service")
	}

	util.HandleResponse(c.Writer, c.Request, variantResponse, "ProductVariantHandler::DeleteProductVariant")
}

func (h *ProductVariantHandler) GetProductVariant(c *gin.Context) {
	ctx := c.Request.Context()

	variantIDStr := c.Param("id")
	variantID, err := uuid.Parse(variantIDStr)
	if err != nil {
		logger.FromContext(ctx).WithError(err).Error("ProductVariantHandler::GetProductVariant -> Invalid variant ID")
		validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid variant ID")
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::GetProductVariant")
		return
	}

	variantResponse := h.productVariantService.GetProductVariantByID(ctx, variantID)
	if variantResponse.HasErrors() {
		errorResp := variantResponse.GetErrors()[0]
		logger.FromContext(ctx).WithError(errorResp).Error("ProductVariantHandler::GetProductVariant -> Failed to get product variant from service")
	}

	util.HandleResponse(c.Writer, c.Request, variantResponse, "ProductVariantHandler::GetProductVariant")
}

func (h *ProductVariantHandler) GetProductVariantsByProduct(c *gin.Context) {
	ctx := c.Request.Context()

	productIDStr := c.Param("product_id")
	productID, err := uuid.Parse(productIDStr)
	if err != nil {
		logger.FromContext(ctx).WithError(err).Error("ProductVariantHandler::GetProductVariantsByProduct -> Invalid product ID")
		validationResponseError := contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "Invalid product ID")
		util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{validationResponseError}), "ProductVariantHandler::GetProductVariantsByProduct")
		return
	}

	variantsResponse := h.productVariantService.GetProductVariantsByProductID(ctx, productID)
	if variantsResponse.HasErrors() {
		errorResp := variantsResponse.GetErrors()[0]
		logger.FromContext(ctx).WithError(errorResp).Error("ProductVariantHandler::GetProductVariantsByProduct -> Failed to get product variants from service")
	}

	util.HandleResponse(c.Writer, c.Request, variantsResponse, "ProductVariantHandler::GetProductVariantsByProduct")
}
