import { Branch } from './Branch';
import { Employee } from './Employee';

export type IncidentType =
  | 'near_miss'
  | 'first_aid'
  | 'medical_treatment'
  | 'lost_time_injury'
  | 'permanent_disability'
  | 'fatality'
  | 'dangerous_occurrence'
  | 'occupational_disease';

export type IncidentSeverity = 'low' | 'medium' | 'high' | 'critical';

export type IncidentStatus =
  | 'reported'
  | 'under_review'
  | 'classified'
  | 'authorities_notified'
  | 'under_investigation'
  | 'investigation_complete'
  | 'corrective_action'
  | 'return_to_work'
  | 'closed'
  | 'reopened';

export type InjuredPersonType = 'employee' | 'contractor' | 'visitor';

export type BodyPartAffected =
  | 'head'
  | 'face'
  | 'neck'
  | 'chest'
  | 'abdomen'
  | 'upper_back'
  | 'lower_back'
  | 'left_shoulder'
  | 'right_shoulder'
  | 'left_arm'
  | 'right_arm'
  | 'left_hand'
  | 'right_hand'
  | 'left_leg'
  | 'right_leg'
  | 'left_foot'
  | 'right_foot';

export type InjuryNature =
  | 'cut'
  | 'fracture'
  | 'burn'
  | 'strain'
  | 'sprain'
  | 'crush'
  | 'poisoning'
  | 'hearing_loss'
  | 'eye_injury'
  | 'respiratory'
  | 'skin_condition'
  | 'other';

export type RegulatoryAuthorityType = 'dfi' | 'labour_office' | 'insurance_company';

export type RegulatoryStatus = 'pending' | 'sent' | 'acknowledged' | 'overdue';

export type CorrectiveActionStatus = 'pending' | 'in_progress' | 'completed' | 'overdue';

export type IncidentDocumentType =
  | 'photo'
  | 'investigation_report'
  | 'medical_certificate'
  | 'witness_statement'
  | 'dfi_correspondence'
  | 'insurance_form'
  | 'other';

export interface Incident {
  id: string;
  incident_number: string;
  incident_date: string;
  incident_time?: string | null;
  reported_date: string;
  reported_by_employee_id?: string | null;
  location_description: string;
  branch_id?: string | null;
  incident_type: IncidentType;
  severity: IncidentSeverity;
  status: IncidentStatus;
  description: string;
  activity_before_incident?: string | null;
  equipment_involved?: string | null;
  supervisor_on_duty_id?: string | null;
  investigating_officer_id?: string | null;
  created_at: string;
  updated_at: string;

  reported_by_employee?: Employee;
  branch?: Branch;
  supervisor_on_duty?: Employee;
  investigating_officer?: Employee;
  injuries?: IncidentInjury[];
  regulatory_reports?: IncidentRegulatoryReport[];
  corrective_actions?: IncidentCorrectiveAction[];
  documents?: IncidentDocument[];
  witnesses?: IncidentWitness[];
  timeline?: IncidentTimeline[];
}

export interface IncidentInjury {
  id: string;
  incident_id: string;
  injured_person_type: InjuredPersonType;
  injured_employee_id?: string | null;
  injured_person_name?: string | null;
  injured_person_employer?: string | null;
  body_parts_affected: BodyPartAffected[];
  nature_of_injury: InjuryNature;
  first_aid_given: boolean;
  first_aider_name?: string | null;
  medical_treatment_required: boolean;
  hospital_name?: string | null;
  medical_leave_days?: number | null;
  actual_days_lost?: number | null;
  permanent_disability: boolean;
  disability_percentage?: number | null;
  fatality: boolean;
  death_date?: string | null;
  return_to_work_date?: string | null;
  modified_duties_required: boolean;
  fitness_certificate_received: boolean;
  created_at: string;
  updated_at: string;

  injured_employee?: Employee;
}

export interface IncidentRegulatoryReport {
  id: string;
  incident_id: string;
  authority_type: RegulatoryAuthorityType;
  notification_required: boolean;
  notification_deadline?: string | null;
  notification_date?: string | null;
  reference_number?: string | null;
  method?: string | null;
  notes?: string | null;
  notified_by?: string | null;
  status: RegulatoryStatus;
  created_at: string;
  updated_at: string;
}

export interface IncidentCorrectiveAction {
  id: string;
  incident_id: string;
  description: string;
  action_owner_id?: string | null;
  target_date: string;
  completion_date?: string | null;
  status: CorrectiveActionStatus;
  effectiveness_review_date?: string | null;
  effectiveness_notes?: string | null;
  created_at: string;
  updated_at: string;

