export const bluetoothLeTrackerDomain = 'bluetooth_le_tracker';
export const bluetoothLeTrackerBlePrefix = 'BLE_';
export const bluetoothLeTrackerSourceType = 'bluetooth_le';
export const bluetoothLeTrackerDefaultScanIntervalSeconds = 12;
export const bluetoothLeTrackerDefaultConsiderHomeSeconds = 180;
export const bluetoothLeTrackerDefaultTrackBatteryIntervalSeconds = 86400;
export const bluetoothLeTrackerDefaultMinSeenNew = 5;

export type TBluetoothLePresenceState = 'home' | 'not_home';
export type TBluetoothLeTrackerCommandType = 'scan';

export type TBluetoothLeScanProvider = () => Promise<
  | IBluetoothLeAdvertisement[]
  | IBluetoothLeTrackerSnapshot
  | {
      snapshot?: IBluetoothLeTrackerSnapshot;
      devices?: IBluetoothLeTrackedDevice[];
      advertisements?: IBluetoothLeAdvertisement[];
    }
>;

export interface IBluetoothLeTrackerConfig {
  scanIntervalSeconds?: number;
  interval_seconds?: number;
  trackNewDevices?: boolean;
  track_new_devices?: boolean;
  trackBattery?: boolean;
  track_battery?: boolean;
  trackBatteryIntervalSeconds?: number;
  track_battery_interval?: number;
  considerHomeSeconds?: number;
  consider_home?: number;
  minSeenNew?: number;
  snapshot?: IBluetoothLeTrackerSnapshot;
  knownDevices?: IBluetoothLeTrackedDevice[];
  trackedDevices?: IBluetoothLeTrackedDevice[];
  devices?: IBluetoothLeTrackedDevice[];
  advertisements?: IBluetoothLeAdvertisement[];
  manualEntries?: IBluetoothLeManualEntry[];
  events?: IBluetoothLeTrackerEvent[];
  scanProvider?: TBluetoothLeScanProvider;
}

export interface IHomeAssistantBluetoothLeTrackerConfig extends IBluetoothLeTrackerConfig {}

export interface IBluetoothLeTrackerScannerState {
  scanIntervalSeconds: number;
  trackNewDevices: boolean;
  trackBattery: boolean;
  trackBatteryIntervalSeconds: number;
  considerHomeSeconds: number;
  minSeenNew: number;
}

export interface IBluetoothLeAdvertisement {
  address?: string;
  mac?: string;
  macAddress?: string;
  haMac?: string;
  name?: string;
  localName?: string;
  hostName?: string;
  rssi?: number;
  txPower?: number;
  connectable?: boolean;
  serviceUuids?: string[];
  serviceUUIDs?: string[];
  serviceData?: Record<string, unknown>;
  manufacturerData?: Record<string, unknown>;
  battery?: number;
  time?: number;
  lastSeen?: number | string;
  last_seen?: number | string;
  source?: string;
  sourceType?: string;
  metadata?: Record<string, unknown>;
  raw?: Record<string, unknown>;
  [key: string]: unknown;
}

export interface IBluetoothLeTrackedDevice {
  address?: string;
  mac?: string;
  macAddress?: string;
  haMac?: string;
  name?: string;
  hostName?: string;
  hostname?: string;
  manufacturer?: string;
  model?: string;
  track?: boolean;
  tracked?: boolean;
  trackBattery?: boolean;
  track_battery?: boolean;
  battery?: number;
  rssi?: number;
  txPower?: number;
  connectable?: boolean;
  serviceUuids?: string[];
  serviceUUIDs?: string[];
  serviceData?: Record<string, unknown>;
  manufacturerData?: Record<string, unknown>;
  lastSeen?: number | string;
  last_seen?: number | string;
  firstSeen?: number | string;
  first_seen?: number | string;
  advertisementCount?: number;
  state?: TBluetoothLePresenceState | 'on' | 'off' | string;
  sourceType?: string;
  metadata?: Record<string, unknown>;
  raw?: Record<string, unknown>;
  [key: string]: unknown;
}

export interface IBluetoothLeManualEntry extends IBluetoothLeTrackedDevice {
  id?: string;
  knownDevices?: IBluetoothLeTrackedDevice[];
  trackedDevices?: IBluetoothLeTrackedDevice[];
  devices?: IBluetoothLeTrackedDevice[];
  advertisements?: IBluetoothLeAdvertisement[];
  snapshot?: IBluetoothLeTrackerSnapshot;
}

export interface IBluetoothLeDiscoveryRecord extends IBluetoothLeAdvertisement {
  id?: string;
}

export interface IBluetoothLeTrackerSnapshot {
  connected: boolean;
  scanner: IBluetoothLeTrackerScannerState;
  devices: IBluetoothLeTrackedDevice[];
  advertisements: IBluetoothLeAdvertisement[];
  events: IBluetoothLeTrackerEvent[];
}

export interface IBluetoothLeTrackerCommand {
  type: TBluetoothLeTrackerCommandType;
  service: string;
  target: {
    entityId?: string;
    deviceId?: string;
  };
  snapshot?: IBluetoothLeTrackerSnapshot;
  devices?: IBluetoothLeTrackedDevice[];
  advertisements?: IBluetoothLeAdvertisement[];
}

export interface IBluetoothLeTrackerCommandResult {
  success: boolean;
  error?: string;
  data?: unknown;
}

export interface IBluetoothLeTrackerEvent {
  type?: 'scan_applied' | 'snapshot_updated' | 'unsupported_scan' | string;
  deviceId?: string;
  entityId?: string;
  data?: unknown;
  timestamp?: number;
}
