/**
 * Production API Types
 *
 * These types mirror the backend Pydantic schemas exactly.
 * Backend schemas location: services/production/app/schemas/
 *
 * @see services/production/app/schemas/production.py - Production batch, schedule, quality schemas
 * @see services/production/app/schemas/quality_templates.py - Quality check template schemas
 * @see services/production/app/api/production_operations.py - Operations endpoints
 */

// ===== ENUMS =====
// Mirror: production.py:15-32

export enum ProductionStatus {
  PENDING = 'PENDING',
  IN_PROGRESS = 'IN_PROGRESS',
  COMPLETED = 'COMPLETED',
  CANCELLED = 'CANCELLED',
  ON_HOLD = 'ON_HOLD',
  QUALITY_CHECK = 'QUALITY_CHECK',
  FAILED = 'FAILED'
}

export enum ProductionPriority {
  LOW = 'LOW',
  MEDIUM = 'MEDIUM',
  HIGH = 'HIGH',
  URGENT = 'URGENT'
}

export enum QualityCheckType {
  VISUAL = 'visual',
  MEASUREMENT = 'measurement',
  TEMPERATURE = 'temperature',
  WEIGHT = 'weight',
  BOOLEAN = 'boolean',
  TIMING = 'timing'
}

export enum ProcessStage {
  MIXING = 'MIXING',
  PROOFING = 'PROOFING',
  SHAPING = 'SHAPING',
  BAKING = 'BAKING',
  COOLING = 'COOLING',
  PACKAGING = 'PACKAGING',
  FINISHED = 'FINISHED'
}

// Compatibility aliases
export const ProductionStatusEnum = ProductionStatus;
export const ProductionPriorityEnum = ProductionPriority;
export const ProductionBatchStatus = ProductionStatus;
export const ProductionBatchPriority = ProductionPriority;
export const QualityCheckStatus = ProductionStatus;

// ===== PRODUCTION BATCH SCHEMAS =====
// Mirror: ProductionBatchCreate from production.py:61

export interface ProductionBatchCreate {
  product_id: string;
  product_name: string;
  recipe_id?: string | null;
  planned_start_time: string;
  planned_end_time: string;
  planned_quantity: number;  // gt=0
  planned_duration_minutes: number;  // gt=0
  priority?: ProductionPriority;  // Default: MEDIUM
  is_rush_order?: boolean;  // Default: false
  is_special_recipe?: boolean;  // Default: false
  production_notes?: string | null;

  // Additional fields
  batch_number?: string | null;
  order_id?: string | null;
  forecast_id?: string | null;
  equipment_used?: string[] | null;
  staff_assigned?: string[] | null;
  station_id?: string | null;
}

// Mirror: ProductionBatchUpdate from production.py:71
export interface ProductionBatchUpdate {
  product_name?: string | null;
  planned_start_time?: string | null;
  planned_end_time?: string | null;
  planned_quantity?: number | null;  // gt=0
  planned_duration_minutes?: number | null;  // gt=0
  actual_quantity?: number | null;  // ge=0
  priority?: ProductionPriority | null;
  equipment_used?: string[] | null;
  staff_assigned?: string[] | null;
  station_id?: string | null;
  production_notes?: string | null;
}

// Mirror: ProductionBatchStatusUpdate from production.py:86
export interface ProductionBatchStatusUpdate {
  status: ProductionStatus;
  actual_quantity?: number | null;  // ge=0
  notes?: string | null;
}

// Mirror: ProductionBatchResponse from production.py:93
export interface ProductionBatchResponse {
  id: string;
  tenant_id: string;
  batch_number: string;
  product_id: string;
  product_name: string;
  recipe_id: string | null;
  planned_start_time: string;
  planned_end_time: string;
  planned_quantity: number;
  planned_duration_minutes: number;
  actual_start_time: string | null;
  actual_end_time: string | null;
  actual_quantity: number | null;
  actual_duration_minutes: number | null;
  status: ProductionStatus;
  priority: ProductionPriority;

  // Process stage tracking (replaces frontend mock data)
  current_process_stage?: string | null;
  process_stage_history?: Array<Record<string, any>> | null;
  pending_quality_checks?: Array<Record<string, any>> | null;
  completed_quality_checks?: Array<Record<string, any>> | null;

