export type TSamsungtvProtocolMethod = 'websocket' | 'legacy' | 'encrypted';

export type TSamsungtvPowerState = 'on' | 'off' | 'unknown';

export type TSamsungtvRemoteKey = `KEY_${string}` | string;

export type TSamsungtvCommandAction = 'Click' | 'Press' | 'Release';

export interface ISamsungtvConfig {
  host?: string;
  port?: number;
  token?: string;
  name?: string;
  model?: string;
  manufacturer?: string;
  macAddress?: string;
  method?: TSamsungtvProtocolMethod;
  sessionId?: string;
  deviceInfo?: ISamsungtvDeviceInfoResponse;
  state?: ISamsungtvState;
  apps?: ISamsungtvApp[];
  activeApp?: ISamsungtvApp;
  snapshot?: ISamsungtvSnapshot;
  ssdpRenderingControlLocation?: string;
  ssdpMainTvAgentLocation?: string;
  websocketName?: string;
  keyPressDelayMs?: number;
  connectTimeoutMs?: number;
}

export interface IHomeAssistantSamsungtvConfig extends ISamsungtvConfig {}

export interface ISamsungtvDeviceInfoResponse {
  id?: string;
  device?: ISamsungtvDeviceInfo;
  [key: string]: unknown;
}

export interface ISamsungtvDeviceInfo {
  type?: string;
  name?: string;
  modelName?: string;
  modelNumber?: string;
  serialNumber?: string;
  udn?: string;
  wifiMac?: string;
  manufacturer?: string;
  networkType?: string;
  PowerState?: string;
  FrameTVSupport?: string;
  [key: string]: unknown;
}

export interface ISamsungtvState {
  power?: TSamsungtvPowerState;
  playback?: 'playing' | 'paused' | 'idle' | 'off' | 'unknown';
  volumeLevel?: number;
  muted?: boolean;
  source?: string;
  appId?: string;
  appName?: string;
  mediaTitle?: string;
}

export interface ISamsungtvApp {
  id: string;
  name: string;
  type?: string;
  appType?: string;
  version?: string;
}

export interface ISamsungtvSnapshot {
  deviceInfo: ISamsungtvDeviceInfoResponse;
  state?: ISamsungtvState;
  apps: ISamsungtvApp[];
  activeApp?: ISamsungtvApp;
}

export interface ISamsungtvKeyCommand {
  type: 'key';
  key: TSamsungtvRemoteKey;
  action?: TSamsungtvCommandAction;
}

export interface ISamsungtvLaunchAppCommand {
  type: 'launch_app';
  appId: string;
  appType?: 'DEEP_LINK' | 'NATIVE_LAUNCH' | string;
  metaTag?: string;
}

export interface ISamsungtvWebsocketCommand {
  method: string;
  params: Record<string, unknown>;
}

export type TSamsungtvCommand = ISamsungtvKeyCommand | ISamsungtvLaunchAppCommand | ISamsungtvWebsocketCommand;

export interface ISamsungtvEvent {
  type: 'websocket' | 'state' | 'apps' | 'error';
  event?: string;
  data?: unknown;
  token?: string;
  timestamp: number;
}

export interface ISamsungtvSsdpRecord {
  st?: string;
  usn?: string;
  location?: string;
  headers?: Record<string, string | undefined>;
  upnp?: Record<string, string | undefined>;
  ssdp_st?: string;
  ssdp_usn?: string;
  ssdp_location?: string;
}

export interface ISamsungtvMdnsRecord {
  type?: string;
  name?: string;
  host?: string;
  port?: number;
  txt?: Record<string, string | undefined>;
  properties?: Record<string, string | undefined>;
}

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

export type TSamsungtvDiscoveryRecord = ISamsungtvSsdpRecord | ISamsungtvMdnsRecord | ISamsungtvManualEntry;
