/**
 * TypeScript types for Recipes service
 * Generated based on backend schemas in services/recipes/app/schemas/recipes.py
 */

import { ProductionPriorityEnum } from './production';

export enum RecipeStatus {
  DRAFT = 'draft',
  ACTIVE = 'active',
  TESTING = 'testing',
  ARCHIVED = 'archived',
  DISCONTINUED = 'discontinued'
}

export enum MeasurementUnit {
  GRAMS = 'g',
  KILOGRAMS = 'kg',
  MILLILITERS = 'ml',
  LITERS = 'l',
  CUPS = 'cups',
  TABLESPOONS = 'tbsp',
  TEASPOONS = 'tsp',
  UNITS = 'units',
  PIECES = 'pieces',
  PERCENTAGE = '%'
}

export enum ProductionStatus {
  PLANNED = 'planned',
  IN_PROGRESS = 'in_progress',
  COMPLETED = 'completed',
  FAILED = 'failed',
  CANCELLED = 'cancelled'
}


export interface RecipeIngredientCreate {
  ingredient_id: string;
  quantity: number;
  unit: MeasurementUnit;
  alternative_quantity?: number | null;
  alternative_unit?: MeasurementUnit | 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;
}

export interface RecipeIngredientUpdate {
  ingredient_id?: string | null;
  quantity?: number | null;
  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;
  ingredient_group?: string | null;
  substitution_options?: Record<string, any> | null;
  substitution_ratio?: number | null;
}

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;
}

export interface RecipeCreate {
  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: MeasurementUnit;
  prep_time_minutes?: number | null;
  cook_time_minutes?: number | null;
  total_time_minutes?: number | null;
  rest_time_minutes?: number | null;
  instructions?: Record<string, any> | null;
  preparation_notes?: string | null;
  storage_instructions?: string | null;
  quality_standards?: string | 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;
  is_seasonal?: boolean;
  season_start_month?: number | null;
  season_end_month?: number | null;
  is_signature_item?: boolean;
  target_margin_percentage?: number | null;
  ingredients: RecipeIngredientCreate[];
}

export interface RecipeUpdate {
  name?: string | null;
  recipe_code?: string | null;
  version?: string | null;
  description?: string | null;
  category?: string | null;
  cuisine_type?: string | null;
  difficulty_level?: number | null;
  yield_quantity?: number | null;
  yield_unit?: MeasurementUnit | null;
  prep_time_minutes?: number | null;
  cook_time_minutes?: number | null;
  total_time_minutes?: number | null;
  rest_time_minutes?: number | null;
  instructions?: Record<string, any> | null;
  preparation_notes?: string | null;
  storage_instructions?: string | null;
  quality_standards?: string | 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 | null;
  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?: RecipeStatus | null;
  is_seasonal?: boolean | null;
  season_start_month?: number | null;
  season_end_month?: number | null;
  is_signature_item?: boolean | null;
  target_margin_percentage?: number | null;
  ingredients?: RecipeIngredientCreate[] | null;
}

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;
  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;
}

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;
  limit?: number;
  offset?: number;
}

export interface RecipeSearchParams {
  search_term?: string;
  status?: string;
  category?: string;
  is_seasonal?: boolean;
  is_signature?: boolean;
  difficulty_level?: number;
  limit?: number;
  offset?: number;
}

export interface RecipeDuplicateRequest {
  new_name: string;
}

export interface RecipeFeasibilityResponse {
  recipe_id: string;
  recipe_name: string;
  batch_multiplier: number;
  feasible: boolean;
  missing_ingredients: Array<Record<string, any>>;
  insufficient_ingredients: Array<Record<string, any>>;
}

export interface RecipeStatisticsResponse {
  total_recipes: number;
  active_recipes: number;
  signature_recipes: number;
  seasonal_recipes: number;
  category_breakdown: Array<Record<string, any>>;
}

export interface RecipeCategoriesResponse {
  categories: string[];
}

// Production Batch Types
export interface ProductionBatchCreate {
  recipe_id: string;
  batch_number: string;
  production_date: string;
  planned_start_time?: string | null;
  planned_end_time?: string | null;
  planned_quantity: number;
  batch_size_multiplier?: number;
  priority?: ProductionPriorityEnum;
  assigned_staff?: Array<Record<string, any>> | null;
  production_notes?: string | null;
  customer_order_reference?: string | null;
  pre_order_quantity?: number | null;
  shelf_quantity?: number | null;
}

export interface ProductionBatchUpdate {
  batch_number?: string | null;
  production_date?: string | null;
  planned_start_time?: string | null;
  actual_start_time?: string | null;
  planned_end_time?: string | null;
  actual_end_time?: string | null;
  planned_quantity?: number | null;
  actual_quantity?: number | null;
  batch_size_multiplier?: number | null;
  status?: ProductionStatus | null;
  priority?: ProductionPriorityEnum | null;
  assigned_staff?: Array<Record<string, any>> | null;
  production_notes?: string | null;
  quality_score?: number | null;
  quality_notes?: string | null;
  defect_rate?: number | null;
  rework_required?: boolean | null;
  production_temperature?: number | null;
  production_humidity?: number | null;
  oven_temperature?: number | null;
  baking_time_minutes?: number | null;
  waste_quantity?: number | null;
  waste_reason?: string | null;
  customer_order_reference?: string | null;
  pre_order_quantity?: number | null;
  shelf_quantity?: number | null;
}

export interface ProductionBatchResponse {
  id: string;
  tenant_id: string;
  recipe_id: string;
  batch_number: string;
  production_date: string;
  planned_start_time?: string | null;
  actual_start_time?: string | null;
  planned_end_time?: string | null;
  actual_end_time?: string | null;
  planned_quantity: number;
  actual_quantity?: number | null;
  yield_percentage?: number | null;
  batch_size_multiplier: number;
  status: string;
  priority: string;
  assigned_staff?: Array<Record<string, any>> | null;
  production_notes?: string | null;
  quality_score?: number | null;
  quality_notes?: string | null;
  defect_rate?: number | null;
  rework_required: boolean;
  planned_material_cost?: number | null;
  actual_material_cost?: number | null;
  labor_cost?: number | null;
  overhead_cost?: number | null;
  total_production_cost?: number | null;
  cost_per_unit?: number | null;
  production_temperature?: number | null;
  production_humidity?: number | null;
  oven_temperature?: number | null;
  baking_time_minutes?: number | null;
  waste_quantity: number;
  waste_reason?: string | null;
  efficiency_percentage?: number | null;
  customer_order_reference?: string | null;
  pre_order_quantity?: number | null;
  shelf_quantity?: number | null;
  created_at: string;
  updated_at: string;
  created_by?: string | null;
  completed_by?: string | null;
}

// Error types
export interface ApiErrorDetail {
  message: string;
  status?: number;
  code?: string;
  details?: any;
}

// Common query parameters for list endpoints
export interface PaginationParams {
  limit?: number;
  offset?: number;
}

export interface DateRangeParams {
  start_date?: string;
  end_date?: string;
}

// Utility types for better type inference
export type RecipeFormData = Omit<RecipeCreate, 'ingredients'> & {
  ingredients: Array<Omit<RecipeIngredientCreate, 'ingredient_order'> & { ingredient_order?: number }>;
};

export type RecipeUpdateFormData = Omit<RecipeUpdate, 'ingredients'> & {
  ingredients?: Array<Omit<RecipeIngredientCreate, 'ingredient_order'> & { ingredient_order?: number }>;
};