// User types
export enum Role {
  ADMINISTRATOR = 'ADMINISTRATOR',
  COORDINATOR = 'COORDINATOR',
  DRIVER = 'DRIVER',
}

export interface User {
  id: string;
  auth0Id: string;
  email: string;
  name: string | null;
  picture: string | null;
  role: Role;
  isApproved: boolean;
  driver?: Driver | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

// Department types
export enum Department {
  OFFICE_OF_DEVELOPMENT = 'OFFICE_OF_DEVELOPMENT',
  ADMIN = 'ADMIN',
  OTHER = 'OTHER',
}

export enum ArrivalMode {
  FLIGHT = 'FLIGHT',
  SELF_DRIVING = 'SELF_DRIVING',
}

// VIP types
export interface VIP {
  id: string;
  name: string;
  organization: string | null;
  department: Department;
  arrivalMode: ArrivalMode;
  expectedArrival: string | null;
  airportPickup: boolean;
  venueTransport: boolean;
  partySize: number;
  notes: string | null;
  // Roster and contact info
  isRosterOnly: boolean;
  phone: string | null;
  email: string | null;
  emergencyContactName: string | null;
  emergencyContactPhone: string | null;
  // Relations
  flights?: Flight[];
  events?: ScheduleEvent[];
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

// Driver types
export interface Driver {
  id: string;
  name: string;
  phone: string;
  department: Department | null;
  userId: string | null;
  user?: User | null;
  events?: ScheduleEvent[];
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

// Vehicle types
export enum VehicleType {
  VAN = 'VAN',
  SUV = 'SUV',
  SEDAN = 'SEDAN',
  BUS = 'BUS',
  GOLF_CART = 'GOLF_CART',
  TRUCK = 'TRUCK',
}

export enum VehicleStatus {
  AVAILABLE = 'AVAILABLE',
  IN_USE = 'IN_USE',
  MAINTENANCE = 'MAINTENANCE',
  RESERVED = 'RESERVED',
}

export interface Vehicle {
  id: string;
  name: string;
  type: VehicleType;
  licensePlate: string | null;
  seatCapacity: number;
  status: VehicleStatus;
  currentDriverId: string | null;
  currentDriver?: Driver | null;
  notes: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

// Event types
export enum EventType {
  TRANSPORT = 'TRANSPORT',
  MEETING = 'MEETING',
  EVENT = 'EVENT',
  MEAL = 'MEAL',
  ACCOMMODATION = 'ACCOMMODATION',
}

export enum EventStatus {
  SCHEDULED = 'SCHEDULED',
  IN_PROGRESS = 'IN_PROGRESS',
  COMPLETED = 'COMPLETED',
  CANCELLED = 'CANCELLED',
}

export interface ScheduleEvent {
  id: string;
  vipIds: string[]; // Array of VIP IDs for multi-passenger trips
  vips?: VIP[]; // Populated VIP objects
  title: string;
  location: string | null;
  pickupLocation: string | null;
  dropoffLocation: string | null;
  startTime: string;
  endTime: string;
  description: string | null;
  type: EventType;
  status: EventStatus;
  driverId: string | null;
  driver?: Driver | null;
  vehicleId: string | null;
  vehicle?: Vehicle | null;
  masterEventId: string | null;
  masterEvent?: { id: string; title: string; type: EventType; startTime: string; endTime: string } | null;
  childEvents?: { id: string; title: string; type: EventType }[];
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

// Flight types
export type FlightStatus = 'scheduled' | 'active' | 'landed' | 'cancelled' | 'incident' | 'diverted';
export type TrackingPhase = 'FAR_OUT' | 'PRE_DEPARTURE' | 'DEPARTURE_WINDOW' | 'ACTIVE' | 'ARRIVAL_WINDOW' | 'LANDED' | 'TERMINAL';

export interface Flight {
  id: string;
  vipId: string;
  vip?: VIP;
  flightNumber: string;
  flightDate: string;
  segment: number;
  departureAirport: string;
  arrivalAirport: string;
  scheduledDeparture: string | null;
  scheduledArrival: string | null;
  actualDeparture: string | null;
  actualArrival: string | null;
  status: string | null;

  // Airline info
  airlineName: string | null;
  airlineIata: string | null;

  // Terminal/gate/baggage
  departureTerminal: string | null;
  departureGate: string | null;
  arrivalTerminal: string | null;
  arrivalGate: string | null;
  arrivalBaggage: string | null;

  // Estimated times (from API)
  estimatedDeparture: string | null;
  estimatedArrival: string | null;

  // Delays in minutes
  departureDelay: number | null;
  arrivalDelay: number | null;

  // Aircraft
  aircraftType: string | null;

  // Live position
  liveLatitude: number | null;
  liveLongitude: number | null;
  liveAltitude: number | null;
  liveSpeed: number | null;
  liveDirection: number | null;
  liveIsGround: boolean | null;
  liveUpdatedAt: string | null;

  // Tracking metadata
  lastPolledAt: string | null;
  pollCount: number;
  trackingPhase: TrackingPhase;
  autoTrackEnabled: boolean;

  createdAt: string;
  updatedAt: string;
}

export interface FlightBudget {
  used: number;
  limit: number;
  remaining: number;
  month: string;
}

// Multi-segment journey types
export type LayoverRiskLevel = 'none' | 'ok' | 'warning' | 'critical' | 'missed';

export interface Layover {
  airport: string;
  afterSegmentIndex: number;
  scheduledMinutes: number;
  effectiveMinutes: number;
  risk: LayoverRiskLevel;
}

export interface Journey {
  vipId: string;
  vip: VIP | undefined;
  flights: Flight[];
  layovers: Layover[];
  effectiveStatus: string;
  currentSegmentIndex: number;
  hasLayoverRisk: boolean;
  origin: string;
  destination: string;
  isMultiSegment: boolean;
}
