package processor

import (
	"context"
	"fmt"

	"apskel-pos-be/internal/mappers"
	"apskel-pos-be/internal/models"
	"apskel-pos-be/internal/repository"

	"github.com/google/uuid"
)

type PaymentMethodProcessor interface {
	CreatePaymentMethod(ctx context.Context, req *models.CreatePaymentMethodRequest) (*models.PaymentMethodResponse, error)
	GetPaymentMethodByID(ctx context.Context, id uuid.UUID) (*models.PaymentMethodResponse, error)
	ListPaymentMethods(ctx context.Context, req *models.ListPaymentMethodsRequest) (*models.ListPaymentMethodsResponse, error)
	UpdatePaymentMethod(ctx context.Context, id uuid.UUID, req *models.UpdatePaymentMethodRequest) (*models.PaymentMethodResponse, error)
	DeletePaymentMethod(ctx context.Context, id uuid.UUID) error
	GetActivePaymentMethodsByOrganization(ctx context.Context, organizationID uuid.UUID) ([]models.PaymentMethodResponse, error)
}

type PaymentMethodProcessorImpl struct {
	paymentMethodRepo repository.PaymentMethodRepository
}

func NewPaymentMethodProcessorImpl(paymentMethodRepo repository.PaymentMethodRepository) *PaymentMethodProcessorImpl {
	return &PaymentMethodProcessorImpl{
		paymentMethodRepo: paymentMethodRepo,
	}
}

func (p *PaymentMethodProcessorImpl) CreatePaymentMethod(ctx context.Context, req *models.CreatePaymentMethodRequest) (*models.PaymentMethodResponse, error) {
	exists, err := p.paymentMethodRepo.ExistsByName(ctx, req.OrganizationID, req.Name, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to check payment method name uniqueness: %w", err)
	}
	if exists {
		return nil, fmt.Errorf("payment method with name '%s' already exists for this organization", req.Name)
	}

	paymentMethodEntity := mappers.CreatePaymentMethodRequestToEntity(req)
	if err := p.paymentMethodRepo.Create(ctx, paymentMethodEntity); err != nil {
		return nil, fmt.Errorf("failed to create payment method: %w", err)
	}

	createdPaymentMethod, err := p.paymentMethodRepo.GetByID(ctx, paymentMethodEntity.ID)
	if err != nil {
		return nil, fmt.Errorf("failed to retrieve created payment method: %w", err)
	}

	response := mappers.PaymentMethodEntityToResponse(createdPaymentMethod)
	return response, nil
}

func (p *PaymentMethodProcessorImpl) GetPaymentMethodByID(ctx context.Context, id uuid.UUID) (*models.PaymentMethodResponse, error) {
	paymentMethod, err := p.paymentMethodRepo.GetByID(ctx, id)
	if err != nil {
		return nil, fmt.Errorf("payment method not found: %w", err)
	}

	response := mappers.PaymentMethodEntityToResponse(paymentMethod)
	return response, nil
}

func (p *PaymentMethodProcessorImpl) ListPaymentMethods(ctx context.Context, req *models.ListPaymentMethodsRequest) (*models.ListPaymentMethodsResponse, error) {
	filters := make(map[string]interface{})
	if req.OrganizationID != nil {
		filters["organization_id"] = *req.OrganizationID
	}
	if req.Type != nil {
		filters["type"] = string(*req.Type)
	}
	if req.IsActive != nil {
		filters["is_active"] = *req.IsActive
	}
	if req.Search != "" {
		filters["search"] = req.Search
	}

	offset := (req.Page - 1) * req.Limit

	paymentMethods, total, err := p.paymentMethodRepo.List(ctx, filters, req.Limit, offset)
	if err != nil {
		return nil, fmt.Errorf("failed to list payment methods: %w", err)
	}

	// Convert to responses
	paymentMethodResponses := make([]models.PaymentMethodResponse, len(paymentMethods))
	for i, paymentMethod := range paymentMethods {
		response := mappers.PaymentMethodEntityToResponse(paymentMethod)
		if response != nil {
			paymentMethodResponses[i] = *response
		}
	}

	// Calculate total pages
	totalPages := int(total) / req.Limit
	if int(total)%req.Limit > 0 {
		totalPages++
	}

	return &models.ListPaymentMethodsResponse{
		PaymentMethods: paymentMethodResponses,
		TotalCount:     int(total),
		Page:           req.Page,
		Limit:          req.Limit,
		TotalPages:     totalPages,
	}, nil
}

func (p *PaymentMethodProcessorImpl) UpdatePaymentMethod(ctx context.Context, id uuid.UUID, req *models.UpdatePaymentMethodRequest) (*models.PaymentMethodResponse, error) {
	// Get existing payment method
	existingPaymentMethod, err := p.paymentMethodRepo.GetByID(ctx, id)
	if err != nil {
		return nil, fmt.Errorf("payment method not found: %w", err)
	}

	// Check name uniqueness if name is being updated
	if req.Name != nil && *req.Name != existingPaymentMethod.Name {
		exists, err := p.paymentMethodRepo.ExistsByName(ctx, existingPaymentMethod.OrganizationID, *req.Name, &id)
		if err != nil {
			return nil, fmt.Errorf("failed to check payment method name uniqueness: %w", err)
		}
		if exists {
			return nil, fmt.Errorf("payment method with name '%s' already exists for this organization", *req.Name)
		}
	}

	// Apply updates
	mappers.UpdatePaymentMethodEntityFromRequest(existingPaymentMethod, req)

	// Update payment method
	if err := p.paymentMethodRepo.Update(ctx, existingPaymentMethod); err != nil {
		return nil, fmt.Errorf("failed to update payment method: %w", err)
	}

	// Get updated payment method
	updatedPaymentMethod, err := p.paymentMethodRepo.GetByID(ctx, id)
	if err != nil {
		return nil, fmt.Errorf("failed to retrieve updated payment method: %w", err)
	}

	response := mappers.PaymentMethodEntityToResponse(updatedPaymentMethod)
	return response, nil
}

func (p *PaymentMethodProcessorImpl) DeletePaymentMethod(ctx context.Context, id uuid.UUID) error {
	// Check if payment method exists
	_, err := p.paymentMethodRepo.GetByID(ctx, id)
	if err != nil {
		return fmt.Errorf("payment method not found: %w", err)
	}

	// TODO: Check if payment method is being used in any payments
	// For now, allow deletion

	// Delete payment method
	if err := p.paymentMethodRepo.Delete(ctx, id); err != nil {
		return fmt.Errorf("failed to delete payment method: %w", err)
	}

	return nil
}

func (p *PaymentMethodProcessorImpl) GetActivePaymentMethodsByOrganization(ctx context.Context, organizationID uuid.UUID) ([]models.PaymentMethodResponse, error) {
	paymentMethods, err := p.paymentMethodRepo.GetActiveByOrganizationID(ctx, organizationID)
	if err != nil {
		return nil, fmt.Errorf("failed to get active payment methods: %w", err)
	}

	responses := make([]models.PaymentMethodResponse, len(paymentMethods))
	for i, paymentMethod := range paymentMethods {
		response := mappers.PaymentMethodEntityToResponse(paymentMethod)
		if response != nil {
			responses[i] = *response
		}
	}

	return responses, nil
}
