export type UserRole = "admin" | "user";

export type VehicleStatus = "online" | "delayed" | "offline";
export type VehicleKind = "passenger" | "delivery";

export type VehicleColorKey =
  | "visau-orange"
  | "visau-pink"
  | "visau-violet"
  | "visau-charcoal"
  | "visau-indigo"
  | "visau-sky"
  | "visau-teal"
  | "visau-mint"
  | "visau-green";

export const VEHICLE_COLOR_OPTIONS: Array<{ key: VehicleColorKey; label: string; hex: string }> = [
  { key: "visau-orange", label: "Pomarańcz", hex: "#F86020" },
  { key: "visau-pink", label: "Róż", hex: "#F80088" },
  { key: "visau-violet", label: "Fiolet", hex: "#7858D0" },
  { key: "visau-charcoal", label: "Grafit", hex: "#262626" },
  { key: "visau-indigo", label: "Indygo", hex: "#4068F8" },
  { key: "visau-sky", label: "Błękit", hex: "#0098F8" },
  { key: "visau-teal", label: "Turkus", hex: "#00B8C0" },
  { key: "visau-mint", label: "Mięta", hex: "#00D0A0" },
  { key: "visau-green", label: "Zieleń", hex: "#18B868" },
];

export const DEFAULT_VEHICLE_COLOR_KEY: VehicleColorKey = "visau-indigo";

export const getVehicleColorHex = (colorKey?: string | null) =>
  VEHICLE_COLOR_OPTIONS.find((option) => option.key === colorKey)?.hex
  ?? VEHICLE_COLOR_OPTIONS.find((option) => option.key === DEFAULT_VEHICLE_COLOR_KEY)?.hex
  ?? "#4068F8";

export type NormalizedTelemetry = {
  imei: string;
  protocolName: string;
  rawPayload: string;
  deviceTime?: string;
  serverTime: string;
  lat: number;
  lon: number;
  speed?: number;
  heading?: number;
  ignition?: boolean;
  battery?: number;
  gsmSignal?: number;
  satelliteCount?: number;
  gpsValid?: boolean;
  charging?: boolean;
  defense?: boolean;
  positionType?: string;
};

export type DeviceStatus = VehicleStatus;

export type AuthUser = {
  id: string;
  email: string;
  firstName: string;
  lastName: string;
  role: UserRole;
  canEditVehicleInspection: boolean;
};

export type VehicleSummary = {
  id: string;
  name: string;
  plateNumber?: string | null;
  colorKey?: VehicleColorKey | null;
  vehicleKind: VehicleKind;
  deviceId?: string | null;
  imei?: string | null;
  phoneNumber?: string | null;
  status: VehicleStatus;
  lastSeen?: string | null;
  firstHistoryAt?: string | null;
  inspectionDueDate?: string | null;
  inspectionStatus?: "ok" | "due_soon" | "missing";
  lastPosition?: PositionSnapshot | null;
  deviceState?: DeviceStateSnapshot | null;
};

export type PositionSnapshot = {
  lat: number;
  lon: number;
  speed?: number | null;
  heading?: number | null;
  deviceTime?: string | null;
  serverTime: string;
  ignition?: boolean | null;
  battery?: number | null;
  gsmSignal?: number | null;
  satelliteCount?: number | null;
  gpsValid?: boolean | null;
  charging?: boolean | null;
  defense?: boolean | null;
  positionType?: string | null;
};

export type HistoryPoint = PositionSnapshot & {
  id: number;
};

export type DeviceStateSnapshot = {
  phoneNumber?: string | null;
  protocolName?: string | null;
  deviceTime?: string | null;
  battery?: number | null;
  gsmSignal?: number | null;
  ignition?: boolean | null;
  charging?: boolean | null;
  defense?: boolean | null;
  gpsValid?: boolean | null;
  satelliteCount?: number | null;
  positionType?: string | null;
};

export type VehicleAlertSummary = {
  id: string;
  vehicleId: string;
  vehicleName: string;
  vehicleKind: VehicleKind;
  imei?: string | null;
  speed: number;
  triggeredAt: string;
  acknowledgedAt?: string | null;
};

export const getVehicleStatus = (
  lastSeen?: string | Date | null,
  onlineSeconds = 90,
  delayedSeconds = 300,
): VehicleStatus => {
  if (!lastSeen) {
    return "offline";
  }

  const timestamp = lastSeen instanceof Date ? lastSeen.getTime() : new Date(lastSeen).getTime();
  if (Number.isNaN(timestamp)) {
    return "offline";
  }

  const ageSeconds = (Date.now() - timestamp) / 1000;
  if (ageSeconds < onlineSeconds) {
    return "online";
  }
  if (ageSeconds < delayedSeconds) {
    return "delayed";
  }
  return "offline";
};
