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

export const nibeHeatpumpDomain = "nibe_heatpump";
export const nibeHeatpumpDefaultName = "Nibe Heat Pump";

export type TNibeHeatpumpConnectionType = 'modbus' | 'nibegw' | 'serial' | 'snapshot';
export type TNibeHeatpumpCoilSize = 'u8' | 's8' | 'u16' | 's16' | 'u32' | 's32';

export interface INibeHeatpumpCoilConfig {
  address: number;
  name: string;
  title?: string;
  size: TNibeHeatpumpCoilSize;
  factor?: number;
  unit?: string;
  write?: boolean;
  type?: 'number' | 'date';
  min?: number;
  max?: number;
  mappings?: Record<string, string>;
  platform?: TEntityPlatform;
}

export type TNibeHeatpumpRawData = TSimpleLocalRawData;
export interface INibeHeatpumpSnapshot extends ISimpleLocalSnapshot {}
export interface INibeHeatpumpConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  connectionType?: TNibeHeatpumpConnectionType;
  modbusUrl?: string;
  modbusUnit?: number;
  model?: string;
  wordSwap?: boolean;
  coils?: INibeHeatpumpCoilConfig[];
  allowWrites?: boolean;
}
export interface IHomeAssistantNibeHeatpumpConfig extends INibeHeatpumpConfig {}

export const nibeHeatpumpDefaultModbusCoils: INibeHeatpumpCoilConfig[] = [
  {
    address: 40004,
    name: 'bt1-outdoor-temperature-40004',
    title: 'BT1 Outdoor Temperature',
    size: 's16',
    factor: 10,
    unit: 'C',
  },
];

export const nibeHeatpumpProfile: ISimpleLocalIntegrationProfile = {
  domain: nibeHeatpumpDomain,
  displayName: "Nibe Heat Pump",
  manufacturer: "Nibe Heat Pump",
  model: "Nibe Heat Pump local integration",
  defaultName: nibeHeatpumpDefaultName,
  defaultPort: 502,
  defaultProtocol: 'tcp',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "climate",
    "number",
    "select",
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "button",
    "climate",
    "number",
    "select",
    "switch",
  ],
  controlServices: [
    "press",
    "select_option",
    "set_hvac_mode",
    "set_temperature",
    "set_value",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "nibe_heatpump",
    "Nibe Heat Pump",
    "Nibe Heatpump",
    "Nibe",
    "Heat",
    "Pump",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/nibe_heatpump",
    "upstreamDomain": "nibe_heatpump",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "nibe==2.22.0",
    ],
    "codeowners": [
      "@elupus",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/nibe_heatpump",
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "press",
        "select_option",
        "set_hvac_mode",
        "set_temperature",
        "set_value",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "climate",
        "number",
        "select",
        "sensor",
        "switch",
      ],
      "controls": "native Modbus TCP writes only when allowWrites is true and the target configured coil is writable; otherwise delegated to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "status": "native TCP Modbus read_coil implemented for configured coils; NibeGW UDP, serial Modbus, model auto-detection, and bundled Python nibe coil databases are not embedded",
      "implemented": [
        "native Modbus TCP reads for configured NIBE coil definitions using the nibe library address split documented in nibe.connection.modbus",
        "optional native Modbus TCP writes only for explicitly configured writable coils when allowWrites is true",
        "manual local setup for Nibe Heat Pump endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Nibe Heat Pump device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor when no live Modbus TCP endpoint is configured",
      ],
      "explicitUnsupported": [
        "NibeGW live polling/control because the HA nibe library documents NibeGW as UDP listener/send ports 9999/10000 and this integration is limited to TCP/TLS/HTTP/file-capable transports",
        "serial Modbus and RFC2217 serial bridge URLs because they require serial semantics outside the existing repository dependencies",
        "automatic model identification and full bundled coil database loading because HA relies on the unavailable Python nibe package data files; provide config.coils for native Modbus TCP reads",
        "claiming write success unless a live Modbus TCP write succeeds for an explicitly configured writable coil with allowWrites=true, or an injected executor reports success",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
