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

export const nzbgetDomain = "nzbget";
export const nzbgetDefaultName = "NZBGet";
export const nzbgetDefaultPort = 6789;
export const nzbgetDefaultXmlRpcPath = '/xmlrpc';
export const nzbgetDefaultTimeoutMs = 5000;

export interface INzbgetRawData {
  version?: string;
  status?: Record<string, unknown>;
  downloads?: Array<Record<string, unknown>>;
  history?: Array<Record<string, unknown>>;
  fetchedAt?: string;
  errors?: Record<string, string>;
  [key: string]: unknown;
}

export type TNzbgetRawData = INzbgetRawData | TSimpleLocalRawData;
export interface INzbgetSnapshot extends ISimpleLocalSnapshot {}
export interface INzbgetConfig extends ISimpleLocalConfig {
  url?: string;
  ssl?: boolean;
  verifySsl?: boolean;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantNzbgetConfig extends INzbgetConfig {}

export const nzbgetProfile: ISimpleLocalIntegrationProfile = {
  domain: nzbgetDomain,
  displayName: "NZBGet",
  manufacturer: "NZBGet",
  model: "NZBGet local integration",
  defaultName: nzbgetDefaultName,
  defaultPort: nzbgetDefaultPort,
  defaultHttpPath: nzbgetDefaultXmlRpcPath,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "switch",
  ],
  controlServices: [
    "pause",
    "resume",
    "set_speed",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "nzbget",
    "NZBGet",
    "Nzbget",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/nzbget",
    "upstreamDomain": "nzbget",
    "integrationType": "service",
    "iotClass": "local_polling",
    "requirements": [
      "pynzbgetapi==0.2.0",
    ],
    "codeowners": [
      "@chriscla",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/nzbget",
    "loggers": [
      "pynzbgetapi",
    ],
    "runtime": {
      "type": "control-runtime",
      "polling": "local NZBGet XML-RPC over HTTP(S) at /xmlrpc",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "pause",
        "resume",
        "set_speed",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "sensor",
        "switch",
      ],
      "controls": "local XML-RPC pause/resume/rate commands or delegated injected client.execute/commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for NZBGet HTTP(S) XML-RPC endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "live XML-RPC reads for version, status, and history using the same /xmlrpc API used by pynzbgetapi",
        "runtime mapping for NZBGet status, history, download switch, and Home Assistant platform metadata",
        "real XML-RPC pause, resume, and rate-limit writes when a live endpoint is configured",
        "safe command delegation to injected client.execute or commandExecutor",
      ],
      "explicitUnsupported": [
        "claiming write success for static snapshots or manual rawData without a live XML-RPC endpoint, injected client.execute, or commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
        "TLS certificate bypass through Node fetch; use HTTP, a valid certificate, or an injected client if NZBGet uses a self-signed HTTPS certificate",
      ],
    },
  },
};
