export type UserRole =
  | 'PU_AGENT'
  | 'WARD_SUPERVISOR'
  | 'LGA_SUPERVISOR'
  | 'STATE_SUPERVISOR'
  | 'REGIONAL_COORDINATOR'
  | 'NATIONAL_COORDINATOR'
  | 'GETOCORE_TECH_SUPPORT';

export type ScopeType = 'PU' | 'WARD' | 'LGA' | 'STATE' | 'REGION' | 'NATIONAL';

export type GeopoliticalZone =
  | 'NORTH_WEST'
  | 'NORTH_EAST'
  | 'NORTH_CENTRAL'
  | 'SOUTH_WEST'
  | 'SOUTH_EAST'
  | 'SOUTH_SOUTH';

export type BvasStatus = 'WORKING' | 'FAULTY' | 'NO_NETWORK';

export type QueueStatus = 'EMPTY' | 'FEW_VOTERS' | 'LONG_QUEUE' | 'DISPERSED';

export type IncidentCategory =
  | 'BALLOT_SNATCHING'
  | 'VOTER_INTIMIDATION'
  | 'BVAS_FAILURE'
  | 'VIOLENCE'
  | 'LATE_ARRIVAL'
  | 'UNAUTHORIZED_OFFICIALS'
  | 'LOGISTICS_DELAY'
  | 'OTHER';

export type IncidentSeverity = 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';

export type IncidentStatus = 'PENDING' | 'INVESTIGATING' | 'RESOLVED';

export type ResultStatus = 'DRAFT' | 'SUBMITTED' | 'VERIFIED' | 'DISPUTED';

export interface UserSession {
  id: string;
  full_name: string;
  phone_number: string;
  role: UserRole;
  backend_role?: string;
  assigned_scope_type: ScopeType;
  assigned_scope_id: string;
  auth_identifier: string;
  token?: string;
}

export interface StateItem {
  id: string;
  name: string;
  geopolitical_zone: GeopoliticalZone;
  total_lgas: number;
  total_wards: number;
  total_pus: number;
  registered_voters: number;
  voters_2023?: number;
  cvr_growth?: number;
  pus_reported?: number;
  votes?: Record<string, number>;
  total_votes?: number;
  turnout_percentage?: number;
  irev_uploaded_count?: number;
}

export interface LgaItem {
  id: string;
  state_id: string;
  name: string;
  code: string;
  total_wards: number;
  total_pus: number;
  registered_voters: number;
  pus_reported?: number;
  votes?: Record<string, number>;
}

export interface WardItem {
  id: string;
  lga_id: string;
  state_id: string;
  name: string;
  code: string;
  total_pus: number;
  registered_voters: number;
  pus_reported?: number;
  votes?: Record<string, number>;
}

export interface PollingUnitItem {
  id: string;
  state_id: string;
  lga_id: string;
  ward_id: string;
  pu_code: string; // e.g. "24-11-01-001"
  pu_name: string;
  registered_voters: number;
  latitude?: number;
  longitude?: number;
  delimitation_address?: string;
}

// Operational Modules
export interface OpenPollsData {
  pu_id: string;
  officials_present_on_time: boolean;
  materials_arrival_time: string;
  poll_opened_at: string;
  notes?: string;
  reported_by?: string;
}

export interface BvasReadinessData {
  pu_id: string;
  status: BvasStatus;
  battery_level: number;
  backup_device_available: boolean;
  zero_accreditation_verified: boolean;
  zero_accreditation_time?: string;
  serial_number?: string;
  notes?: string;
  reported_by?: string;
}

export interface OngoingTurnoutData {
  pu_id: string;
  hour_interval: string;
  estimated_voter_turnout: number;
  is_peaceful: boolean;
  quick_note?: string;
  reported_by?: string;
  timestamp?: string;
}

export interface ClosingPollsData {
  pu_id: string;
  official_closing_time: string;
  queue_status_at_cutoff: QueueStatus;
  estimated_queue_length: number;
  cutoff_compliance_confirmed: boolean;
  notes?: string;
  reported_by?: string;
}

