/**
 * TypeScript types for Recipes service
 * Mirrored from backend schemas: services/recipes/app/schemas/recipes.py
 * Backend models: services/recipes/app/models/recipes.py
 *
 * Coverage:
 * - Recipe CRUD (create, update, search, response)
 * - Recipe Ingredients (create, update, response)
 * - Quality Configuration (stage-based quality checks)
 * - Recipe Operations (duplicate, activate, feasibility)
 * - Statistics & Analytics
 */

// ================================================================
// ENUMS
// ================================================================

/**
 * Recipe lifecycle status
 * Backend: RecipeStatus enum in models/recipes.py
 */
export enum RecipeStatus {
  DRAFT = 'draft',
  ACTIVE = 'active',
  TESTING = 'testing',
  ARCHIVED = 'archived',
  DISCONTINUED = 'discontinued'
}

/**
 * Units for recipe measurements
 * Backend: MeasurementUnit enum in models/recipes.py
 */
export enum MeasurementUnit {
  GRAMS = 'g',
  KILOGRAMS = 'kg',
  MILLILITERS = 'ml',
  LITERS = 'l',
  CUPS = 'cups',
  TABLESPOONS = 'tbsp',
  TEASPOONS = 'tsp',
  UNITS = 'units',
  PIECES = 'pieces',
  PERCENTAGE = '%'
}

/**
 * Production batch status
 * Backend: ProductionStatus enum in models/recipes.py
 */
export enum ProductionStatus {
  PLANNED = 'planned',
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  FAILED = 'failed',
  CANCELLED = 'cancelled'
}

// ================================================================
// QUALITY CONFIGURATION TYPES
// ================================================================

/**
 * Quality checks configuration per production stage
 * Backend: QualityStageConfiguration in schemas/recipes.py (lines 16-22)
 */
export interface QualityStageConfiguration {
  template_ids?: string[];  // Default: []
  required_checks?: string[];  // Default: []
  optional_checks?: string[];  // Default: []
  blocking_on_failure?: boolean;  // Default: true
  min_quality_score?: number | null;  // ge=0, le=10
}

/**
 * Recipe quality configuration across all stages
 * Backend: RecipeQualityConfiguration in schemas/recipes.py (lines 25-31)
 */
export interface RecipeQualityConfiguration {
  stages?: Record<string, QualityStageConfiguration>;  // Default: {}
  overall_quality_threshold?: number;  // Default: 7.0, ge=0, le=10
  critical_stage_blocking?: boolean;  // Default: true
  auto_create_quality_checks?: boolean;  // Default: true
  quality_manager_approval_required?: boolean;  // Default: false
}

/**
 * Schema for updating recipe quality configuration
 * Backend: RecipeQualityConfigurationUpdate in schemas/recipes.py (lines 34-40)
 */
export interface RecipeQualityConfigurationUpdate {
  stages?: Record<string, QualityStageConfiguration> | null;
  overall_quality_threshold?: number | null;  // ge=0, le=10
  critical_stage_blocking?: boolean | null;
  auto_create_quality_checks?: boolean | null;
  quality_manager_approval_required?: boolean | null;
}

// ================================================================
// RECIPE INGREDIENT TYPES
// ================================================================

/**
 * Schema for creating recipe ingredients
 * Backend: RecipeIngredientCreate in schemas/recipes.py (lines 43-56)
 */
export interface RecipeIngredientCreate {
  ingredient_id: string;
  quantity: number;  // gt=0
  unit: MeasurementUnit;
  alternative_quantity?: number | null;
  alternative_unit?: MeasurementUnit | null;
  preparation_method?: string | null;
  ingredient_notes?: string | null;
  is_optional?: boolean;  // Default: false
  ingredient_order: number;  // ge=1
  ingredient_group?: string | null;
  substitution_options?: Record<string, any> | null;
  substitution_ratio?: number | null;
}

/**
 * Schema for updating recipe ingredients
 * Backend: RecipeIngredientUpdate in schemas/recipes.py (lines 59-72)
 */
export interface RecipeIngredientUpdate {
  ingredient_id?: string | null;
  quantity?: number | null;  // gt=0
  unit?: MeasurementUnit | null;
  alternative_quantity?: number | null;
  alternative_unit?: MeasurementUnit | null;
  preparation_method?: string | null;
  ingredient_notes?: string | null;
  is_optional?: boolean | null;
  ingredient_order?: number | null;  // ge=1
  ingredient_group?: string | null;
  substitution_options?: Record<string, any> | null;
  substitution_ratio?: number | null;
}

/**
 * Schema for recipe ingredient responses
 * Backend: RecipeIngredientResponse in schemas/recipes.py (lines 75-98)
 */
