package service

import (
	"errors"
	"fmt"
	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/approval_workflow_steps/repository"
	"web-qudo-be/app/module/approval_workflow_steps/request"
	workflowRepo "web-qudo-be/app/module/approval_workflows/repository"
	"web-qudo-be/utils/paginator"

	"github.com/google/uuid"
	"github.com/rs/zerolog"
)

type approvalWorkflowStepsService struct {
	ApprovalWorkflowStepsRepository repository.ApprovalWorkflowStepsRepository
	ApprovalWorkflowsRepository     workflowRepo.ApprovalWorkflowsRepository
	Log                             zerolog.Logger
}

// ApprovalWorkflowStepsService define interface of IApprovalWorkflowStepsService
type ApprovalWorkflowStepsService interface {
	// Basic CRUD
	GetAll(clientId *uuid.UUID, req request.GetApprovalWorkflowStepsRequest) (steps []*entity.ApprovalWorkflowSteps, paging paginator.Pagination, err error)
	FindOne(clientId *uuid.UUID, id uint) (step *entity.ApprovalWorkflowSteps, err error)
	Create(clientId *uuid.UUID, step *entity.ApprovalWorkflowSteps) (stepReturn *entity.ApprovalWorkflowSteps, err error)
	Update(clientId *uuid.UUID, id uint, step *entity.ApprovalWorkflowSteps) (err error)
	Delete(clientId *uuid.UUID, id uint) (err error)

	// Workflow steps management
	GetByWorkflowID(clientId *uuid.UUID, workflowID uint) (steps []*entity.ApprovalWorkflowSteps, err error)
	// GetByRoleID(clientId *uuid.UUID, roleID uint) (steps []*entity.ApprovalWorkflowSteps, err error) // Not implemented yet
	BulkCreate(clientId *uuid.UUID, workflowID uint, steps []*entity.ApprovalWorkflowSteps) (stepsReturn []*entity.ApprovalWorkflowSteps, err error)
	ReorderSteps(clientId *uuid.UUID, workflowID uint, stepOrders []struct {
		ID        uint
		StepOrder int
	}) (err error)

	// Validation
	ValidateStep(clientId *uuid.UUID, step *entity.ApprovalWorkflowSteps) (isValid bool, errors []string, err error)
	CanDeleteStep(clientId *uuid.UUID, id uint) (canDelete bool, reason string, err error)
	ValidateStepOrder(clientId *uuid.UUID, workflowID uint, stepOrder int, excludeID *uint) (isValid bool, err error)
}

func NewApprovalWorkflowStepsService(
	approvalWorkflowStepsRepository repository.ApprovalWorkflowStepsRepository,
	approvalWorkflowsRepository workflowRepo.ApprovalWorkflowsRepository,
	log zerolog.Logger,
) ApprovalWorkflowStepsService {
	return &approvalWorkflowStepsService{
		ApprovalWorkflowStepsRepository: approvalWorkflowStepsRepository,
		ApprovalWorkflowsRepository:     approvalWorkflowsRepository,
		Log:                             log,
	}
}

func (_i *approvalWorkflowStepsService) GetAll(clientId *uuid.UUID, req request.GetApprovalWorkflowStepsRequest) (steps []*entity.ApprovalWorkflowSteps, paging paginator.Pagination, err error) {
	return _i.ApprovalWorkflowStepsRepository.GetAll(clientId, req)
}

func (_i *approvalWorkflowStepsService) FindOne(clientId *uuid.UUID, id uint) (step *entity.ApprovalWorkflowSteps, err error) {
	return _i.ApprovalWorkflowStepsRepository.FindOne(clientId, id)
}

func (_i *approvalWorkflowStepsService) Create(clientId *uuid.UUID, step *entity.ApprovalWorkflowSteps) (stepReturn *entity.ApprovalWorkflowSteps, err error) {
	// Validate workflow exists
	workflow, err := _i.ApprovalWorkflowsRepository.FindOne(clientId, step.WorkflowId)
	if err != nil {
		return nil, fmt.Errorf("workflow not found: %w", err)
	}
	if workflow == nil {
		return nil, errors.New("workflow not found")
	}

	// Validate step order is unique within workflow
	isValid, err := _i.ValidateStepOrder(clientId, step.WorkflowId, step.StepOrder, nil)
	if err != nil {
		return nil, err
	}
	if !isValid {
		return nil, errors.New("step order already exists in this workflow")
	}

	// Validate step data
	isValid, validationErrors, err := _i.ValidateStep(clientId, step)
	if err != nil {
		return nil, err
	}
	if !isValid {
		return nil, fmt.Errorf("validation failed: %v", validationErrors)
	}

	return _i.ApprovalWorkflowStepsRepository.Create(clientId, step)
}

