/**
 * Inventory API Types - Mirror backend schemas
 */

// Enums - Mirror backend enum definitions
export enum ProductType {
  INGREDIENT = 'ingredient',
  FINISHED_PRODUCT = 'finished_product'
}

export enum ProductionStage {
  RAW_INGREDIENT = 'raw_ingredient',
  PAR_BAKED = 'par_baked',
  FULLY_BAKED = 'fully_baked',
  PREPARED_DOUGH = 'prepared_dough',
  FROZEN_PRODUCT = 'frozen_product'
}

export enum UnitOfMeasure {
  KILOGRAMS = 'kg',
  GRAMS = 'g',
  LITERS = 'l',
  MILLILITERS = 'ml',
  UNITS = 'units',
  PIECES = 'pcs',
  PACKAGES = 'pkg',
  BAGS = 'bags',
  BOXES = 'boxes'
}

export enum IngredientCategory {
  FLOUR = 'flour',
  YEAST = 'yeast',
  DAIRY = 'dairy',
  EGGS = 'eggs',
  SUGAR = 'sugar',
  FATS = 'fats',
  SALT = 'salt',
  SPICES = 'spices',
  ADDITIVES = 'additives',
  PACKAGING = 'packaging',
  CLEANING = 'cleaning',
  OTHER = 'other'
}

export enum ProductCategory {
  BREAD = 'bread',
  CROISSANTS = 'croissants',
  PASTRIES = 'pastries',
  CAKES = 'cakes',
  COOKIES = 'cookies',
  MUFFINS = 'muffins',
  SANDWICHES = 'sandwiches',
  SEASONAL = 'seasonal',
  BEVERAGES = 'beverages',
  OTHER_PRODUCTS = 'other_products'
}

export enum StockMovementType {
  PURCHASE = 'PURCHASE',
  PRODUCTION_USE = 'PRODUCTION_USE',
  ADJUSTMENT = 'ADJUSTMENT',
  WASTE = 'WASTE',
  TRANSFER = 'TRANSFER',
  RETURN = 'RETURN',
  INITIAL_STOCK = 'INITIAL_STOCK',
  TRANSFORMATION = 'TRANSFORMATION'
}


// Base Inventory Types
export interface IngredientCreate {
  name: string;
  description?: string;
  category?: string;
  unit_of_measure: string;
  low_stock_threshold: number;
  max_stock_level?: number;
  reorder_point: number;
  shelf_life_days?: number;  // Default shelf life only
  is_seasonal?: boolean;
  average_cost?: number;
  notes?: string;
}

export interface IngredientUpdate {
  name?: string;
  description?: string;
  category?: string;
  subcategory?: string;
  brand?: string;
  unit_of_measure?: string;
  package_size?: number;
  average_cost?: number;
  last_purchase_price?: number;
  standard_cost?: number;
  low_stock_threshold?: number;
  reorder_point?: number;
  reorder_quantity?: number;
  max_stock_level?: number;
  shelf_life_days?: number;  // Default shelf life only
  is_active?: boolean;
  is_perishable?: boolean;
  is_seasonal?: boolean;
  allergen_info?: any;
  notes?: string;
}

export interface IngredientResponse {
  id: string;
  tenant_id: string;
  name: string;
  description?: string;
  product_type: ProductType;
  category: string;
  subcategory?: string;
  brand?: string;
  unit_of_measure: string;
  package_size?: number;
  average_cost?: number;
  last_purchase_price?: number;
  standard_cost?: number;
  low_stock_threshold: number;
  reorder_point: number;
  reorder_quantity: number;
  max_stock_level?: number;
  shelf_life_days?: number;  // Default shelf life only
  is_active: boolean;
  is_perishable: boolean;
  is_seasonal?: boolean;
  allergen_info?: any;
  created_at: string;
  updated_at: string;
  created_by?: string;

  // Computed fields
  current_stock?: number;
  is_low_stock?: boolean;
  needs_reorder?: boolean;
  stock_status?: 'in_stock' | 'low_stock' | 'out_of_stock' | 'overstock';
  last_restocked?: string;
  supplier_id?: string;
  notes?: string;
}

// Stock Management Types
export interface StockCreate {
  ingredient_id: string;
  supplier_id?: string;
  batch_number?: string;
  lot_number?: string;
  supplier_batch_ref?: string;

  // Production stage tracking
  production_stage?: ProductionStage;
  transformation_reference?: string;

  current_quantity: number;
  received_date?: string;
  expiration_date?: string;
  best_before_date?: string;

  // Stage-specific expiration fields
  original_expiration_date?: string;
  transformation_date?: string;
  final_expiration_date?: string;

  unit_cost?: number;
  storage_location?: string;
  warehouse_zone?: string;
  shelf_position?: string;

  quality_status?: string;

  // Batch-specific storage requirements
  requires_refrigeration?: boolean;
  requires_freezing?: boolean;
  storage_temperature_min?: number;
  storage_temperature_max?: number;
  storage_humidity_max?: number;
  shelf_life_days?: number;
  storage_instructions?: string;

  // Optional supplier reference
  supplier_id?: string;
}

export interface StockUpdate {
  supplier_id?: string;
  batch_number?: string;
  lot_number?: string;
  supplier_batch_ref?: string;

  // Production stage tracking
  production_stage?: ProductionStage;
  transformation_reference?: string;

  current_quantity?: number;
  reserved_quantity?: number;
  received_date?: string;
  expiration_date?: string;
  best_before_date?: string;

  // Stage-specific expiration fields
  original_expiration_date?: string;
  transformation_date?: string;
  final_expiration_date?: string;

  unit_cost?: number;
  storage_location?: string;
  warehouse_zone?: string;
  shelf_position?: string;

