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

export const kostalPlenticoreDomain = "kostal_plenticore";
export const kostalPlenticoreDefaultName = "Kostal Plenticore Solar Inverter";
export const kostalPlenticoreDefaultPort = 80;
export const kostalPlenticoreDefaultTimeoutMs = 10000;

export type TKostalPlenticoreSnapshotSource = 'http' | 'client' | 'manual' | 'snapshot' | 'runtime';

export interface IKostalPlenticoreProcessDataItem {
  id: string;
  value: number | string | boolean | null;
  unit?: string;
}

export interface IKostalPlenticoreVersionData {
  api_version?: string;
  hostname?: string;
  name?: string;
  sw_version?: string;
  [key: string]: unknown;
}

export interface IKostalPlenticoreRawData {
  version?: IKostalPlenticoreVersionData;
  processData?: Record<string, IKostalPlenticoreProcessDataItem[] | Record<string, unknown>>;
  settings?: Record<string, Record<string, string>>;
  fetchedAt?: string;
  source?: TKostalPlenticoreSnapshotSource;
}

export type TKostalPlenticoreRawData = TSimpleLocalRawData | IKostalPlenticoreRawData;
export interface IKostalPlenticoreSnapshot extends ISimpleLocalSnapshot {}
export interface IKostalPlenticoreConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  serviceCode?: string;
  service_code?: string;
  processData?: Record<string, string[]>;
  settingsData?: Record<string, string[]>;
  sessionId?: string;
}

export const kostalPlenticoreProfile: ISimpleLocalIntegrationProfile = {
  domain: kostalPlenticoreDomain,
  displayName: "Kostal Plenticore Solar Inverter",
  manufacturer: "Kostal",
  model: "Plenticore Solar Inverter",
  defaultName: kostalPlenticoreDefaultName,
  defaultPort: kostalPlenticoreDefaultPort,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "number",
    "select",
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "number",
    "select",
    "switch",
  ],
  controlServices: [
    "select_option",
    "set_value",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "kostal_plenticore",
    "Kostal Plenticore Solar Inverter",
    "Kostal Plenticore",
    "Kostal",
    "Plenticore",
    "Solar",
    "Inverter",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/kostal_plenticore",
    "upstreamDomain": "kostal_plenticore",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "pykoplenti==1.5.0",
    ],
    "codeowners": [
      "@stegm",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/kostal_plenticore",
    "loggers": [
      "kostal",
    ],
    "runtime": {
      "type": "control-runtime",
      "polling": "native Kostal Plenticore /api/v1 local REST API with SCRAM-style auth and AES-GCM session creation",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "select_option",
        "set_value",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "number",
        "select",
        "sensor",
        "switch",
      ],
      "controls": "native /api/v1/settings writes when moduleId/dataId/value are supplied, otherwise delegated to injected executors only",
    },
    "localApi": {
      "implemented": [
        "native local HTTP auth flow via /api/v1/auth/start, /auth/finish, and /auth/create_session",
        "native local HTTP process data reads via /api/v1/processdata",
        "native local HTTP setting reads and writes via /api/v1/settings",
        "runtime mapping for Kostal Plenticore Solar Inverter device snapshots and Home Assistant platform metadata",
        "snapshot, rawData, snapshotProvider, and injected native client operation",
      ],
      "explicitUnsupported": [
        "cloud account APIs; Home Assistant uses the inverter-local REST API only",
        "command success without a live host/password plus moduleId/dataId/value or an injected executor",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