  estimated_cost: number | null;
  actual_cost: number | null;
  yield_percentage: number | null;
  quality_score: number | null;
  equipment_used: string[] | null;
  staff_assigned: string[] | null;
  station_id: string | null;
  order_id: string | null;
  forecast_id: string | null;
  is_rush_order: boolean;
  is_special_recipe: boolean;
  production_notes: string | null;
  quality_notes: string | null;
  delay_reason: string | null;
  cancellation_reason: string | null;
  reasoning_data?: Record<string, any> | null;
  created_at: string;
  updated_at: string;
  completed_at: string | null;
}

// ===== PRODUCTION SCHEDULE SCHEMAS =====
// Mirror: ProductionScheduleCreate from production.py:163

export interface ProductionScheduleCreate {
  schedule_date: string;  // date format
  shift_start: string;  // datetime
  shift_end: string;  // datetime
  total_capacity_hours: number;  // gt=0
  planned_capacity_hours: number;  // gt=0
  staff_count: number;  // gt=0
  equipment_capacity?: Record<string, any> | null;
  station_assignments?: Record<string, any> | null;
  schedule_notes?: string | null;
}

// Mirror: ProductionScheduleUpdate from production.py:168
export interface ProductionScheduleUpdate {
  shift_start?: string | null;
  shift_end?: string | null;
  total_capacity_hours?: number | null;  // gt=0
  planned_capacity_hours?: number | null;  // gt=0
  staff_count?: number | null;  // gt=0
  overtime_hours?: number | null;  // ge=0
  equipment_capacity?: Record<string, any> | null;
  station_assignments?: Record<string, any> | null;
  schedule_notes?: string | null;
}

// Mirror: ProductionScheduleResponse from production.py:181
export interface ProductionScheduleResponse {
  id: string;
  tenant_id: string;
  schedule_date: string;  // date format
  shift_start: string;
  shift_end: string;
  total_capacity_hours: number;
  planned_capacity_hours: number;
  actual_capacity_hours: number | null;
  overtime_hours: number | null;
  staff_count: number;
  equipment_capacity: Record<string, any> | null;
  station_assignments: Record<string, any> | null;
  total_batches_planned: number;
  total_batches_completed: number | null;
  total_quantity_planned: number;
  total_quantity_produced: number | null;
  is_finalized: boolean;
  is_active: boolean;
  efficiency_percentage: number | null;
  utilization_percentage: number | null;
  on_time_completion_rate: number | null;
  schedule_notes: string | null;
  schedule_adjustments: Record<string, any> | null;
  created_at: string;
  updated_at: string;
  finalized_at: string | null;
}

// ===== QUALITY CHECK SCHEMAS =====
// Mirror: QualityCheckCreate from production.py:230

export interface QualityCheckCreate {
  batch_id: string;
  check_type: string;  // min_length=1, max_length=50
  check_time: string;
  quality_score: number;  // ge=1, le=10
  pass_fail: boolean;
  defect_count?: number;  // Default: 0, ge=0
  defect_types?: string[] | null;
  check_notes?: string | null;

  // Measurement fields
  checker_id?: string | null;
  measured_weight?: number | null;  // gt=0
  measured_temperature?: number | null;
  measured_moisture?: number | null;  // ge=0, le=100
  measured_dimensions?: Record<string, number> | null;
  target_weight?: number | null;  // gt=0
  target_temperature?: number | null;
  target_moisture?: number | null;  // ge=0, le=100
  tolerance_percentage?: number | null;  // ge=0, le=100
  corrective_actions?: string[] | null;
}

// Mirror: QualityCheckResponse from production.py:244
export interface QualityCheckResponse {
  id: string;
  tenant_id: string;
  batch_id: string;
  check_type: string;
  check_time: string;
  checker_id: string | null;
  quality_score: number;
  pass_fail: boolean;
  defect_count: number;
  defect_types: string[] | null;
  measured_weight: number | null;
  measured_temperature: number | null;
  measured_moisture: number | null;
  measured_dimensions: Record<string, number> | null;
  target_weight: number | null;
  target_temperature: number | null;
  target_moisture: number | null;
  tolerance_percentage: number | null;
  within_tolerance: boolean | null;
  corrective_action_needed: boolean;
  corrective_actions: string[] | null;
  check_notes: string | null;
  photos_urls: string[] | null;
  certificate_url: string | null;
  created_at: string;
  updated_at: string;
}

// ===== QUALITY CHECK TEMPLATE SCHEMAS =====
// Mirror: quality_templates.py:25