export interface RecipeIngredientResponse {
  id: string;
  tenant_id: string;
  recipe_id: string;
  ingredient_id: string;
  quantity: number;
  unit: string;
  quantity_in_base_unit?: number | null;
  alternative_quantity?: number | null;
  alternative_unit?: string | null;
  preparation_method?: string | null;
  ingredient_notes?: string | null;
  is_optional: boolean;
  ingredient_order: number;
  ingredient_group?: string | null;
  substitution_options?: Record<string, any> | null;
  substitution_ratio?: number | null;
  unit_cost?: number | null;
  total_cost?: number | null;
  cost_updated_at?: string | null;
}

// ================================================================
// RECIPE CRUD TYPES
// ================================================================

/**
 * Schema for creating recipes
 * Backend: RecipeCreate in schemas/recipes.py (lines 101-138)
 */
export interface RecipeCreate {
  name: string;  // min_length=1, max_length=255
  recipe_code?: string | null;  // max_length=100
  version?: string;  // Default: "1.0", max_length=20
  finished_product_id: string;
  description?: string | null;
  category?: string | null;  // max_length=100
  cuisine_type?: string | null;  // max_length=100
  difficulty_level?: number;  // Default: 1, ge=1, le=5
  yield_quantity: number;  // gt=0
  yield_unit: MeasurementUnit;
  prep_time_minutes?: number | null;  // ge=0
  cook_time_minutes?: number | null;  // ge=0
  total_time_minutes?: number | null;  // ge=0
  rest_time_minutes?: number | null;  // ge=0
  instructions?: Record<string, any> | null;
  preparation_notes?: string | null;
  storage_instructions?: string | null;
  quality_standards?: string | null;
  quality_check_configuration?: RecipeQualityConfiguration | null;
  serves_count?: number | null;  // ge=1
  nutritional_info?: Record<string, any> | null;
  allergen_info?: Record<string, any> | null;
  dietary_tags?: Record<string, any> | null;
  batch_size_multiplier?: number;  // Default: 1.0, gt=0
  minimum_batch_size?: number | null;  // gt=0
  maximum_batch_size?: number | null;  // gt=0
  optimal_production_temperature?: number | null;
  optimal_humidity?: number | null;  // ge=0, le=100
  quality_check_points?: Record<string, any> | null;
  common_issues?: Record<string, any> | null;
  is_seasonal?: boolean;  // Default: false
  season_start_month?: number | null;  // ge=1, le=12
  season_end_month?: number | null;  // ge=1, le=12
  is_signature_item?: boolean;  // Default: false
  target_margin_percentage?: number | null;  // ge=0
  ingredients: RecipeIngredientCreate[];  // min_items=1
}

/**
 * Schema for updating recipes
 * Backend: RecipeUpdate in schemas/recipes.py (lines 141-178)
 */
export interface RecipeUpdate {
  name?: string | null;  // min_length=1, max_length=255
  recipe_code?: string | null;  // max_length=100
  version?: string | null;  // max_length=20
  description?: string | null;
  category?: string | null;  // max_length=100
  cuisine_type?: string | null;  // max_length=100
  difficulty_level?: number | null;  // ge=1, le=5
  yield_quantity?: number | null;  // gt=0
  yield_unit?: MeasurementUnit | null;
  prep_time_minutes?: number | null;  // ge=0
  cook_time_minutes?: number | null;  // ge=0
  total_time_minutes?: number | null;  // ge=0
  rest_time_minutes?: number | null;  // ge=0
  instructions?: Record<string, any> | null;
  preparation_notes?: string | null;
  storage_instructions?: string | null;
  quality_standards?: string | null;
  quality_check_configuration?: RecipeQualityConfigurationUpdate | null;
  serves_count?: number | null;  // ge=1
  nutritional_info?: Record<string, any> | null;
  allergen_info?: Record<string, any> | null;
  dietary_tags?: Record<string, any> | null;
  batch_size_multiplier?: number | null;  // gt=0
  minimum_batch_size?: number | null;  // gt=0
  maximum_batch_size?: number | null;  // gt=0
  optimal_production_temperature?: number | null;
  optimal_humidity?: number | null;  // ge=0, le=100
  quality_check_points?: Record<string, any> | null;
  common_issues?: Record<string, any> | null;
  status?: RecipeStatus | null;
  is_seasonal?: boolean | null;
  season_start_month?: number | null;  // ge=1, le=12
  season_end_month?: number | null;  // ge=1, le=12
  is_signature_item?: boolean | null;
  target_margin_percentage?: number | null;  // ge=0
  ingredients?: RecipeIngredientCreate[] | null;
}

