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

export const nutDomain = "nut";
export const nutDefaultName = "Network UPS Tools (NUT)";
export const nutDefaultPort = 3493;
export const nutDefaultTimeoutMs = 5000;

export type TNutRawData = TSimpleLocalRawData;
export type TNutSnapshotSource = 'tcp' | 'manual' | 'snapshot' | 'client' | 'provider' | 'runtime';

export interface INutStatusRecord {
  [key: string]: string;
}

export interface INutUpsInfo {
  alias?: string;
  description?: string;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  firmware?: string;
  macAddress?: string;
  host?: string;
  port?: number;
  status?: string;
  statusDisplay?: string;
}

export interface INutSnapshot extends ISimpleLocalSnapshot {
  ups?: INutUpsInfo;
  vars?: INutStatusRecord;
  upsList?: Record<string, string>;
  commands?: string[];
  source: TNutSnapshotSource;
}
export interface INutConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  alias?: string;
  upsName?: string;
  useTls?: boolean;
}
export interface IHomeAssistantNutConfig extends INutConfig {}

export const nutProfile: ISimpleLocalIntegrationProfile = {
  domain: nutDomain,
  displayName: "Network UPS Tools (NUT)",
  manufacturer: "Network UPS Tools (NUT)",
  model: "Network UPS Tools (NUT) local integration",
  defaultName: nutDefaultName,
  defaultPort: nutDefaultPort,
  defaultProtocol: 'tcp',
  status: "control-runtime",
  platforms: [
    "button",
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "button",
    "switch",
  ],
  controlServices: [
    "press",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "nut",
    "Network UPS Tools (NUT)",
    "Nut",
    "Network",
    "UPS",
    "Tools",
    "(NUT)",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/nut",
    "upstreamDomain": "nut",
    "integrationType": "device",
    "iotClass": "local_polling",
    "qualityScale": "platinum",
    "requirements": [
      "aionut==4.3.4",
    ],
    "codeowners": [
      "@bdraco",
      "@ollo69",
      "@pestevez",
      "@tdfountain",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/nut",
    "loggers": [
      "aionut",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "press",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "button",
        "sensor",
        "switch",
      ],
      "polling": "native local TCP NUT protocol LIST UPS, LIST VAR, and LIST CMD on port 3493",
      "controls": "native NUT INSTCMD over TCP when host, command name, username, and password are configured; otherwise delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "native local TCP NUT protocol polling using LIST UPS, LIST VAR, and LIST CMD",
        "native NUT INSTCMD execution after USERNAME/PASSWORD authentication when command names are supplied",
        "manual local setup for Network UPS Tools (NUT) endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Network UPS Tools (NUT) device snapshots and Home Assistant platform metadata",
      ],
      "explicitUnsupported": [
        "direct UPS USB/serial hardware communication; this integration intentionally talks to a configured NUT server, matching Home Assistant's aionut usage",
        "NUT STARTTLS/TLS upgrade; use plain local TCP or an injected native client for TLS deployments",
        "SET VAR and parameterized INSTCMD operations",
        "claiming live Network UPS Tools (NUT) polling, pairing, subscription, or write success without a reachable NUT server, injected native client, or snapshotProvider",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without a NUT command name and configured credentials or an injected client.execute/commandExecutor",
      ],
    },
  },
};
