import type { IServiceCallRequest } from '../../core/types.js';

export const homeWizardDomain = 'homewizard';
export const homeWizardDisplayName = 'HomeWizard';
export const homeWizardDefaultHttpPort = 80;
export const homeWizardDefaultHttpsPort = 443;
export const homeWizardDefaultTimeoutMs = 5000;
export const homeWizardZeroconfTypes = ['_hwenergy._tcp.local.', '_homewizard._tcp.local.'] as const;

export const homeWizardProductTypes = {
  p1Meter: 'HWE-P1',
  energySocket: 'HWE-SKT',
  waterMeter: 'HWE-WTR',
  display: 'HWE-DSP',
  energyMeter1Phase: 'HWE-KWH1',
  energyMeter3Phase: 'HWE-KWH3',
  eastronSdm230: 'SDM230-wifi',
  eastronSdm630: 'SDM630-wifi',
  battery: 'HWE-BAT',
} as const;

export const homeWizardProductNames: Record<string, string> = {
  [homeWizardProductTypes.p1Meter]: 'Wi-Fi P1 Meter',
  [homeWizardProductTypes.energySocket]: 'Wi-Fi Energy Socket',
  [homeWizardProductTypes.waterMeter]: 'Wi-Fi Watermeter',
  [homeWizardProductTypes.display]: 'Energy Display',
  [homeWizardProductTypes.energyMeter1Phase]: 'Wi-Fi kWh Meter 1-phase',
  [homeWizardProductTypes.energyMeter3Phase]: 'Wi-Fi kWh Meter 3-phase',
  [homeWizardProductTypes.eastronSdm230]: 'Wi-Fi kWh Meter 1-phase',
  [homeWizardProductTypes.eastronSdm630]: 'Wi-Fi kWh Meter 3-phase',
  [homeWizardProductTypes.battery]: 'Plug-In Battery',
};

export const homeWizardVerificationIds: Record<string, string> = {
  [homeWizardProductTypes.p1Meter]: 'p1dongle',
  [homeWizardProductTypes.energySocket]: 'energysocket',
  [homeWizardProductTypes.waterMeter]: 'watermeter',
  [homeWizardProductTypes.display]: 'display',
  [homeWizardProductTypes.energyMeter1Phase]: 'energymeter',
  [homeWizardProductTypes.energyMeter3Phase]: 'energymeter',
  [homeWizardProductTypes.eastronSdm230]: 'energymeter',
  [homeWizardProductTypes.eastronSdm630]: 'energymeter',
  [homeWizardProductTypes.battery]: 'battery',
};

export type THomeWizardApiVersion = 'auto' | 'v1' | 'v2';
export type THomeWizardSnapshotSource = 'http' | 'client' | 'manual' | 'snapshot' | 'runtime';
export type THomeWizardProtocol = 'http' | 'https';

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

export interface IHomeWizardDhcpRecord {
  hostname?: string;
  hostName?: string;
  ip?: string;
  address?: string;
  macaddress?: string;
  macAddress?: string;
  registered_devices?: boolean;
  registeredDevices?: boolean;
  product_type?: string;
  productType?: string;
  product_name?: string;
  productName?: string;
  serial?: string;
  metadata?: Record<string, unknown>;
}

export interface IHomeWizardManualEntry {
  id?: string;
  uniqueId?: string;
  host?: string;
  url?: string;
  port?: number;
  protocol?: THomeWizardProtocol;
  apiVersion?: THomeWizardApiVersion;
  token?: string;
  authorizationToken?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  productType?: string;
  productName?: string;
  serial?: string;
  snapshot?: IHomeWizardSnapshot;
  rawData?: Partial<IHomeWizardRawData>;
  client?: IHomeWizardClientLike;
  commandExecutor?: IHomeWizardCommandExecutor;
  metadata?: Record<string, unknown>;
}

export interface IHomeWizardDeviceSnapshot {
  id: string;
  productName: string;
  productType: string;
  serial: string;
  firmwareVersion?: string;
  apiVersion: string;
  modelName?: string;
  verificationId?: string;
}

