export type Protocol = 'GB32960' | 'JT808' | 'YUTONG_MQTT';

export type VehicleStatus = 'online' | 'warning' | 'offline';

export type SourceHealth = 'healthy' | 'warning' | 'down';

export type VehicleSourceSnapshot = {
  id: string;
  protocol: Protocol;
  sourceName: string;
  endpoint: string;
  platformAccount: string;
  status: VehicleStatus;
  latencyMs: number;
  lastSeen: string;
  lastSeenAt?: string;
  totalMileageKm?: number;
  todayMileageKm?: number;
  speedKph?: number;
  fieldCount: number;
};

export type VehicleRecord = {
  id: string;
  plate: string;
  vin: string;
  phone?: string;
  oem: string;
  status: VehicleStatus;
  city: string;
  address: string;
  longitude: number;
  latitude: number;
  mapX: number;
  mapY: number;
  heading: number;
  soc?: number;
  hydrogenKg?: number;
  primarySourceId: string;
  sources: VehicleSourceSnapshot[];
  flatFields: Record<Protocol, Record<string, string | number | boolean | null>>;
};

export type DataSourceRecord = {
  id: string;
  name: string;
  protocol: Protocol;
  endpoint: string;
  platformAccount: string;
  health: SourceHealth;
  vehicleCount: number;
  onlineCount: number;
  latencyP95Ms: number;
  lastFrameAt: string;
  owner: string;
};

export type TrackPoint = {
  id: string;
  vehicleId: string;
  time: string;
  timestampMs?: number;
  state: 'running' | 'stop' | 'alert';
  address: string;
  longitude?: number;
  latitude?: number;
  speedKph: number;
  mileageKm: number;
  note: string;
};

export type AlertEvent = {
  id: string;
  vehicleId: string;
  level: 'P0' | 'P1' | 'P2';
  type: '断链' | '里程跳变' | '字段缺失' | '定位异常';
  title: string;
  detail: string;
  sourceId: string;
  time: string;
  status: 'open' | 'processing' | 'closed';
};

export type HistoryRecord = {
  id: string;
  vehicleId: string;
  protocol: Protocol;
  frameType: string;
  receiveTime: string;
  deviceTime: string;
  sourceName: string;
  latencyMs: number;
  fieldCount: number;
  exportable: boolean;
};

export type FieldOption = {
  key: string;
  label: string;
  protocol: Protocol | 'ALL';
};