func (_i *approvalWorkflowStepsService) Update(clientId *uuid.UUID, id uint, step *entity.ApprovalWorkflowSteps) (err error) {
	// Check if step exists
	existingStep, err := _i.ApprovalWorkflowStepsRepository.FindOne(clientId, id)
	if err != nil {
		return err
	}
	if existingStep == nil {
		return errors.New("step not found")
	}

	// If step order is being changed, validate it's unique
	if step.StepOrder != 0 && step.StepOrder != existingStep.StepOrder {
		isValid, err := _i.ValidateStepOrder(clientId, existingStep.WorkflowId, step.StepOrder, &id)
		if err != nil {
			return err
		}
		if !isValid {
			return errors.New("step order already exists in this workflow")
		}
	}

	return _i.ApprovalWorkflowStepsRepository.Update(id, step)
}

func (_i *approvalWorkflowStepsService) Delete(clientId *uuid.UUID, id uint) (err error) {
	// Check if step can be deleted
	canDelete, reason, err := _i.CanDeleteStep(clientId, id)
	if err != nil {
		return err
	}
	if !canDelete {
		return fmt.Errorf("cannot delete step: %s", reason)
	}

	return _i.ApprovalWorkflowStepsRepository.Delete(clientId, id)
}

func (_i *approvalWorkflowStepsService) GetByWorkflowID(clientId *uuid.UUID, workflowID uint) (steps []*entity.ApprovalWorkflowSteps, err error) {
	return _i.ApprovalWorkflowStepsRepository.GetByWorkflowId(clientId, workflowID)
}

// GetByRoleID method is not implemented in repository yet
// func (_i *approvalWorkflowStepsService) GetByRoleID(clientId *uuid.UUID, roleID uint) (steps []*entity.ApprovalWorkflowSteps, err error) {
//	return _i.ApprovalWorkflowStepsRepository.GetByRoleID(clientId, roleID)
// }

func (_i *approvalWorkflowStepsService) BulkCreate(clientId *uuid.UUID, workflowID uint, steps []*entity.ApprovalWorkflowSteps) (stepsReturn []*entity.ApprovalWorkflowSteps, err error) {
	// Validate workflow exists
	workflow, err := _i.ApprovalWorkflowsRepository.FindOne(clientId, workflowID)
	if err != nil {
		return nil, fmt.Errorf("workflow not found: %w", err)
	}
	if workflow == nil {
		return nil, errors.New("workflow not found")
	}

	// Validate all steps
	stepOrders := make(map[int]bool)
	for i, step := range steps {
		step.WorkflowId = workflowID

		// Check for duplicate step orders within the batch
		if stepOrders[step.StepOrder] {
			return nil, fmt.Errorf("duplicate step order %d in batch", step.StepOrder)
		}
		stepOrders[step.StepOrder] = true

		// Validate step order is unique in database
		isValid, err := _i.ValidateStepOrder(clientId, workflowID, step.StepOrder, nil)
		if err != nil {
			return nil, err
		}
		if !isValid {
			return nil, fmt.Errorf("step order %d already exists in workflow", step.StepOrder)
		}

		// Validate step data
		var errors []string
		if step.RequiredUserLevelId == 0 {
			errors = append(errors, fmt.Sprintf("Step %d: RequiredUserLevelId is required", i+1))
		}

		if step.AutoApproveAfterHours != nil && *step.AutoApproveAfterHours <= 0 {
			errors = append(errors, fmt.Sprintf("Step %d: AutoApproveAfterHours must be positive", i+1))
		}

		if step.AutoApproveAfterHours != nil && *step.AutoApproveAfterHours > 720 {
			errors = append(errors, fmt.Sprintf("Step %d: AutoApproveAfterHours cannot exceed 720 hours (30 days)", i+1))
		}

		if len(step.StepName) > 500 {
			errors = append(errors, fmt.Sprintf("Step %d: StepName cannot exceed 500 characters", i+1))
		}

		if len(errors) > 0 {
			return nil, fmt.Errorf("validation failed for step %d: %v", step.StepOrder, errors)
		}
	}

	// BulkCreate method is not implemented in repository yet
	// return _i.ApprovalWorkflowStepsRepository.BulkCreate(clientId, steps)
	return nil, fmt.Errorf("BulkCreate method not implemented yet")
}