export interface QualityCheckTemplateCreate {
  name: string;  // min_length=1, max_length=255
  template_code?: string | null;  // max_length=100
  check_type: QualityCheckType;
  category?: string | null;  // max_length=100
  description?: string | null;
  instructions?: string | null;

  // Configuration
  parameters?: Record<string, any> | null;
  thresholds?: Record<string, any> | null;
  scoring_criteria?: Record<string, any> | null;

  // Settings
  is_active?: boolean;  // Default: true
  is_required?: boolean;  // Default: false
  is_critical?: boolean;  // Default: false
  weight?: number;  // ge=0.0, le=10.0, Default: 1.0

  // Measurement specifications
  min_value?: number | null;
  max_value?: number | null;
  target_value?: number | null;
  unit?: string | null;  // max_length=20
  tolerance_percentage?: number | null;  // ge=0.0, le=100.0

  // Process stage applicability
  applicable_stages?: ProcessStage[] | null;

  // Required field
  created_by: string;
}

// Mirror: quality_templates.py:76
export interface QualityCheckTemplateUpdate {
  name?: string | null;
  template_code?: string | null;
  check_type?: QualityCheckType | null;
  category?: string | null;
  description?: string | null;
  instructions?: string | null;
  parameters?: Record<string, any> | null;
  thresholds?: Record<string, any> | null;
  scoring_criteria?: Record<string, any> | null;
  is_active?: boolean | null;
  is_required?: boolean | null;
  is_critical?: boolean | null;
  weight?: number | null;  // ge=0.0, le=10.0
  min_value?: number | null;
  max_value?: number | null;
  target_value?: number | null;
  unit?: string | null;
  tolerance_percentage?: number | null;
  applicable_stages?: ProcessStage[] | null;
}

// Mirror: quality_templates.py:99
export interface QualityCheckTemplateResponse {
  id: string;
  tenant_id: string;
  name: string;
  template_code: string | null;
  check_type: QualityCheckType;
  category: string | null;
  description: string | null;
  instructions: string | null;
  parameters: Record<string, any> | null;
  thresholds: Record<string, any> | null;
  scoring_criteria: Record<string, any> | null;
  is_active: boolean;
  is_required: boolean;
  is_critical: boolean;
  weight: number;
  min_value: number | null;
  max_value: number | null;
  target_value: number | null;
  unit: string | null;
  tolerance_percentage: number | null;
  applicable_stages: ProcessStage[] | null;
  created_by: string;
  created_at: string;
  updated_at: string;
}

// Mirror: quality_templates.py:119
export interface QualityCheckCriterion {
  id: string;
  name: string;
  description: string;
  check_type: QualityCheckType;
  required?: boolean;  // Default: true
  weight?: number;  // ge=0.0, le=10.0, Default: 1.0
  acceptable_criteria: string;
  min_value?: number | null;
  max_value?: number | null;
  unit?: string | null;
  is_critical?: boolean;  // Default: false
}

// Mirror: quality_templates.py:134
export interface QualityCheckResult {
  criterion_id: string;
  value: number | string | boolean;
  score: number;  // ge=0.0, le=10.0
  notes?: string | null;
  photos?: string[] | null;
  pass_check: boolean;
  timestamp: string;
}

// Mirror: quality_templates.py:145
export interface QualityCheckExecutionRequest {
  template_id: string;
  batch_id: string;
  process_stage: ProcessStage;
  checker_id?: string | null;
  results: QualityCheckResult[];
  final_notes?: string | null;
  photos?: string[] | null;
}

// Mirror: quality_templates.py:156
export interface QualityCheckExecutionResponse {
  check_id: string;
  overall_score: number;  // ge=0.0, le=10.0
  overall_pass: boolean;
  critical_failures: string[];
  corrective_actions: string[];
  timestamp: string;
}

// ===== DASHBOARD AND ANALYTICS SCHEMAS =====
// Mirror: production.py:283

export interface ProductionDashboardSummary {
  active_batches: number;
  todays_production_plan: Array<Record<string, any>>;
  capacity_utilization: number;
  on_time_completion_rate: number;
  average_quality_score: number;
  total_output_today: number;
  efficiency_percentage: number;
}

// Mirror: production.py:294
export interface DailyProductionRequirements {
  date: string;  // date format
  production_plan: Array<Record<string, any>>;
  total_capacity_needed: number;
  available_capacity: number;
  capacity_gap: number;
  urgent_items: number;
  recommended_schedule: Record<string, any> | null;
}

