/**
 * 🚗 Interfaces para Rastreamento GPS do Veículo
 * 
 * Define estruturas de dados para tracking GPS em tempo real,
 * histórico de posições e eventos relacionados ao veículo.
 */

// ========================================
// 📍 INTERFACE PRINCIPAL - PONTO GPS
// ========================================

export interface GpsTrackPoint {
  lat: number;
  lng: number;
  timestamp: string | Date;
  speed?: number; // km/h
  altitude?: number; // metros
  accuracy?: number; // metros
  event?: GpsEventType;
  stopId?: string; // ID da parada relacionada
  nearStopId?: string; // ID da parada próxima
  location?: string; // Descrição do local
}

// ========================================
// 🎯 TIPOS DE EVENTOS GPS
// ========================================

export type GpsEventType = 
  | 'route_start'
  | 'route_end'
  | 'moving'
  | 'stop_arrival'
  | 'stop_departure'
  | 'approaching_stop'
  | 'fuel_stop'
  | 'fuel_departure'
  | 'delivery_arrival'
  | 'delivery_departure'
  | 'rest_start'
  | 'rest_end'
  | 'emergency_stop'
  | 'maintenance_stop'
  | 'traffic_delay'
  | 'route_deviation';

// ========================================
// 🚛 INTERFACE - TRACK COMPLETO
// ========================================

export interface VehicleGpsTrack {
  trackId: string;
  routeId: string;
  vehiclePlate: string;
  driverName: string;
  startTime: string | Date;
  endTime?: string | Date;
  totalDistance: number; // km
  trackPoints: GpsTrackPoint[];
  summary: GpsTrackSummary;
}

// ========================================
// 📊 INTERFACE - RESUMO DO TRACK
// ========================================

export interface GpsTrackSummary {
  totalPoints: number;
  totalTimeMinutes: number;
  averageSpeed: number; // km/h
  maxSpeed: number; // km/h
  stopEvents: number;
  fuelStops: number;
  deliveryStops: number;
  restStops: number;
  emergencyStops?: number;
  deviations?: number;
}

// ========================================
// 🔍 INTERFACE - FILTROS GPS
// ========================================

export interface GpsTrackFilters {
  routeId?: string;
  vehiclePlate?: string;
  driverName?: string;
  dateRange?: {
    start: Date;
    end: Date;
  };
  eventTypes?: GpsEventType[];
  minSpeed?: number;
  maxSpeed?: number;
  hasStops?: boolean;
  page?: number;
  limit?: number;
}

// ========================================
// 📱 INTERFACE - GPS EM TEMPO REAL
// ========================================

export interface RealTimeGpsData {
  vehiclePlate: string;
  routeId: string;
  currentPosition: GpsTrackPoint;
  lastUpdate: Date;
  isOnline: boolean;
  signalStrength?: number; // 0-100%
  batteryLevel?: number; // 0-100%
  deviceInfo?: {
    deviceId: string;
    model: string;
    firmware: string;
  };
}

// ========================================
// 📊 INTERFACE - RESPOSTA DA API
// ========================================

export interface GpsTrackResponse {
  data: VehicleGpsTrack[];
  pagination?: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
  source: 'backend' | 'fallback';
  timestamp: string;
}

// ========================================
// 🌍 INTERFACE - ANÁLISE DE ROTA
// ========================================

export interface RouteAnalysis {
  plannedRoute: GpsTrackPoint[];
  actualRoute: GpsTrackPoint[];
  deviations: RouteDeviation[];
  efficiency: {
    distanceVariance: number; // %
    timeVariance: number; // %
    fuelEfficiency: number; // km/l
    score: number; // 0-100
  };
}

export interface RouteDeviation {
  startPoint: GpsTrackPoint;
  endPoint: GpsTrackPoint;
  deviationDistance: number; // metros
  deviationTime: number; // minutos
  reason?: string;
  severity: 'low' | 'medium' | 'high';
}

// ========================================
// 📈 INTERFACE - MÉTRICAS GPS
// ========================================

export interface GpsMetrics {
  totalTracks: number;
  totalDistance: number; // km
  totalTime: number; // horas
  averageSpeed: number; // km/h
  onTimeDeliveries: number;
  delayedDeliveries: number;
  punctualityRate: number; // %
  fuelEfficiency: number; // km/l
  
  // Métricas por período
  daily: { [date: string]: GpsTrackSummary };
  weekly: { [week: string]: GpsTrackSummary };
  monthly: { [month: string]: GpsTrackSummary };
  
  // Métricas por veículo
  byVehicle: { [plate: string]: GpsTrackSummary };
  
  // Métricas por motorista
  byDriver: { [driver: string]: GpsTrackSummary };
}

// ========================================
// 🚨 INTERFACE - ALERTAS GPS
// ========================================

export interface GpsAlert {
  alertId: string;
  routeId: string;
  vehiclePlate: string;
  type: GpsAlertType;
  severity: 'info' | 'warning' | 'critical';
  message: string;
  position: GpsTrackPoint;
  timestamp: Date;
  acknowledged: boolean;
  acknowledgedBy?: string;
  acknowledgedAt?: Date;
}

export type GpsAlertType = 
  | 'speed_limit'
  | 'route_deviation'
  | 'long_stop'
  | 'fuel_low'
  | 'maintenance_due'
  | 'emergency'
  | 'geofence_exit'
  | 'geofence_enter'
  | 'signal_lost'
  | 'battery_low'; 