export type TBoschShcSnapshotSource = 'snapshot' | 'executor';

export type TBoschShcEntityPlatform =
  | 'sensor'
  | 'binary_sensor'
  | 'switch'
  | 'light'
  | 'climate'
  | 'cover';

export type TBoschShcCommandAction =
  | 'read_snapshot'
  | 'get_public_information'
  | 'put_device_service_state'
  | 'post_domain_action'
  | 'pair_client';

export type TBoschShcCommandMethod = 'GET' | 'PUT' | 'POST';

export type TBoschShcJsonValue =
  | string
  | number
  | boolean
  | null
  | TBoschShcJsonValue[]
  | { [key: string]: TBoschShcJsonValue | undefined };

export interface IBoschShcSoftwareUpdateState {
  swInstalledVersion?: string;
  swUpdateState?: string;
  [key: string]: unknown;
}

export interface IBoschShcPublicInformation {
  '@type'?: string;
  shcIpAddress?: string;
  macAddress?: string;
  softwareUpdateState?: IBoschShcSoftwareUpdateState;
  [key: string]: unknown;
}

export interface IBoschShcDevice {
  '@type'?: 'device' | string;
  rootDeviceId?: string;
  id: string;
  deviceServiceIds?: string[];
  manufacturer?: string;
  roomId?: string;
  deviceModel?: string;
  serial?: string;
  profile?: string;
  name?: string;
  status?: string;
  parentDeviceId?: string;
  childDeviceIds?: string[];
  deleted?: boolean;
  services?: IBoschShcDeviceService[];
  [key: string]: unknown;
}

export interface IBoschShcDeviceService {
  '@type'?: 'DeviceServiceData' | string;
  id: string;
  deviceId: string;
  path?: string;
  state?: Record<string, unknown>;
  faults?: {
    entries?: Array<{
      type?: string;
      [key: string]: unknown;
    }>;
    [key: string]: unknown;
  };
  [key: string]: unknown;
}

export interface IBoschShcRoom {
  id: string;
  name?: string;
  iconId?: string;
  [key: string]: unknown;
}

export interface IBoschShcIntrusionState {
  '@type'?: 'systemState' | string;
  systemAvailability?: { available?: boolean; [key: string]: unknown };
  armingState?: { state?: string; remainingTimeUntilArmed?: number; [key: string]: unknown };
  alarmState?: { value?: string; incidents?: unknown[]; [key: string]: unknown };
  activeConfigurationProfile?: { profileId?: string | number; [key: string]: unknown };
  securityGapState?: { securityGaps?: unknown[]; [key: string]: unknown };
  [key: string]: unknown;
}

export interface IBoschShcSnapshot {
  information?: IBoschShcPublicInformation;
  authenticatedInformation?: Record<string, unknown>;
  devices: IBoschShcDevice[];
  services?: IBoschShcDeviceService[];
  rooms?: IBoschShcRoom[];
  intrusionDetection?: IBoschShcIntrusionState;
  host?: string;
  name?: string;
  uniqueId?: string;
  online?: boolean;
  source?: TBoschShcSnapshotSource;
  updatedAt?: string;
}

export interface IBoschShcFeatureBase {
  platform: TBoschShcEntityPlatform;
  id: string;
  alias: string;
  uniqueId: string;
  name: string;
  deviceId: string;
  device: IBoschShcDevice;
  available: boolean;
}

export interface IBoschShcSensorFeature extends IBoschShcFeatureBase {
  platform: 'sensor';
  nativeValue: string | number | null;
  unit?: string;
  deviceClass?: string;
  stateClass?: string;
  attributes?: Record<string, unknown>;
}

export interface IBoschShcBinarySensorFeature extends IBoschShcFeatureBase {
  platform: 'binary_sensor';
  isOn: boolean;
  deviceClass?: 'battery' | 'door' | 'window' | 'moisture' | string;
  attributes?: Record<string, unknown>;
}

export type TBoschShcSwitchKind = 'power_switch' | 'routing' | 'camera_light' | 'privacy_mode';