export interface IHomeWizardExternalDevice {
  id: string;
  uniqueId: string;
  type?: string;
  value?: number | string;
  unit?: string;
  timestamp?: string | number;
}

export interface IHomeWizardMeasurementSnapshot extends Record<string, unknown> {
  externalDevices?: IHomeWizardExternalDevice[];
}

export interface IHomeWizardSystemSnapshot extends Record<string, unknown> {}
export interface IHomeWizardStateSnapshot extends Record<string, unknown> {}
export interface IHomeWizardBatteriesSnapshot extends Record<string, unknown> {}

export interface IHomeWizardCapabilities {
  localControl: boolean;
  apiV1: boolean;
  apiV2: boolean;
  powerSwitch: boolean;
  switchLock: boolean;
  cloudConnection: boolean;
  statusLedBrightness: boolean;
  identify: boolean;
  reboot: boolean;
  batteries: boolean;
  telegram: boolean;
}

export interface IHomeWizardRawData {
  device?: Record<string, unknown>;
  measurement?: Record<string, unknown>;
  system?: Record<string, unknown>;
  state?: Record<string, unknown>;
  batteries?: Record<string, unknown>;
  telegram?: string;
  errors?: Record<string, string>;
  fetchedAt?: string;
}

export interface IHomeWizardSnapshot {
  device: IHomeWizardDeviceSnapshot;
  host?: string;
  port?: number;
  protocol?: THomeWizardProtocol;
  apiVersion: THomeWizardApiVersion;
  measurement: IHomeWizardMeasurementSnapshot;
  system?: IHomeWizardSystemSnapshot;
  state?: IHomeWizardStateSnapshot;
  batteries?: IHomeWizardBatteriesSnapshot;
  externalDevices: IHomeWizardExternalDevice[];
  capabilities: IHomeWizardCapabilities;
  rawData?: IHomeWizardRawData;
  online: boolean;
  updatedAt: string;
  source: THomeWizardSnapshotSource;
  error?: string;
}

export interface IHomeWizardCommandExecutor {
  execute(commandArg: IHomeWizardCommandRequest): Promise<unknown>;
}

export interface IHomeWizardClientLike {
  getSnapshot?(): Promise<IHomeWizardSnapshot | Partial<IHomeWizardRawData>>;
  getRawData?(): Promise<Partial<IHomeWizardRawData>>;
  execute?(commandArg: IHomeWizardCommandRequest): Promise<unknown>;
  destroy?(): Promise<void>;
}

export interface IHomeWizardConfig {
  host?: string;
  url?: string;
  port?: number;
  protocol?: THomeWizardProtocol;
  apiVersion?: THomeWizardApiVersion;
  token?: string;
  authorizationToken?: string;
  identifier?: string;
  verifyTls?: boolean;
  timeoutMs?: number;
  name?: string;
  productName?: string;
  productType?: string;
  serial?: string;
  uniqueId?: string;
  online?: boolean;
  snapshot?: IHomeWizardSnapshot;
  rawData?: Partial<IHomeWizardRawData>;
  client?: IHomeWizardClientLike;
  commandExecutor?: IHomeWizardCommandExecutor;
}

export interface IHomeAssistantHomewizardConfig extends IHomeWizardConfig {}

export type THomeWizardCommandAction =
  | 'refresh'
  | 'request_token'
  | 'set_state'
  | 'set_system'
  | 'set_battery_mode'
  | 'identify'
  | 'reboot'
  | 'telegram';

export interface IHomeWizardCommandRequest {
  action: THomeWizardCommandAction;
  powerOn?: boolean;
  switchLock?: boolean;
  brightness?: number;
  cloudEnabled?: boolean;
  statusLedBrightnessPct?: number;
  apiV1Enabled?: boolean;
  batteryMode?: string;
  tokenName?: string;
  service?: string;
  target?: IServiceCallRequest['target'];
  data?: Record<string, unknown>;
}

export interface IHomeWizardRefreshResult {
  success: boolean;
  snapshot?: IHomeWizardSnapshot;
  error?: string;
  data?: unknown;
}
