export const hunterDouglasPowerViewDomain = 'hunterdouglas_powerview';
export const hunterDouglasPowerViewDisplayName = 'Hunter Douglas PowerView';
export const hunterDouglasPowerViewManufacturer = 'Hunter Douglas';
export const hunterDouglasPowerViewDefaultPort = 80;
export const hunterDouglasPowerViewDefaultTimeoutMs = 15000;
export const hunterDouglasPowerViewZeroconfTypes = ['_powerview._tcp.local.', '_PowerView-G3._tcp.local.'] as const;
export const hunterDouglasPowerViewDhcpHostnamePrefix = 'hunter';
export const hunterDouglasPowerViewDhcpMacPrefix = '002674';

export type THunterDouglasPowerViewSnapshotSource = 'http' | 'client' | 'manual' | 'snapshot' | 'runtime' | 'executor';

export type THunterDouglasPowerViewCommandAction =
  | 'move_shade'
  | 'stop_shade'
  | 'activate_scene'
  | 'refresh'
  | 'raw_request';

export interface IHunterDouglasPowerViewVersionInfo {
  revision?: number;
  subRevision?: number;
  build?: number;
  name?: string;
  [key: string]: unknown;
}

export interface IHunterDouglasPowerViewHubRawData {
  userData?: Record<string, unknown>;
  config?: Record<string, unknown>;
  firmware?: Record<string, unknown>;
  fwVersion?: string;
  gateways?: Array<Record<string, unknown>>;
  [key: string]: unknown;
}

export interface IHunterDouglasPowerViewRoomRaw {
  id?: number | string;
  name?: string;
  ptName?: string;
  room?: IHunterDouglasPowerViewRoomRaw;
  [key: string]: unknown;
}

export interface IHunterDouglasPowerViewSceneRaw {
  id?: number | string;
  name?: string;
  ptName?: string;
  roomId?: number | string;
  roomIds?: Array<number | string>;
  scene?: IHunterDouglasPowerViewSceneRaw;
  [key: string]: unknown;
}

export interface IHunterDouglasPowerViewShadeRaw {
  id?: number | string;
  name?: string;
  ptName?: string;
  roomId?: number | string;
  roomIds?: Array<number | string>;
  type?: number | string;
  capabilities?: number | string;
  positions?: Record<string, unknown>;
  firmware?: IHunterDouglasPowerViewVersionInfo;
  batteryKind?: number;
  batteryStrength?: number;
  batteryStatus?: number;
  powerType?: number;
  signalStrength?: number;
  shade?: IHunterDouglasPowerViewShadeRaw;
  [key: string]: unknown;
}

export interface IHunterDouglasPowerViewRawData {
  apiVersion?: number;
  hub?: IHunterDouglasPowerViewHubRawData;
  home?: IHunterDouglasPowerViewHubRawData;
  rooms?: IHunterDouglasPowerViewRoomRaw[];
  scenes?: IHunterDouglasPowerViewSceneRaw[];
  shades?: IHunterDouglasPowerViewShadeRaw[];
  fetchedAt?: string;
}

export interface IHunterDouglasPowerViewHubSnapshot {
  id: string;
  name: string;
  host?: string;
  port?: number;
  manufacturer: string;
  model?: string;
  firmware?: string;
  hardware?: string;
  apiVersion: number;
  role: 'Primary' | 'Secondary' | 'Unknown';
  macAddress?: string;
  serialNumber?: string;
  url?: string;
  rawData?: IHunterDouglasPowerViewHubRawData;
}

export interface IHunterDouglasPowerViewRoomSnapshot {
  id: string;
  name: string;
  rawData: IHunterDouglasPowerViewRoomRaw;
}

export interface IHunterDouglasPowerViewSceneSnapshot {
  sceneId: string;
  name: string;
  roomId?: string;
  roomName?: string;
  rawData: IHunterDouglasPowerViewSceneRaw;
}

export interface IHunterDouglasPowerViewShadePosition {
  primary?: number;
  secondary?: number;
  tilt?: number;
  velocity?: number;
}

export interface IHunterDouglasPowerViewShadeCapabilities {
  primary: boolean;
  secondary: boolean;
  tilt: boolean;
  stop: boolean;
  topDown: boolean;
  tiltOnly: boolean;
  dualRail: boolean;
  dualOverlapped: boolean;
  light: boolean;
}