  quality_status?: string;
  notes?: string;
  is_available?: boolean;

  // Batch-specific storage requirements
  requires_refrigeration?: boolean;
  requires_freezing?: boolean;
  storage_temperature_min?: number;
  storage_temperature_max?: number;
  storage_humidity_max?: number;
  shelf_life_days?: number;
  storage_instructions?: string;
}

export interface StockResponse {
  id: string;
  ingredient_id: string;
  tenant_id: string;

  // Production stage tracking
  production_stage: ProductionStage;
  transformation_reference?: string;

  // API returns current_quantity, keeping quantity for backward compatibility
  quantity?: number;
  current_quantity: number;
  available_quantity: number;
  reserved_quantity: number;

  // API returns unit_cost, keeping unit_price for backward compatibility
  unit_price?: number;
  unit_cost: number;

  // API returns total_cost, keeping total_value for backward compatibility
  total_value?: number;
  total_cost: number;
  expiration_date?: string;
  batch_number?: string;
  supplier_id?: string;
  purchase_order_reference?: string;
  storage_location?: string;

  // Stage-specific expiration fields
  original_expiration_date?: string;
  transformation_date?: string;
  final_expiration_date?: string;

  notes?: string;
  is_available: boolean;
  is_expired: boolean;
  days_until_expiry?: number;

  // Batch-specific storage requirements
  requires_refrigeration: boolean;
  requires_freezing: boolean;
  storage_temperature_min?: number;
  storage_temperature_max?: number;
  storage_humidity_max?: number;
  shelf_life_days?: number;
  storage_instructions?: string;

  created_at: string;
  updated_at: string;
  created_by?: string;
}

export interface StockMovementCreate {
  ingredient_id: string;
  stock_id?: string;
  movement_type: 'purchase' | 'production_use' | 'adjustment' | 'waste' | 'transfer' | 'return' | 'initial_stock' | 'transformation';
  quantity: number;
  unit_cost?: number;
  reference_number?: string;
  supplier_id?: string;
  notes?: string;
  reason_code?: string;
  movement_date?: string;
}

export interface StockMovementResponse {
  id: string;
  tenant_id: string;
  ingredient_id: string;
  stock_id?: string;
  movement_type: 'purchase' | 'production_use' | 'adjustment' | 'waste' | 'transfer' | 'return' | 'initial_stock' | 'transformation';
  quantity: number;
  unit_cost?: number;
  total_cost?: number;
  quantity_before?: number;
  quantity_after?: number;
  reference_number?: string;
  supplier_id?: string;
  notes?: string;
  reason_code?: string;
  movement_date: string;
  created_at: string;
  created_by?: string;
  ingredient?: IngredientResponse;
}

// Product Transformation Types
export interface ProductTransformationCreate {
  source_ingredient_id: string;
  target_ingredient_id: string;
  source_stage: ProductionStage;
  target_stage: ProductionStage;
  source_quantity: number;
  target_quantity: number;
  conversion_ratio?: number;
  expiration_calculation_method?: string;
  expiration_days_offset?: number;
  process_notes?: string;
  target_batch_number?: string;
  source_stock_ids?: string[];
}

export interface ProductTransformationResponse {
  id: string;
  tenant_id: string;
  transformation_reference: string;
  source_ingredient_id: string;
  target_ingredient_id: string;
  source_stage: ProductionStage;
  target_stage: ProductionStage;
  source_quantity: number;
  target_quantity: number;
  conversion_ratio: number;
  expiration_calculation_method: string;
  expiration_days_offset?: number;
  transformation_date: string;
  process_notes?: string;
  performed_by?: string;
  source_batch_numbers?: string;
  target_batch_number?: string;
  is_completed: boolean;
  is_reversed: boolean;
  created_at: string;
  created_by?: string;
  source_ingredient?: IngredientResponse;
  target_ingredient?: IngredientResponse;
}

// Filter and Query Types
export interface InventoryFilter {
  category?: string;
  stock_status?: 'in_stock' | 'low_stock' | 'out_of_stock' | 'overstock';
  requires_refrigeration?: boolean;
  requires_freezing?: boolean;
  is_seasonal?: boolean;
  supplier_id?: string;
  expiring_within_days?: number;
  search?: string;
  limit?: number;
  offset?: number;
  order_by?: string;
  order_direction?: 'asc' | 'desc';
}

export interface StockFilter {
  ingredient_id?: string;
  production_stage?: ProductionStage;
  transformation_reference?: string;
  is_available?: boolean;
  is_expired?: boolean;
  expiring_within_days?: number;
  batch_number?: string;
  supplier_id?: string;
  limit?: number;
  offset?: number;
  order_by?: string;
  order_direction?: 'asc' | 'desc';
}

// Stock Consumption Types
export interface StockConsumptionRequest {
  ingredient_id: string;
  quantity: number;
  reference_number?: string;
  notes?: string;
  fifo?: boolean;
}

export interface StockConsumptionResponse {
  ingredient_id: string;
  total_quantity_consumed: number;
  consumed_items: Array<{
    stock_id: string;
    quantity_consumed: number;
    batch_number?: string;
    expiration_date?: string;
  }>;
  method: 'FIFO' | 'LIFO';
}

// Pagination Response
export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  page: number;
  per_page: number;
  total_pages: number;
}

// Deletion Summary Response
export interface DeletionSummary {
  ingredient_id: string;
  ingredient_name: string | null;
  deleted_stock_entries: number;
  deleted_stock_movements: number;
  deleted_stock_alerts: number;
  success: boolean;
}

// Select option interface for enum helpers
export interface EnumOption {
  value: string | number;
  label: string;
  disabled?: boolean;
  description?: string;
}