package service

import (
	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/processor"
	"context"
	"fmt"

	"github.com/google/uuid"
)

type ProductRecipeService interface {
	Create(ctx context.Context, organizationID uuid.UUID, req *contract.CreateProductRecipeRequest) (*contract.ProductRecipeResponse, error)
	GetByID(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error)
	GetByProductID(ctx context.Context, req *contract.GetProductRecipeByProductIDRequest, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error)
	GetByIngredientID(ctx context.Context, ingredientID uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error)
	Update(ctx context.Context, id uuid.UUID, organizationID uuid.UUID, req *contract.UpdateProductRecipeRequest) (*contract.ProductRecipeResponse, error)
	Delete(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) error
	BulkCreate(ctx context.Context, organizationID uuid.UUID, req *contract.BulkCreateProductRecipeRequest) ([]*contract.ProductRecipeResponse, error)
}

type ProductRecipeServiceImpl struct {
	processor processor.ProductRecipeProcessor
}

func NewProductRecipeService(processor processor.ProductRecipeProcessor) *ProductRecipeServiceImpl {
	return &ProductRecipeServiceImpl{
		processor: processor,
	}
}

func (s *ProductRecipeServiceImpl) Create(ctx context.Context, organizationID uuid.UUID, req *contract.CreateProductRecipeRequest) (*contract.ProductRecipeResponse, error) {
	// Validate request
	if req == nil {
		return nil, fmt.Errorf("request cannot be nil")
	}

	// Call processor to handle business logic
	return s.processor.Create(ctx, req, organizationID)
}

func (s *ProductRecipeServiceImpl) GetByID(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) (*contract.ProductRecipeResponse, error) {
	// Validate ID
	if id == uuid.Nil {
		return nil, fmt.Errorf("invalid recipe ID")
	}

	return s.processor.GetByID(ctx, id, organizationID)
}

func (s *ProductRecipeServiceImpl) GetByProductID(ctx context.Context, req *contract.GetProductRecipeByProductIDRequest, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error) {
	// Validate request
	if req == nil {
		return nil, fmt.Errorf("request cannot be nil")
	}

	// Validate product ID
	if req.ProductID == uuid.Nil {
		return nil, fmt.Errorf("invalid product ID")
	}

	// If variant ID is provided, get by product and variant
	if req.VariantID != nil {
		return s.processor.GetByProductAndVariantID(ctx, req.ProductID, req.VariantID, organizationID)
	}

	// Otherwise get by product ID only
	return s.processor.GetByProductID(ctx, req.ProductID, organizationID)
}

func (s *ProductRecipeServiceImpl) GetByIngredientID(ctx context.Context, ingredientID uuid.UUID, organizationID uuid.UUID) ([]*contract.ProductRecipeResponse, error) {
	// Validate ingredient ID
	if ingredientID == uuid.Nil {
		return nil, fmt.Errorf("invalid ingredient ID")
	}

	return s.processor.GetByIngredientID(ctx, ingredientID, organizationID)
}

func (s *ProductRecipeServiceImpl) Update(ctx context.Context, id uuid.UUID, organizationID uuid.UUID, req *contract.UpdateProductRecipeRequest) (*contract.ProductRecipeResponse, error) {
	// Validate ID
	if id == uuid.Nil {
		return nil, fmt.Errorf("invalid recipe ID")
	}

	// Validate request
	if req == nil {
		return nil, fmt.Errorf("request cannot be nil")
	}

	return s.processor.Update(ctx, id, req, organizationID)
}

func (s *ProductRecipeServiceImpl) Delete(ctx context.Context, id uuid.UUID, organizationID uuid.UUID) error {
	// Validate ID
	if id == uuid.Nil {
		return fmt.Errorf("invalid recipe ID")
	}

	return s.processor.Delete(ctx, id, organizationID)
}

func (s *ProductRecipeServiceImpl) BulkCreate(ctx context.Context, organizationID uuid.UUID, req *contract.BulkCreateProductRecipeRequest) ([]*contract.ProductRecipeResponse, error) {
	// Validate request
	if req == nil {
		return nil, fmt.Errorf("request cannot be nil")
	}

	if len(req.Recipes) == 0 {
		return nil, fmt.Errorf("at least one recipe is required")
	}

	return s.processor.BulkCreate(ctx, req.Recipes, organizationID)
}