export interface IHunterDouglasPowerViewShadeSnapshot {
  shadeId: string;
  name: string;
  roomId?: string;
  roomName?: string;
  type?: number | string;
  typeName?: string;
  capabilityType?: number | string;
  capabilities: IHunterDouglasPowerViewShadeCapabilities;
  positions: IHunterDouglasPowerViewShadePosition;
  firmware?: string;
  batteryPercent?: number;
  powerSource?: string;
  signalStrength?: number;
  rawData: IHunterDouglasPowerViewShadeRaw;
}

export interface IHunterDouglasPowerViewCapabilities {
  localControl: boolean;
  covers: boolean;
  scenes: boolean;
  rooms: boolean;
  tilt: boolean;
  stop: boolean;
}

export interface IHunterDouglasPowerViewSnapshot {
  hub: IHunterDouglasPowerViewHubSnapshot;
  rooms: IHunterDouglasPowerViewRoomSnapshot[];
  scenes: IHunterDouglasPowerViewSceneSnapshot[];
  shades: IHunterDouglasPowerViewShadeSnapshot[];
  capabilities: IHunterDouglasPowerViewCapabilities;
  rawData?: IHunterDouglasPowerViewRawData;
  online: boolean;
  updatedAt: string;
  source: THunterDouglasPowerViewSnapshotSource;
  error?: string;
}

export interface IHunterDouglasPowerViewCommandRequest {
  action: THunterDouglasPowerViewCommandAction;
  shadeId?: string;
  sceneId?: string;
  positions?: IHunterDouglasPowerViewShadePosition;
  path?: string;
  method?: 'GET' | 'PUT' | 'POST' | 'DELETE';
  body?: unknown;
  query?: Record<string, string | number | boolean | undefined>;
  apiVersion?: number;
  service?: string;
  target?: Record<string, unknown>;
  data?: Record<string, unknown>;
}

export interface IHunterDouglasPowerViewCommandExecutor {
  execute(requestArg: IHunterDouglasPowerViewCommandRequest): Promise<unknown>;
}

export interface IHunterDouglasPowerViewClientLike {
  getSnapshot?: () => Promise<IHunterDouglasPowerViewSnapshot | Partial<IHunterDouglasPowerViewRawData>>;
  getRawData?: () => Promise<Partial<IHunterDouglasPowerViewRawData>>;
  execute?: (requestArg: IHunterDouglasPowerViewCommandRequest) => Promise<unknown>;
  destroy?: () => Promise<void>;
}

export interface IHunterDouglasPowerViewConfig {
  host?: string;
  url?: string;
  port?: number;
  apiVersion?: number;
  timeoutMs?: number;
  name?: string;
  manufacturer?: string;
  model?: string;
  uniqueId?: string;
  serialNumber?: string;
  macAddress?: string;
  online?: boolean;
  snapshot?: IHunterDouglasPowerViewSnapshot;
  rawData?: Partial<IHunterDouglasPowerViewRawData>;
  client?: IHunterDouglasPowerViewClientLike;
  commandExecutor?: IHunterDouglasPowerViewCommandExecutor;
}

export interface IHomeAssistantHunterdouglasPowerviewConfig extends IHunterDouglasPowerViewConfig {}

export interface IHunterDouglasPowerViewManualEntry extends IHunterDouglasPowerViewConfig {
  id?: string;
  metadata?: Record<string, unknown>;
}

export interface IHunterDouglasPowerViewZeroconfRecord {
  type?: string;
  serviceType?: string;
  name?: 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 IHunterDouglasPowerViewDhcpRecord {
  hostname?: string;
  hostName?: string;
  ip?: string;
  address?: string;
  macaddress?: string;
  macAddress?: string;
  registeredDevices?: boolean;
  registered_devices?: boolean;
  manufacturer?: string;
  model?: string;
  metadata?: Record<string, unknown>;
}

export interface IHunterDouglasPowerViewRefreshResult {
  success: boolean;
  snapshot?: IHunterDouglasPowerViewSnapshot;
  error?: string;
  data?: unknown;
}