// Mirror: production.py:305
export interface ProductionMetrics {
  period_start: string;  // date format
  period_end: string;  // date format
  total_batches: number;
  completed_batches: number;
  completion_rate: number;
  average_yield_percentage: number;
  on_time_completion_rate: number;
  total_production_cost: number;
  average_quality_score: number;
  efficiency_trends: Array<Record<string, any>>;
}

// ===== LIST RESPONSE WRAPPERS =====
// Mirror: production.py:323

export interface ProductionBatchListResponse {
  batches: ProductionBatchResponse[];
  total_count: number;
  page: number;
  page_size: number;
}

// Mirror: production.py:331
export interface ProductionScheduleListResponse {
  schedules: ProductionScheduleResponse[];
  total_count: number;
  page: number;
  page_size: number;
}

// Mirror: production.py:339
export interface QualityCheckListResponse {
  quality_checks: QualityCheckResponse[];
  total_count: number;
  page: number;
  page_size: number;
}

// Mirror: quality_templates.py:111
export interface QualityCheckTemplateList {
  templates: QualityCheckTemplateResponse[];
  total: number;
  skip: number;
  limit: number;
}

// ===== FILTER TYPES =====

export interface ProductionBatchFilters {
  status?: ProductionStatus | null;
  product_id?: string | null;
  order_id?: string | null;
  start_date?: string | null;
  end_date?: string | null;
  page?: number;
  page_size?: number;
}

export interface ProductionScheduleFilters {
  start_date?: string | null;
  end_date?: string | null;
  is_finalized?: boolean | null;
  page?: number;
  page_size?: number;
}

export interface QualityCheckFilters {
  batch_id?: string | null;
  product_id?: string | null;
  start_date?: string | null;
  end_date?: string | null;
  pass_fail?: boolean | null;
  page?: number;
  page_size?: number;
}

// ===== OPERATIONS TYPES =====
// From production_operations.py

export interface BatchStatistics {
  total_batches: number;
  completed_batches: number;
  failed_batches: number;
  cancelled_batches: number;
  completion_rate: number;
  average_yield: number;
  on_time_rate: number;
  period_start: string;
  period_end: string;
}

export interface CapacityBottlenecks {
  bottlenecks: Array<{
    date: string;
    time_slot: string;
    resource_name: string;
    predicted_utilization: number;
    severity: 'low' | 'medium' | 'high';
    suggestion: string;
  }>;
}

// ===== ANALYTICS TYPES =====
// From analytics.py endpoints

export interface ProductionPerformanceAnalytics {
  completion_rate: number;
  waste_percentage: number;
  labor_cost_per_unit: number;
  on_time_completion_rate: number;
  average_yield_percentage: number;
  total_output: number;
  efficiency_percentage: number;
  period_start: string;
  period_end: string;
}

export interface YieldTrendsAnalytics {
  trends: Array<{
    date: string;
    product_name: string;
    yield_percentage: number;
    quantity_produced: number;
  }>;
  period: 'week' | 'month';
}

export interface TopDefectsAnalytics {
  defects: Array<{
    defect_type: string;
    count: number;
    percentage: number;
  }>;
}

export interface EquipmentEfficiencyAnalytics {
  equipment: Array<{
    resource_name: string;
    efficiency_rating: number;
    uptime_percentage: number;
    downtime_hours: number;
    total_batches: number;
  }>;
}

// ===== ADDITIONAL HELPER TYPES =====

export interface ProcessStageQualityConfig {
  stage: ProcessStage;
  template_ids: string[];
  custom_parameters?: Record<string, any> | null;
  is_required?: boolean;  // Default: true
  blocking?: boolean;  // Default: true
}

export interface RecipeQualityConfiguration {
  stages: Record<string, ProcessStageQualityConfig>;
  global_parameters?: Record<string, any> | null;
  default_templates?: string[] | null;
}

export interface ProductionCapacityStatus {
  date: string;
  total_capacity: number;
  utilized_capacity: number;
  utilization_percentage: number;
  equipment_utilization: Array<{
    equipment_id: string;
    equipment_name: string;
    capacity: number;
    utilization: number;
    status: 'operational' | 'maintenance' | 'down';
  }>;
}

export interface ProductionYieldMetrics {
  start_date: string;
  end_date: string;
  overall_yield: number;
  products: Array<{
    product_id: string;
    product_name: string;
    average_yield: number;
    best_yield: number;
    worst_yield: number;
    batch_count: number;
  }>;
}
