export type TBraviatvPowerStatus = 'active' | 'standby' | 'off' | 'unknown' | string;

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

export type TBraviatvPlaybackState = 'playing' | 'paused' | 'idle' | 'off' | 'unknown';

export type TBraviatvSourceType = 'input' | 'app' | 'channel';

export type TBraviatvRestService = 'accessControl' | 'appControl' | 'audio' | 'avContent' | 'guide' | 'system' | 'video' | 'videoScreen';

export interface IBraviatvConfig {
  host?: string;
  port?: number;
  useSsl?: boolean;
  psk?: string;
  pin?: string;
  usePsk?: boolean;
  clientId?: string;
  nickname?: string;
  name?: string;
  model?: string;
  manufacturer?: string;
  macAddress?: string;
  serialNumber?: string;
  cid?: string;
  timeoutMs?: number;
  irccEndpoint?: 'ircc' | 'IRCC' | string;
  fetchChannels?: boolean;
  systemInfo?: IBraviatvSystemInfo;
  state?: IBraviatvState;
  sources?: IBraviatvSource[];
  apps?: IBraviatvApp[];
  channels?: IBraviatvChannel[];
  commands?: Record<string, string>;
  snapshot?: IBraviatvSnapshot;
  scalarWebApiBaseUrl?: string;
}

export interface IHomeAssistantBraviatvConfig extends IBraviatvConfig {}

export interface IBraviatvSystemInfo {
  product?: string;
  region?: string;
  language?: string;
  model?: string;
  serial?: string;
  macAddr?: string;
  name?: string;
  generation?: string;
  area?: string;
  cid?: string;
  [key: string]: unknown;
}

export interface IBraviatvVolumeInfo {
  target?: string;
  volume?: number | string;
  minVolume?: number;
  maxVolume?: number;
  mute?: boolean;
  [key: string]: unknown;
}

export interface IBraviatvPlayingInfo {
  title?: string;
  uri?: string;
  source?: string;
  programTitle?: string;
  dispNum?: string;
  durationSec?: number;
  startDateTime?: string;
  mediaType?: string;
  [key: string]: unknown;
}

export interface IBraviatvSource {
  title: string;
  uri: string;
  type?: TBraviatvSourceType;
  icon?: string;
  label?: string;
  connection?: string;
  status?: string;
  dispNum?: string;
  [key: string]: unknown;
}

export interface IBraviatvApp extends IBraviatvSource {
  type?: 'app';
}

export interface IBraviatvChannel extends IBraviatvSource {
  type?: 'channel';
  dispNum?: string;
}

export interface IBraviatvState {
  powerStatus?: TBraviatvPowerStatus;
  power?: TBraviatvPowerState;
  playback?: TBraviatvPlaybackState;
  available?: boolean;
  source?: string;
  sourceUri?: string;
  appTitle?: string;
  volumeLevel?: number;
  volumePercent?: number;
  muted?: boolean;
  mediaTitle?: string;
  mediaChannel?: string;
  mediaContentId?: string;
  mediaContentType?: string;
  mediaDuration?: number;
  mediaPosition?: number;
  mediaPositionUpdatedAt?: string;
  lastError?: string;
}

export interface IBraviatvSnapshot {
  systemInfo: IBraviatvSystemInfo;
  state: IBraviatvState;
  sources: IBraviatvSource[];
  apps: IBraviatvApp[];
  channels?: IBraviatvChannel[];
  commands?: Record<string, string>;
  updatedAt?: string;
}

export interface IBraviatvRestCommand {
  type: 'rest';
  service: TBraviatvRestService;
  method: string;
  params?: unknown[];
  version?: string;
}

export interface IBraviatvIrccCommand {
  type: 'ircc';
  command?: string;
  code?: string;
}

export interface IBraviatvSourceCommand {
  type: 'source';
  source: string;
}

export type TBraviatvCommand = IBraviatvRestCommand | IBraviatvIrccCommand | IBraviatvSourceCommand;

export interface IBraviatvEvent {
  type: 'snapshot' | 'command' | 'error';
  command?: TBraviatvCommand;
  snapshot?: IBraviatvSnapshot;
  message?: string;
  timestamp: number;
}

export interface IBraviatvSsdpRecord {
  st?: string;
  usn?: string;
  location?: string;
  headers?: Record<string, string | undefined>;
  upnp?: Record<string, unknown>;
  ssdp_st?: string;
  ssdp_usn?: string;
  ssdp_location?: string;
  [key: string]: unknown;
}

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

export interface IBraviatvManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  model?: string;
  manufacturer?: string;
  macAddress?: string;
  serialNumber?: string;
  cid?: string;
  psk?: string;
  useSsl?: boolean;
  metadata?: Record<string, unknown>;
}

export type TBraviatvDiscoveryRecord = IBraviatvSsdpRecord | IBraviatvMdnsRecord | IBraviatvManualEntry;