/**
 * Schema for recipe responses
 * Backend: RecipeResponse in schemas/recipes.py (lines 181-232)
 */
export interface RecipeResponse {
  id: string;
  tenant_id: string;
  name: string;
  recipe_code?: string | null;
  version: string;
  finished_product_id: string;
  description?: string | null;
  category?: string | null;
  cuisine_type?: string | null;
  difficulty_level: number;
  yield_quantity: number;
  yield_unit: string;
  prep_time_minutes?: number | null;
  cook_time_minutes?: number | null;
  total_time_minutes?: number | null;
  rest_time_minutes?: number | null;
  estimated_cost_per_unit?: number | null;
  last_calculated_cost?: number | null;
  cost_calculation_date?: string | null;
  target_margin_percentage?: number | null;
  suggested_selling_price?: number | null;
  instructions?: Record<string, any> | null;
  preparation_notes?: string | null;
  storage_instructions?: string | null;
  quality_standards?: string | null;
  quality_check_configuration?: RecipeQualityConfiguration | null;
  serves_count?: number | null;
  nutritional_info?: Record<string, any> | null;
  allergen_info?: Record<string, any> | null;
  dietary_tags?: Record<string, any> | null;
  batch_size_multiplier: number;
  minimum_batch_size?: number | null;
  maximum_batch_size?: number | null;
  optimal_production_temperature?: number | null;
  optimal_humidity?: number | null;
  quality_check_points?: Record<string, any> | null;
  common_issues?: Record<string, any> | null;
  status: string;
  is_seasonal: boolean;
  season_start_month?: number | null;
  season_end_month?: number | null;
  is_signature_item: boolean;
  created_at: string;
  updated_at: string;
  created_by?: string | null;
  updated_by?: string | null;
  ingredients?: RecipeIngredientResponse[] | null;
}

// ================================================================
// SEARCH AND OPERATIONS TYPES
// ================================================================

/**
 * Schema for recipe search requests
 * Backend: RecipeSearchRequest in schemas/recipes.py (lines 235-244)
 */
export interface RecipeSearchRequest {
  search_term?: string | null;
  status?: RecipeStatus | null;
  category?: string | null;
  is_seasonal?: boolean | null;
  is_signature?: boolean | null;
  difficulty_level?: number | null;  // ge=1, le=5
  limit?: number;  // Default: 100, ge=1, le=1000
  offset?: number;  // Default: 0, ge=0
}

/**
 * Schema for recipe duplication requests
 * Backend: RecipeDuplicateRequest in schemas/recipes.py (lines 247-249)
 */
export interface RecipeDuplicateRequest {
  new_name: string;  // min_length=1, max_length=255
}

/**
 * Schema for recipe feasibility check responses
 * Backend: RecipeFeasibilityResponse in schemas/recipes.py (lines 252-259)
 */
export interface RecipeFeasibilityResponse {
  recipe_id: string;
  recipe_name: string;
  batch_multiplier: number;
  feasible: boolean;
  missing_ingredients: Array<Record<string, any>>;  // Default: []
  insufficient_ingredients: Array<Record<string, any>>;  // Default: []
}

/**
 * Schema for recipe statistics responses
 * Backend: RecipeStatisticsResponse in schemas/recipes.py (lines 262-268)
 */
export interface RecipeStatisticsResponse {
  total_recipes: number;
  active_recipes: number;
  signature_recipes: number;
  seasonal_recipes: number;
  category_breakdown: Array<Record<string, any>>;
}

/**
 * Summary of what will be deleted when hard-deleting a recipe
 * Backend: RecipeDeletionSummary in schemas/recipes.py (lines 235-246)
 */
export interface RecipeDeletionSummary {
  recipe_id: string;
  recipe_name: string;
  recipe_code: string;
  production_batches_count: number;
  recipe_ingredients_count: number;
  dependent_recipes_count: number;
  affected_orders_count: number;
  last_used_date?: string | null;
  can_delete: boolean;
  warnings: string[];  // Default: []
}

/**
 * Response for recipe categories list
 * Backend: get_recipe_categories endpoint in api/recipe_operations.py (lines 168-186)
 */
export interface RecipeCategoriesResponse {
  categories: string[];
}

/**
 * Request body for adding quality templates to a stage
 * Backend: add_quality_templates_to_stage endpoint in api/recipe_quality_configs.py (lines 103-133)
 */
export interface AddQualityTemplatesRequest {
  template_ids: string[];
}

/**
 * Generic success message response
 * Used by various operations endpoints
 */
export interface MessageResponse {
  message: string;
}