  action_owner?: Employee;
}

export interface IncidentDocument {
  id: string;
  incident_id: string;
  document_type: IncidentDocumentType;
  file_path: string;
  file_name: string;
  uploaded_by?: string | null;
  created_at: string;
  updated_at: string;
}

export interface IncidentWitness {
  id: string;
  incident_id: string;
  employee_id?: string | null;
  witness_name?: string | null;
  statement?: string | null;
  statement_date?: string | null;
  created_at: string;
  updated_at: string;

  employee?: Employee;
}

export interface IncidentTimeline {
  id: string;
  incident_id: string;
  action: string;
  old_status?: string | null;
  new_status?: string | null;
  notes?: string | null;
  performed_by?: string | null;
  created_at: string;
  updated_at: string;
}

export interface IncidentStats {
  total: number;
  by_status: Record<string, number>;
  critical: number;
  open: number;
  pending_regulatory_reports: number;
  overdue_regulatory_reports: number;
}

export interface CreateIncidentPayload {
  incident_date: string;
  incident_time?: string | null;
  reported_date: string;
  reported_by_employee_id?: string | null;
  location_description: string;
  branch_id?: string | null;
  incident_type: IncidentType;
  severity: IncidentSeverity;
  status?: IncidentStatus;
  description: string;
  activity_before_incident?: string | null;
  equipment_involved?: string | null;
  supervisor_on_duty_id?: string | null;
  investigating_officer_id?: string | null;
}

export interface UpdateIncidentPayload extends Partial<CreateIncidentPayload> {
  id: string;
}

export interface UpdateIncidentStatusPayload {
  id: string;
  status: IncidentStatus;
  notes?: string;
}

export interface CreateIncidentInjuryPayload {
  incident_id: string;
  injured_person_type: InjuredPersonType;
  injured_employee_id?: string | null;
  injured_person_name?: string | null;
  injured_person_employer?: string | null;
  body_parts_affected: BodyPartAffected[];
  nature_of_injury: InjuryNature;
  first_aid_given?: boolean;
  first_aider_name?: string | null;
  medical_treatment_required?: boolean;
  hospital_name?: string | null;
  medical_leave_days?: number | null;
  actual_days_lost?: number | null;
  permanent_disability?: boolean;
  disability_percentage?: number | null;
  fatality?: boolean;
  death_date?: string | null;
  return_to_work_date?: string | null;
  modified_duties_required?: boolean;
  fitness_certificate_received?: boolean;
}

export interface UpdateIncidentInjuryPayload extends Partial<CreateIncidentInjuryPayload> {
  incident_id: string;
  injury_id: string;
}

export interface DeleteIncidentInjuryPayload {
  incident_id: string;
  injury_id: string;
}

export interface UploadIncidentDocumentPayload {
  incident_id: string;
  document_type: IncidentDocumentType;
  file: File;
}

export interface DeleteIncidentDocumentPayload {
  incident_id: string;
  document_id: string;
}

export interface CreateIncidentWitnessPayload {
  incident_id: string;
  employee_id?: string | null;
  witness_name?: string | null;
  statement?: string | null;
  statement_date?: string | null;
}

export interface UpdateIncidentWitnessPayload extends Partial<CreateIncidentWitnessPayload> {
  incident_id: string;
  witness_id: string;
}

export interface DeleteIncidentWitnessPayload {
  incident_id: string;
  witness_id: string;
}

export interface CreateCorrectiveActionPayload {
  incident_id: string;
  description: string;
  action_owner_id?: string | null;
  target_date: string;
  completion_date?: string | null;
  status?: CorrectiveActionStatus;
  effectiveness_review_date?: string | null;
  effectiveness_notes?: string | null;
}

export interface UpdateCorrectiveActionPayload extends Partial<CreateCorrectiveActionPayload> {
  incident_id: string;
  corrective_action_id: string;
}

export interface CreateRegulatoryReportPayload {
  incident_id: string;
  authority_type: RegulatoryAuthorityType;
  notification_required?: boolean;
  notification_deadline?: string | null;
  notification_date?: string | null;
  reference_number?: string | null;
  method?: string | null;
  notes?: string | null;
  notified_by?: string | null;
  status?: RegulatoryStatus;
}

export interface UpdateRegulatoryReportPayload extends Partial<CreateRegulatoryReportPayload> {
  incident_id: string;
  report_id: string;
}