func (_i *approvalWorkflowStepsService) ReorderSteps(clientId *uuid.UUID, workflowID uint, stepOrders []struct {
	ID        uint
	StepOrder int
}) (err error) {
	// Validate workflow exists
	workflow, err := _i.ApprovalWorkflowsRepository.FindOne(clientId, workflowID)
	if err != nil {
		return fmt.Errorf("workflow not found: %w", err)
	}
	if workflow == nil {
		return errors.New("workflow not found")
	}

	// Validate all steps belong to the workflow and orders are unique
	orders := make(map[int]bool)
	for _, stepOrder := range stepOrders {
		// Check step exists and belongs to workflow
		step, err := _i.ApprovalWorkflowStepsRepository.FindOne(clientId, stepOrder.ID)
		if err != nil {
			return err
		}
		if step == nil {
			return fmt.Errorf("step with ID %d not found", stepOrder.ID)
		}
		if step.WorkflowId != workflowID {
			return fmt.Errorf("step with ID %d does not belong to workflow %d", stepOrder.ID, workflowID)
		}

		// Check for duplicate orders
		if orders[stepOrder.StepOrder] {
			return fmt.Errorf("duplicate step order %d", stepOrder.StepOrder)
		}
		orders[stepOrder.StepOrder] = true
	}

	// Convert stepOrders to the format expected by repository
	stepOrderMaps := make([]map[string]interface{}, len(stepOrders))
	for i, stepOrder := range stepOrders {
		stepOrderMaps[i] = map[string]interface{}{
			"id":         stepOrder.ID,
			"step_order": stepOrder.StepOrder,
		}
	}

	return _i.ApprovalWorkflowStepsRepository.ReorderSteps(clientId, workflowID, stepOrderMaps)
}

func (_i *approvalWorkflowStepsService) ValidateStep(clientId *uuid.UUID, step *entity.ApprovalWorkflowSteps) (isValid bool, errors []string, err error) {
	var validationErrors []string

	// Validate step name
	if step.StepName == "" {
		validationErrors = append(validationErrors, "step name is required")
	}
	if len(step.StepName) < 3 {
		validationErrors = append(validationErrors, "step name must be at least 3 characters")
	}
	if len(step.StepName) > 500 {
		validationErrors = append(validationErrors, "step name must not exceed 500 characters")
	}

	// Validate step order
	if step.StepOrder < 1 {
		validationErrors = append(validationErrors, "step order must be at least 1")
	}

	// Validate required user level ID
	if step.RequiredUserLevelId == 0 {
		validationErrors = append(validationErrors, "required user level ID is required")
	}

	// Validate auto approve after hours if provided
	if step.AutoApproveAfterHours != nil && *step.AutoApproveAfterHours < 1 {
		validationErrors = append(validationErrors, "auto approve after hours must be at least 1")
	}
	if step.AutoApproveAfterHours != nil && *step.AutoApproveAfterHours > 720 {
		validationErrors = append(validationErrors, "auto approve after hours must not exceed 720 (30 days)")
	}

	return len(validationErrors) == 0, validationErrors, nil
}

func (_i *approvalWorkflowStepsService) CanDeleteStep(clientId *uuid.UUID, id uint) (canDelete bool, reason string, err error) {
	// Check if step exists
	step, err := _i.ApprovalWorkflowStepsRepository.FindOne(clientId, id)
	if err != nil {
		return false, "", err
	}
	if step == nil {
		return false, "step not found", nil
	}

	// Check if there are any active approval flows using this step
	// This would require checking article_approval_step_logs table
	// For now, we'll allow deletion but this should be implemented
	// based on business requirements

	return true, "", nil
}

func (_i *approvalWorkflowStepsService) ValidateStepOrder(clientId *uuid.UUID, workflowID uint, stepOrder int, excludeID *uint) (isValid bool, err error) {
	existingStep, err := _i.ApprovalWorkflowStepsRepository.FindByWorkflowAndStep(clientId, workflowID, stepOrder)
	if err != nil {
		return false, err
	}

	// If no existing step found, order is valid
	if existingStep == nil {
		return true, nil
	}

	// If excludeID is provided and matches existing step, order is valid (updating same step)
	if excludeID != nil && existingStep.ID == *excludeID {
		return true, nil
	}

	// Order already exists for different step
	return false, nil
}
