package mapper

import (
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"web-qudo-be/app/database/entity"
	res "web-qudo-be/app/module/approval_workflow_steps/response"
	usersRepository "web-qudo-be/app/module/users/repository"
)

func ApprovalWorkflowStepsResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	approvalWorkflowStepsReq *entity.ApprovalWorkflowSteps,
	usersRepo usersRepository.UsersRepository,
) (approvalWorkflowStepsRes *res.ApprovalWorkflowStepsResponse) {

	if approvalWorkflowStepsReq != nil {
		// Convert boolean pointers to boolean values
		isOptional := false
		if approvalWorkflowStepsReq.CanSkip != nil {
			isOptional = *approvalWorkflowStepsReq.CanSkip
		}

		autoApprove := false
		if approvalWorkflowStepsReq.AutoApproveAfterHours != nil {
			autoApprove = *approvalWorkflowStepsReq.AutoApproveAfterHours > 0
		}

		approvalWorkflowStepsRes = &res.ApprovalWorkflowStepsResponse{
			ID:               approvalWorkflowStepsReq.ID,
			WorkflowID:       approvalWorkflowStepsReq.WorkflowId,
			StepName:         approvalWorkflowStepsReq.StepName,
			StepOrder:        approvalWorkflowStepsReq.StepOrder,
			ApproverRoleID:   approvalWorkflowStepsReq.RequiredUserLevelId,
			IsOptional:       isOptional,
			RequiresComment:  false, // Default value
			AutoApprove:      autoApprove,
			TimeoutHours:     approvalWorkflowStepsReq.AutoApproveAfterHours,
			CreatedAt:        approvalWorkflowStepsReq.CreatedAt,
			UpdatedAt:        approvalWorkflowStepsReq.UpdatedAt,
		}
	}

	return approvalWorkflowStepsRes
}

func ApprovalWorkflowStepsSummaryResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	approvalWorkflowStepsReq *entity.ApprovalWorkflowSteps,
	usersRepo usersRepository.UsersRepository,
) (approvalWorkflowStepsRes *res.ApprovalWorkflowStepsSummaryResponse) {

	if approvalWorkflowStepsReq != nil {
		// Convert boolean pointers to boolean values
		isOptional := false
		if approvalWorkflowStepsReq.CanSkip != nil {
			isOptional = *approvalWorkflowStepsReq.CanSkip
		}

		approvalWorkflowStepsRes = &res.ApprovalWorkflowStepsSummaryResponse{
			ID:              approvalWorkflowStepsReq.ID,
			WorkflowID:      approvalWorkflowStepsReq.WorkflowId,
			StepName:        approvalWorkflowStepsReq.StepName,
			StepOrder:       approvalWorkflowStepsReq.StepOrder,
			ApproverRoleID:  approvalWorkflowStepsReq.RequiredUserLevelId,
			IsOptional:      isOptional,
			RequiresComment: false, // Default value
			TimeoutHours:    approvalWorkflowStepsReq.AutoApproveAfterHours,
		}
	}

	return approvalWorkflowStepsRes
}