export type THomekitTransport = 'ip' | 'ble' | 'thread' | 'coap' | 'snapshot' | 'unknown';

export type THomekitControllerCommandType =
  | 'snapshot'
  | 'pair_setup'
  | 'pair_verify'
  | 'read_characteristics'
  | 'write_characteristics'
  | 'subscribe_events'
  | 'identify'
  | 'camera_snapshot';

export interface IHomekitControllerConfig {
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  setupCode?: string;
  allowInsecureSetupCode?: boolean;
  pairingData?: IHomekitPairingData;
  paired?: boolean;
  transport?: THomekitTransport;
  model?: string;
  manufacturer?: string;
  category?: string | number;
  configNumber?: number;
  stateNumber?: number;
  accessories?: IHomekitAccessory[];
  snapshot?: IHomekitControllerSnapshot;
  discovery?: IHomekitDiscoveryRecord;
  connected?: boolean;
}

export interface IHomeAssistantHomekitControllerConfig extends IHomekitControllerConfig {}

export interface IHomekitPairingData {
  AccessoryPairingID?: string;
  AccessoryIP?: string;
  AccessoryIPs?: string[];
  AccessoryPort?: number;
  AccessoryAddress?: string;
  Connection?: string;
  iOSPairingId?: string;
  iOSDeviceLTSK?: string;
  iOSDeviceLTPK?: string;
  AccessoryLTPK?: string;
  [key: string]: unknown;
}

export interface IHomekitDiscoveryRecord {
  source?: 'mdns' | 'manual' | 'bluetooth' | 'snapshot';
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  model?: string;
  manufacturer?: string;
  category?: string | number;
  paired?: boolean;
  configNumber?: number;
  stateNumber?: number;
  statusFlags?: number;
  setupHash?: string;
  protocolVersion?: string;
  txt?: Record<string, string | undefined>;
}

export interface IHomekitMdnsRecord {
  type?: string;
  name?: string;
  host?: string;
  hostname?: string;
  addresses?: string[];
  port?: number;
  txt?: Record<string, unknown>;
  properties?: Record<string, unknown>;
}

export interface IHomekitManualEntry {
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  setupCode?: string;
  pairingData?: IHomekitPairingData;
  accessories?: IHomekitAccessory[];
  snapshot?: IHomekitControllerSnapshot;
  model?: string;
  manufacturer?: string;
  category?: string | number;
  metadata?: Record<string, unknown>;
}

export interface IHomekitControllerSnapshot {
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  paired?: boolean;
  connected: boolean;
  transport?: THomekitTransport;
  configNumber?: number;
  stateNumber?: number;
  pairingData?: IHomekitPairingData;
  discovery?: IHomekitDiscoveryRecord;
  accessories: IHomekitAccessory[];
  events?: IHomekitEvent[];
}

export interface IHomekitAccessory {
  aid: number;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  firmwareRevision?: string;
  hardwareRevision?: string;
  services?: IHomekitService[] | Record<string, IHomekitService>;
  [key: string]: unknown;
}

export interface IHomekitService {
  iid: number;
  type: string;
  name?: string;
  primary?: boolean;
  hidden?: boolean;
  characteristics?: IHomekitCharacteristic[] | Record<string, IHomekitCharacteristic>;
  characteristicsByType?: Record<string, IHomekitCharacteristic>;
  characteristics_by_type?: Record<string, IHomekitCharacteristic>;
  [key: string]: unknown;
}

export interface IHomekitCharacteristic {
  iid: number;
  type: string;
  value?: unknown;
  description?: string;
  name?: string;
  perms?: string[];
  format?: string;
  unit?: string;
  minValue?: number;
  maxValue?: number;
  minStep?: number;
  maxLen?: number;
  validValues?: unknown[];
  available?: boolean;
  [key: string]: unknown;
}

export interface IHomekitCharacteristicReference {
  aid: number;
  iid: number;
  type?: string;
  serviceIid?: number;
  serviceType?: string;
}

export interface IHomekitCharacteristicWrite extends IHomekitCharacteristicReference {
  value: unknown;
}

export interface IHomekitControllerCommand {
  command: THomekitControllerCommandType;
  reads?: IHomekitCharacteristicReference[];
  writes?: IHomekitCharacteristicWrite[];
  aid?: number;
  width?: number;
  height?: number;
}

export interface IHomekitEvent {
  type: 'snapshot' | 'characteristic_updated' | 'availability_changed' | 'config_changed' | 'unsupported' | 'error';
  aid?: number;
  iid?: number;
  value?: unknown;
  data?: unknown;
  timestamp: number;
}
