export type TYeelightPowerState = 'on' | 'off';
export type TYeelightEffect = 'smooth' | 'sudden';
export type TYeelightCommandMethod =
  | 'get_prop'
  | 'set_power'
  | 'set_bright'
  | 'set_ct_abx'
  | 'set_rgb'
  | 'set_hsv'
  | 'set_scene'
  | 'start_cf'
  | 'stop_cf'
  | 'set_default'
  | 'set_name'
  | 'toggle'
  | string;

export interface IYeelightConfig {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  model?: string;
  detectedModel?: string;
  detected_model?: string;
  transition?: number;
  effect?: TYeelightEffect;
  snapshot?: IYeelightSnapshot;
  bulb?: IYeelightBulbInfo;
  bulbs?: IYeelightBulbInfo[];
  devices?: Record<string, IYeelightManualDeviceConfig>;
  commandExecutor?: TYeelightCommandExecutor;
  [key: string]: unknown;
}

export interface IHomeAssistantYeelightConfig extends IYeelightConfig {}

export interface IYeelightManualDeviceConfig {
  id?: string;
  name?: string;
  model?: string;
  detectedModel?: string;
  detected_model?: string;
  port?: number;
  properties?: IYeelightBulbProperties;
  capabilities?: IYeelightCapabilities;
  [key: string]: unknown;
}

export interface IYeelightCapabilities {
  id?: string;
  location?: string;
  model?: string;
  fw_ver?: string;
  support?: string;
  power?: string;
  bright?: string;
  color_mode?: string;
  ct?: string;
  rgb?: string;
  hue?: string;
  sat?: string;
  name?: string;
  [key: string]: string | undefined;
}

export interface IYeelightBulbProperties {
  power?: string | null;
  main_power?: string | null;
  bright?: string | null;
  ct?: string | null;
  rgb?: string | null;
  hue?: string | null;
  sat?: string | null;
  color_mode?: string | null;
  flowing?: string | null;
  delayoff?: string | null;
  music_on?: string | null;
  name?: string | null;
  bg_power?: string | null;
  bg_flowing?: string | null;
  bg_ct?: string | null;
  bg_bright?: string | null;
  bg_hue?: string | null;
  bg_sat?: string | null;
  bg_rgb?: string | null;
  bg_lmode?: string | null;
  nl_br?: string | null;
  active_mode?: string | null;
  current_brightness?: string | null;
  [key: string]: string | number | boolean | null | undefined;
}

export interface IYeelightBulbState {
  on: boolean;
  brightness?: number;
  colorTempKelvin?: number;
  rgbColor?: [number, number, number];
  hsColor?: [number, number];
  colorMode?: string;
  flowing?: boolean;
  nightLight?: boolean;
}

export interface IYeelightBulbInfo {
  id?: string;
  host?: string;
  port?: number;
  name?: string;
  model?: string;
  fwVersion?: string;
  support?: string[];
  capabilities?: IYeelightCapabilities;
  properties?: IYeelightBulbProperties;
  state?: IYeelightBulbState;
  available?: boolean;
  metadata?: Record<string, unknown>;
}

export interface IYeelightSnapshot {
  connected: boolean;
  bulbs: IYeelightBulbInfo[];
  events: IYeelightEvent[];
  source?: 'snapshot' | 'manual' | 'tcp';
  updatedAt?: string;
}

export interface IYeelightCommand {
  id?: number;
  method: TYeelightCommandMethod;
  params?: unknown[];
  host?: string;
  port?: number;
}

export interface IYeelightCommandResponse {
  id?: number;
  result?: unknown[];
  error?: unknown;
  method?: string;
  params?: Record<string, unknown>;
}

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

export interface IYeelightLightStatePatch {
  on?: boolean;
  brightness?: number;
  percentage?: number;
  colorTempKelvin?: number;
  rgbColor?: [number, number, number];
  transition?: number;
  effect?: TYeelightEffect;
}

export type TYeelightCommandExecutor = (
  commandArg: IYeelightCommand
) => Promise<IYeelightCommandResponse | IYeelightCommandResult | unknown> | IYeelightCommandResponse | IYeelightCommandResult | unknown;

export interface IYeelightEvent {
  type: 'properties' | 'command' | 'error';
  timestamp?: number;
  bulbId?: string;
  host?: string;
  entityId?: string;
  command?: IYeelightCommand;
  data?: unknown;
  error?: string;
  [key: string]: unknown;
}

export interface IYeelightSsdpRecord {
  location?: string;
  Location?: string;
  id?: string;
  Id?: string;
  model?: string;
  Model?: string;
  support?: string;
  Support?: string;
  st?: string;
  ST?: string;
  usn?: string;
  USN?: string;
  server?: string;
  Server?: string;
  ssdp_headers?: Record<string, string | undefined>;
  headers?: Record<string, string | undefined>;
  [key: string]: unknown;
}

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

export interface IYeelightManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  model?: string;
  manufacturer?: string;
  properties?: IYeelightBulbProperties;
  capabilities?: IYeelightCapabilities;
  snapshot?: IYeelightSnapshot;
  metadata?: Record<string, unknown>;
}