export interface PuResultData {
  pu_id: string;
  pu_code: string;
  registered_voters: number;
  accredited_voters: number;
  rejected_ballots: number;
  party_votes: Record<string, number>;
  total_valid_votes: number;
  total_votes_cast: number;
  form_ec8a_url: string;
  physical_ec8a_photo?: string;
  is_balanced: boolean;
  discrepancy_count: number;
  over_voting_detected: boolean;
  bvas_zero_accredited_verified: boolean;
  agent_signature_confirmed: boolean;
  agent_signed_at?: string;
  status: ResultStatus;
  reported_by: string;
  submitted_at: string;
  agent_name?: string;
  agent_phone?: string;

  // INEC IReV Server Comparison Integration
  inec_irev_url?: string;
  inec_irev_votes?: Record<string, number>;
  inec_irev_uploaded_at?: string;
  irev_discrepancy_detected?: boolean;
  irev_variance_notes?: string;
}

export interface IncidentReport {
  id: string;
  pu_id: string;
  pu_code: string;
  state_name: string;
  lga_name: string;
  category: IncidentCategory;
  severity: IncidentSeverity;
  status: IncidentStatus;
  latitude?: number;
  longitude?: number;
  description: string;
  media_urls: string[];
  reported_by: string;
  reported_by_name?: string;
  created_at: string;
  resolution_notes?: string;
}

export interface OfflineQueueItem {
  id: string;
  type: 'OPEN_POLLS' | 'BVAS' | 'TURNOUT' | 'CLOSING' | 'RESULT_EC8A' | 'INCIDENT';
  payload: any;
  timestamp: number;
  attempts: number;
  status: 'PENDING' | 'SYNCING' | 'FAILED' | 'SYNCED';
  errorMessage?: string;
}

export interface TimelineActivityEvent {
  id: string;
  state_name: string;
  zone: GeopoliticalZone;
  event_type: 'PU_OPENED' | 'BVAS_ZERO_AUDIT' | 'TURNOUT_UPDATE' | 'RESULT_TRANSMITTED' | 'IREV_MATCH' | 'IREV_DISCREPANCY' | 'INCIDENT_BEACON';
  title: string;
  description: string;
  timestamp: string;
  severity?: 'NORMAL' | 'ALERT' | 'CRITICAL';
}

// 2-Way Field Communication Types
export type DirectiveType =
  | 'SITUATION_ROOM_BROADCAST'
  | 'SOS_ALERT'
  | 'LOGISTICS_UPDATE'
  | 'LEGAL_DIRECTIVE';

export interface CommunicationMessage {
  id: string;
  sender: string;
  role: string;
  jurisdiction: string;
  pu_code?: string;
  message: string;
  timestamp: string;
  type: DirectiveType | 'FIELD_REPORT';
  status: 'DELIVERED' | 'ACKNOWLEDGED';
}

// 6 Chronological Election Lifecycle Stages & Phased Reporting
export type ElectionSegmentId =
  | 'STAGE_1_ARRIVAL_SETUP'
  | 'STAGE_2_BVAS_ZERO_CHECK'
  | 'STAGE_3_VOTING_COMMENCEMENT'
  | 'STAGE_4_QUEUE_CUTOFF'
  | 'STAGE_5_BALLOT_SORTING_COUNTING'
  | 'STAGE_6_EC8A_ENDORSEMENT_UPLOAD';

export interface ElectionStageMeta {
  id: ElectionSegmentId;
  stageNumber: number;
  name: string;
  shortName: string;
  timeWindow: string;
  description: string;
  checkQuestions: string[];
}

export interface SegmentReport {
  id: string;
  segmentId: ElectionSegmentId;
  segmentName: string;
  puCode: string;
  puName: string;
  state: string;
  lga: string;
  ward: string;
  reportedBy: string;
  agentPhone: string;
  reportedAt: string;
  status: 'COMPLETED' | 'IN_PROGRESS' | 'DELAYED' | 'INCIDENT_FLAGGED';
  notes: string;
  bvasZeroConfirmed?: boolean;
  registeredVoters?: number;
  accreditedVoters?: number;
  adcVotes?: number;
  apcVotes?: number;
  pdpVotes?: number;
  lpVotes?: number;
  ec8aPhotoUrl?: string;
}
