// frontend/src/api/types/qualityTemplates.ts
/**
 * Quality Check Template types for API integration
 */

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

export enum ProcessStage {
  MIXING = 'mixing',
  PROOFING = 'proofing',
  SHAPING = 'shaping',
  BAKING = 'baking',
  COOLING = 'cooling',
  PACKAGING = 'packaging',
  FINISHING = 'finishing'
}

export interface QualityCheckTemplate {
  id: string;
  tenant_id: string;
  name: string;
  template_code?: string;
  check_type: QualityCheckType;
  category?: string;
  description?: string;
  instructions?: string;
  parameters?: Record<string, any>;
  thresholds?: Record<string, any>;
  scoring_criteria?: Record<string, any>;
  is_active: boolean;
  is_required: boolean;
  is_critical: boolean;
  weight: number;
  min_value?: number;
  max_value?: number;
  target_value?: number;
  unit?: string;
  tolerance_percentage?: number;
  applicable_stages?: ProcessStage[];
  created_by: string;
  created_at: string;
  updated_at: string;
}

export interface QualityCheckTemplateCreate {
  name: string;
  template_code?: string;
  check_type: QualityCheckType;
  category?: string;
  description?: string;
  instructions?: string;
  parameters?: Record<string, any>;
  thresholds?: Record<string, any>;
  scoring_criteria?: Record<string, any>;
  is_active?: boolean;
  is_required?: boolean;
  is_critical?: boolean;
  weight?: number;
  min_value?: number;
  max_value?: number;
  target_value?: number;
  unit?: string;
  tolerance_percentage?: number;
  applicable_stages?: ProcessStage[];
  created_by: string;
}

export interface QualityCheckTemplateUpdate {
  name?: string;
  template_code?: string;
  check_type?: QualityCheckType;
  category?: string;
  description?: string;
  instructions?: string;
  parameters?: Record<string, any>;
  thresholds?: Record<string, any>;
  scoring_criteria?: Record<string, any>;
  is_active?: boolean;
  is_required?: boolean;
  is_critical?: boolean;
  weight?: number;
  min_value?: number;
  max_value?: number;
  target_value?: number;
  unit?: string;
  tolerance_percentage?: number;
  applicable_stages?: ProcessStage[];
}

export interface QualityCheckTemplateList {
  templates: QualityCheckTemplate[];
  total: number;
  skip: number;
  limit: number;
}

export interface QualityCheckCriterion {
  id: string;
  name: string;
  description: string;
  check_type: QualityCheckType;
  required: boolean;
  weight: number;
  acceptable_criteria: string;
  min_value?: number;
  max_value?: number;
  unit?: string;
  is_critical: boolean;
}

export interface QualityCheckResult {
  criterion_id: string;
  value: number | string | boolean;
  score: number;
  notes?: string;
  photos?: string[];
  pass_check: boolean;
  timestamp: string;
}

export interface QualityCheckExecutionRequest {
  template_id: string;
  batch_id: string;
  process_stage: ProcessStage;
  checker_id?: string;
  results: QualityCheckResult[];
  final_notes?: string;
  photos?: string[];
}

export interface QualityCheckExecutionResponse {
  check_id: string;
  overall_score: number;
  overall_pass: boolean;
  critical_failures: string[];
  corrective_actions: string[];
  timestamp: string;
}

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

export interface RecipeQualityConfiguration {
  stages: Record<string, ProcessStageQualityConfig>;
  global_parameters?: Record<string, any>;
  default_templates?: string[];
  overall_quality_threshold?: number;
  critical_stage_blocking?: boolean;
  auto_create_quality_checks?: boolean;
  quality_manager_approval_required?: boolean;
}

// Filter and query types
export interface QualityTemplateFilters {
  stage?: ProcessStage;
  check_type?: QualityCheckType;
  is_active?: boolean;
  category?: string;
  search?: string;
}

export interface QualityTemplateQueryParams extends QualityTemplateFilters {
  skip?: number;
  limit?: number;
}