package mapper

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

func ApprovalWorkflowsResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	approvalWorkflowsReq *entity.ApprovalWorkflows,
	approvalWorkflowStepsRepo approvalWorkflowStepsRepository.ApprovalWorkflowStepsRepository,
	usersRepo usersRepository.UsersRepository,
) (approvalWorkflowsRes *res.ApprovalWorkflowsResponse) {

	// Get workflow steps using GetAll with filter
	var workflowStepsArr []*approvalWorkflowStepsResponse.ApprovalWorkflowStepsResponse
	if len(approvalWorkflowsReq.Steps) > 0 {
		for _, step := range approvalWorkflowsReq.Steps {
			workflowStepsArr = append(workflowStepsArr, approvalWorkflowStepsMapper.ApprovalWorkflowStepsResponseMapper(
				log,
				clientId,
				&step,
				usersRepo,
			))
		}
	}

	if approvalWorkflowsReq != nil {
		// Convert boolean pointer to boolean value
		isActive := false
		if approvalWorkflowsReq.IsActive != nil {
			isActive = *approvalWorkflowsReq.IsActive
		}



		approvalWorkflowsRes = &res.ApprovalWorkflowsResponse{
			ID:          approvalWorkflowsReq.ID,
			Name:        approvalWorkflowsReq.Name,
			Description: approvalWorkflowsReq.Description,
			IsActive:    isActive,
			CreatedBy:   0, // Default value since entity doesn't have CreatedBy field
			CreatedAt:   approvalWorkflowsReq.CreatedAt,
			UpdatedAt:   approvalWorkflowsReq.UpdatedAt,
			Steps:       workflowStepsArr,
		}
	}

	return approvalWorkflowsRes
}

func ApprovalWorkflowsWithStepsResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	approvalWorkflowsReq *entity.ApprovalWorkflows,
	approvalWorkflowStepsRepo approvalWorkflowStepsRepository.ApprovalWorkflowStepsRepository,
	usersRepo usersRepository.UsersRepository,
) (approvalWorkflowsRes *res.ApprovalWorkflowsWithStepsResponse) {

	// Get workflow steps with detailed information
	var workflowStepsArr []*approvalWorkflowStepsResponse.ApprovalWorkflowStepsResponse
	if len(approvalWorkflowsReq.Steps) > 0 {
		for _, step := range approvalWorkflowsReq.Steps {
			workflowStepsArr = append(workflowStepsArr, approvalWorkflowStepsMapper.ApprovalWorkflowStepsResponseMapper(
				log,
				clientId,
				&step,
				usersRepo,
			))
		}
	}

	if approvalWorkflowsReq != nil {
		// Convert boolean pointer to boolean value
		isActive := false
		if approvalWorkflowsReq.IsActive != nil {
			isActive = *approvalWorkflowsReq.IsActive
		}

		approvalWorkflowsRes = &res.ApprovalWorkflowsWithStepsResponse{
			ID:          approvalWorkflowsReq.ID,
			Name:        approvalWorkflowsReq.Name,
			Description: approvalWorkflowsReq.Description,
			IsActive:    isActive,
			CreatedBy:   0, // Default value since entity doesn't have CreatedBy field
			CreatedAt:   approvalWorkflowsReq.CreatedAt,
			UpdatedAt:   approvalWorkflowsReq.UpdatedAt,
			Steps:       workflowStepsArr,
		}
	}

	return approvalWorkflowsRes
}

func ApprovalWorkflowsSummaryResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	approvalWorkflowsReq *entity.ApprovalWorkflows,
	usersRepo usersRepository.UsersRepository,
) (approvalWorkflowsRes *res.ApprovalWorkflowsSummaryResponse) {

	if approvalWorkflowsReq != nil {
		// Convert boolean pointer to boolean value
		isActive := false
		if approvalWorkflowsReq.IsActive != nil {
			isActive = *approvalWorkflowsReq.IsActive
		}

		approvalWorkflowsRes = &res.ApprovalWorkflowsSummaryResponse{
			ID:          approvalWorkflowsReq.ID,
			Name:        approvalWorkflowsReq.Name,
			Description: approvalWorkflowsReq.Description,
			IsActive:    isActive,
			StepCount:   0, // Default value, should be calculated if needed
		}
	}

	return approvalWorkflowsRes
}