/**
 * Food Safety API Types - Mirror backend schemas
 */

// Food Safety Enums
export enum FoodSafetyStandard {
  HACCP = 'haccp',
  FDA = 'fda',
  USDA = 'usda',
  FSMA = 'fsma',
  SQF = 'sqf',
  BRC = 'brc',
  IFS = 'ifs',
  ISO22000 = 'iso22000',
  ORGANIC = 'organic',
  NON_GMO = 'non_gmo',
  ALLERGEN_FREE = 'allergen_free',
  KOSHER = 'kosher',
  HALAL = 'halal'
}

export enum ComplianceStatus {
  COMPLIANT = 'compliant',
  NON_COMPLIANT = 'non_compliant',
  PENDING_REVIEW = 'pending_review',
  EXPIRED = 'expired',
  WARNING = 'warning'
}

export enum FoodSafetyAlertType {
  TEMPERATURE_VIOLATION = 'temperature_violation',
  EXPIRATION_WARNING = 'expiration_warning',
  EXPIRED_PRODUCT = 'expired_product',
  CONTAMINATION_RISK = 'contamination_risk',
  ALLERGEN_CROSS_CONTAMINATION = 'allergen_cross_contamination',
  STORAGE_VIOLATION = 'storage_violation',
  QUALITY_DEGRADATION = 'quality_degradation',
  RECALL_NOTICE = 'recall_notice',
  CERTIFICATION_EXPIRY = 'certification_expiry',
  SUPPLIER_COMPLIANCE_ISSUE = 'supplier_compliance_issue'
}

export interface FoodSafetyComplianceCreate {
  ingredient_id: string;
  compliance_type: 'temperature_check' | 'quality_inspection' | 'hygiene_audit' | 'batch_verification';
  status: 'pass' | 'fail' | 'warning';
  temperature?: number;
  humidity?: number;
  ph_level?: number;
  visual_inspection_notes?: string;
  corrective_actions?: string;
  inspector_name?: string;
  certification_reference?: string;
  notes?: string;
}

export interface FoodSafetyComplianceUpdate {
  compliance_type?: 'temperature_check' | 'quality_inspection' | 'hygiene_audit' | 'batch_verification';
  status?: 'pass' | 'fail' | 'warning';
  temperature?: number;
  humidity?: number;
  ph_level?: number;
  visual_inspection_notes?: string;
  corrective_actions?: string;
  inspector_name?: string;
  certification_reference?: string;
  notes?: string;
  resolved?: boolean;
}

export interface FoodSafetyComplianceResponse {
  id: string;
  tenant_id: string;
  ingredient_id: string;
  ingredient_name: string;
  compliance_type: 'temperature_check' | 'quality_inspection' | 'hygiene_audit' | 'batch_verification';
  status: 'pass' | 'fail' | 'warning';
  temperature?: number;
  humidity?: number;
  ph_level?: number;
  visual_inspection_notes?: string;
  corrective_actions?: string;
  inspector_name?: string;
  certification_reference?: string;
  notes?: string;
  resolved: boolean;
  created_at: string;
  updated_at: string;
  created_by?: string;
}

export interface TemperatureLogCreate {
  location: string;
  temperature: number;
  humidity?: number;
  equipment_id?: string;
  notes?: string;
}

export interface BulkTemperatureLogCreate {
  logs: TemperatureLogCreate[];
}

export interface TemperatureLogResponse {
  id: string;
  tenant_id: string;
  location: string;
  temperature: number;
  humidity?: number;
  equipment_id?: string;
  notes?: string;
  is_within_range: boolean;
  alert_triggered: boolean;
  created_at: string;
  created_by?: string;
}

export interface FoodSafetyAlertCreate {
  alert_type: 'temperature_violation' | 'expiry_warning' | 'quality_issue' | 'compliance_failure';
  severity: 'critical' | 'warning' | 'info';
  title: string;
  description: string;
  ingredient_id?: string;
  temperature_log_id?: string;
  compliance_record_id?: string;
  requires_action: boolean;
  assigned_to?: string;
}

export interface FoodSafetyAlertUpdate {
  status?: 'open' | 'in_progress' | 'resolved' | 'dismissed';
  assigned_to?: string;
  resolution_notes?: string;
  corrective_actions?: string;
}

export interface FoodSafetyAlertResponse {
  id: string;
  tenant_id: string;
  alert_type: 'temperature_violation' | 'expiry_warning' | 'quality_issue' | 'compliance_failure';
  severity: 'critical' | 'warning' | 'info';
  title: string;
  description: string;
  status: 'open' | 'in_progress' | 'resolved' | 'dismissed';
  ingredient_id?: string;
  ingredient_name?: string;
  temperature_log_id?: string;
  compliance_record_id?: string;
  requires_action: boolean;
  assigned_to?: string;
  assigned_to_name?: string;
  resolution_notes?: string;
  corrective_actions?: string;
  created_at: string;
  updated_at: string;
  resolved_at?: string;
  created_by?: string;
}

export interface FoodSafetyFilter {
  compliance_type?: 'temperature_check' | 'quality_inspection' | 'hygiene_audit' | 'batch_verification';
  status?: 'pass' | 'fail' | 'warning';
  ingredient_id?: string;
  resolved?: boolean;
  date_range?: {
    start: string;
    end: string;
  };
  limit?: number;
  offset?: number;
  order_by?: string;
  order_direction?: 'asc' | 'desc';
}

export interface TemperatureMonitoringFilter {
  location?: string;
  equipment_id?: string;
  temperature_range?: {
    min: number;
    max: number;
  };
  alert_triggered?: boolean;
  date_range?: {
    start: string;
    end: string;
  };
  limit?: number;
  offset?: number;
  order_by?: string;
  order_direction?: 'asc' | 'desc';
}

export interface FoodSafetyMetrics {
  tenant_id: string;
  period_start: string;
  period_end: string;
  total_compliance_checks: number;
  passed_checks: number;
  failed_checks: number;
  warning_checks: number;
  compliance_rate: number;
  total_temperature_logs: number;
  temperature_violations: number;
  critical_alerts: number;
  resolved_alerts: number;
  average_resolution_time_hours: number;
  top_risk_ingredients: Array<{
    ingredient_name: string;
    risk_score: number;
    incident_count: number;
  }>;
}

export interface TemperatureAnalytics {
  tenant_id: string;
  location: string;
  period_start: string;
  period_end: string;
  average_temperature: number;
  min_temperature: number;
  max_temperature: number;
  temperature_trend: Array<{
    timestamp: string;
    temperature: number;
    humidity?: number;
  }>;
  violations_count: number;
  uptime_percentage: number;
}

export interface FoodSafetyDashboard {
  tenant_id: string;
  compliance_summary: {
    total_checks: number;
    passed: number;
    failed: number;
    warnings: number;
    compliance_rate: number;
  };
  temperature_monitoring: {
    total_logs: number;
    violations: number;
    locations_monitored: number;
    latest_readings: TemperatureLogResponse[];
  };
  active_alerts: {
    critical: number;
    warning: number;
    info: number;
    overdue: number;
  };
  recent_activities: Array<{
    type: string;
    description: string;
    timestamp: string;
    severity?: string;
  }>;
  upcoming_expirations: Array<{
    ingredient_name: string;
    expiration_date: string;
    days_until_expiry: number;
    quantity: number;
  }>;
}

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