export interface IBoschShcSwitchFeature extends IBoschShcFeatureBase {
  platform: 'switch';
  kind: TBoschShcSwitchKind;
  isOn: boolean | null;
  deviceClass?: 'outlet' | 'switch' | string;
  serviceId: string;
  stateKey: string;
  onValue: unknown;
  offValue: unknown;
  entityCategory?: 'config';
}

export interface IBoschShcLightFeature extends IBoschShcFeatureBase {
  platform: 'light';
  isOn: boolean | null;
  switchServiceId: 'BinarySwitch' | 'PowerSwitch';
  brightness?: number | null;
  colorTemperature?: number | null;
  minColorTemperature?: number | null;
  maxColorTemperature?: number | null;
  rgb?: number | null;
  supportsBrightness: boolean;
  supportsColorTemperature: boolean;
  supportsRgb: boolean;
}

export type TBoschShcCoverState = 'opening' | 'closing' | 'open' | 'closed' | 'stopped' | 'unknown';

export interface IBoschShcCoverFeature extends IBoschShcFeatureBase {
  platform: 'cover';
  serviceId: 'ShutterControl';
  deviceClass: 'shutter' | 'awning' | 'blind';
  state: TBoschShcCoverState;
  position: number | null;
  operationState?: string;
  calibrated?: boolean;
  supportsOpen: boolean;
  supportsClose: boolean;
  supportsStop: boolean;
  supportsPosition: boolean;
}

export interface IBoschShcClimateFeature extends IBoschShcFeatureBase {
  platform: 'climate';
  serviceId: 'RoomClimateControl' | 'HeatingCircuit';
  hvacMode: 'heat' | 'off' | 'unknown';
  currentTemperature?: number | null;
  targetTemperature?: number | null;
  operationMode?: string;
  boostMode?: boolean;
  low?: boolean;
  summerMode?: boolean;
  supportsBoostMode?: boolean;
  supportedHvacModes: Array<'heat' | 'off'>;
}

export type TBoschShcFeature =
  | IBoschShcSensorFeature
  | IBoschShcBinarySensorFeature
  | IBoschShcSwitchFeature
  | IBoschShcLightFeature
  | IBoschShcCoverFeature
  | IBoschShcClimateFeature;

export interface IBoschShcModeledCommand {
  action: TBoschShcCommandAction;
  method: TBoschShcCommandMethod;
  path: string;
  body?: Record<string, unknown> | null;
  deviceId?: string;
  serviceId?: string;
  domain?: string;
  service?: string;
  reason?: string;
  sensitiveFields?: string[];
}

export interface IBoschShcCommandContext {
  config: IBoschShcConfig;
  snapshot?: IBoschShcSnapshot;
}

export type TBoschShcCommandExecutor =
  | ((commandArg: IBoschShcModeledCommand, contextArg: IBoschShcCommandContext) => Promise<unknown> | unknown)
  | {
      execute(commandArg: IBoschShcModeledCommand, contextArg: IBoschShcCommandContext): Promise<unknown> | unknown;
    };

export interface IBoschShcPairClientRequest {
  systemPassword: string;
  clientId?: string;
  clientName?: string;
  certificatePem?: string;
}

export interface IBoschShcConfig {
  host?: string;
  apiPort?: number;
  publicPort?: number;
  pairingPort?: number;
  sslCertificate?: string;
  sslKey?: string;
  token?: string;
  hostname?: string;
  uniqueId?: string;
  name?: string;
  timeoutMs?: number;
  snapshot?: IBoschShcSnapshot;
  executor?: TBoschShcCommandExecutor;
}

export interface IBoschShcMdnsRecord {
  type?: string;
  name?: string;
  host?: string;
  port?: number;
  hostname?: string;
  txt?: Record<string, unknown>;
  properties?: Record<string, unknown>;
  metadata?: Record<string, unknown>;
}

export interface IBoschShcManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  macAddress?: string;
  metadata?: Record<string, unknown>;
}

export interface IHomeAssistantBoschShcConfig extends IBoschShcConfig {}
