package controller

import (
	"jaecoo-be/app/module/approval_histories/request"
	"jaecoo-be/app/module/approval_histories/service"
	"jaecoo-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"

	utilRes "jaecoo-be/utils/response"
)

type approvalHistoriesController struct {
	approvalHistoriesService service.ApprovalHistoriesService
	Log                      zerolog.Logger
}

type ApprovalHistoriesController interface {
	GetByModule(c *fiber.Ctx) error
	All(c *fiber.Ctx) error
}

func NewApprovalHistoriesController(approvalHistoriesService service.ApprovalHistoriesService, log zerolog.Logger) ApprovalHistoriesController {
	return &approvalHistoriesController{
		approvalHistoriesService: approvalHistoriesService,
		Log:                      log,
	}
}

// GetByModule get approval history by module type and module id
// @Summary      Get approval history by module
// @Description  API for getting approval history by module type and module id
// @Tags         ApprovalHistories
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param        module_type path string true "Module Type (banners, galleries, products, sales_agents, promotions)"
// @Param        module_id path int true "Module ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /approval-histories/{module_type}/{module_id} [get]
func (_i *approvalHistoriesController) GetByModule(c *fiber.Ctx) error {
	moduleType := c.Params("module_type")
	moduleIdStr := c.Params("module_id")

	moduleId, err := strconv.ParseUint(moduleIdStr, 10, 0)
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"Invalid module_id"},
		})
	}

	histories, err := _i.approvalHistoriesService.GetByModule(moduleType, uint(moduleId))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Approval histories successfully retrieved"},
		Data:     histories,
	})
}

// All get all ApprovalHistories
// @Summary      Get all ApprovalHistories
// @Description  API for getting all ApprovalHistories
// @Tags         ApprovalHistories
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     req query request.ApprovalHistoriesQueryRequest false "query parameters"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /approval-histories [get]
func (_i *approvalHistoriesController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	req := request.ApprovalHistoriesQueryRequest{
		Pagination: *paginate,
	}

	if moduleType := c.Query("module_type"); moduleType != "" {
		req.ModuleType = &moduleType
	}
	if moduleIdStr := c.Query("module_id"); moduleIdStr != "" {
		moduleId, err := strconv.ParseUint(moduleIdStr, 10, 0)
		if err == nil {
			moduleIdUint := uint(moduleId)
			req.ModuleId = &moduleIdUint
		}
	}
	if statusIdStr := c.Query("status_id"); statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			req.StatusId = &statusId
		}
	}
	if action := c.Query("action"); action != "" {
		req.Action = &action
	}
	if approvedByStr := c.Query("approved_by"); approvedByStr != "" {
		approvedBy, err := strconv.ParseUint(approvedByStr, 10, 0)
		if err == nil {
			approvedByUint := uint(approvedBy)
			req.ApprovedBy = &approvedByUint
		}
	}

	histories, paging, err := _i.approvalHistoriesService.GetAll(req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Approval histories successfully retrieved"},
		Data:     histories,
		Meta:     paging,
	})
}
