import type { ISimpleLocalConfig, ISimpleLocalIntegrationProfile, ISimpleLocalSnapshot, TSimpleLocalRawData } from '../../core/index.js';

export const nadDomain = "nad";
export const nadDefaultName = "NAD";
export const nadDefaultTelnetPort = 53;
export const nadDefaultTcpPort = 50001;
export const nadDefaultTimeoutMs = 5000;

export type TNadTransportType = 'RS232' | 'Telnet' | 'TCP' | 'rs232' | 'telnet' | 'tcp';

export interface INadLiveStatus {
  mode: 'tcp' | 'telnet';
  power?: boolean | string;
  volume?: number;
  muted?: boolean;
  source?: string | number;
  model?: string;
  version?: string;
}

export interface INadRawApiData {
  status: INadLiveStatus;
  availableSources?: string[];
  fetchedAt: string;
}

export type TNadRawData = TSimpleLocalRawData | INadRawApiData;
export interface INadSnapshot extends ISimpleLocalSnapshot {}
export interface INadConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  type?: TNadTransportType;
  sources?: Record<string, string>;
  minVolume?: number;
  maxVolume?: number;
  volumeStep?: number;
}
export interface IHomeAssistantNadConfig extends INadConfig {}

export const nadProfile: ISimpleLocalIntegrationProfile = {
  domain: nadDomain,
  displayName: "NAD",
  manufacturer: "NAD",
  model: "NAD local integration",
  defaultName: nadDefaultName,
  defaultPort: nadDefaultTelnetPort,
  defaultProtocol: 'telnet',
  status: "control-runtime",
  platforms: [
    "media_player",
  ],
  serviceDomains: [
    "media_player",
  ],
  controlServices: [
    "turn_on",
    "turn_off",
    "set_volume_level",
    "volume_mute",
    "volume_down",
    "volume_up",
    "select_source",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "nad",
    "NAD",
    "Nad",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/nad",
    "upstreamDomain": "nad",
    "iotClass": "local_polling",
    "qualityScale": "legacy",
    "requirements": [
      "nad-receiver==0.3.0",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/nad",
    "loggers": [
      "nad_receiver",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "turn_on",
        "turn_off",
        "set_volume_level",
        "volume_mute",
        "volume_down",
        "volume_up",
        "select_source",
      ],
      "platforms": [
        "media_player",
      ],
      "controls": "native TCP/Telnet control when host is configured; RS-232 remains unsupported without a serial dependency",
    },
    "localApi": {
      "implemented": [
        "native NAD D7050 TCP status polling and control on the documented binary TCP protocol, default port 50001",
        "native NAD receiver Telnet status polling and control using Main.Power/Main.Volume/Main.Mute/Main.Source text commands",
        "manual snapshots, rawData, snapshotProvider, and injected native clients remain supported",
      ],
      "explicitUnsupported": [
        "RS-232 serial transport because Home Assistant uses pyserial through nad-receiver and this package has no serial-port dependency",
        "Home Assistant entity registry migrations and repair flows",
        "NAD models whose commands are not part of the documented nad-receiver Main.* or D7050 TCP command set",
      ],
    },
  },
};
