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

export const iskraDomain = "iskra";
export const iskraDefaultName = "iskra";
export const iskraManufacturer = "Iskra d.o.o";
export const iskraDefaultHttpPort = 80;
export const iskraDefaultTimeoutMs = 10000;

export type TIskraProtocol = 'rest_api' | 'modbus_tcp' | 'http' | 'https';

export interface IIskraRestDeviceRaw {
  index: number;
  basicInfo: Record<string, unknown>;
  measurements?: Record<string, unknown>;
  counters?: Record<string, unknown>;
  errors?: Record<string, string>;
}

export interface IIskraRestRawData extends Record<string, unknown> {
  gateway?: Record<string, unknown>;
  devices: IIskraRestDeviceRaw[];
  fetchedAt: string;
  errors?: Record<string, string>;
}

export type TIskraRawData = TSimpleLocalRawData;
export interface IIskraSnapshot extends ISimpleLocalSnapshot {}
export interface IIskraConfig extends ISimpleLocalConfig {
  url?: string;
  protocol?: TIskraProtocol;
  baseUrl?: string;
  deviceIndex?: number;
  device_index?: number;
  address?: number;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantIskraConfig extends IIskraConfig {}

export const iskraProfile: ISimpleLocalIntegrationProfile = {
  domain: iskraDomain,
  displayName: "iskra",
  manufacturer: iskraManufacturer,
  model: "Iskra local integration",
  defaultName: iskraDefaultName,
  defaultPort: iskraDefaultHttpPort,
  defaultProtocol: 'http',
  status: "read-only-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "iskra",
    "Iskra",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/iskra",
    "upstreamDomain": "iskra",
    "integrationType": "hub",
    "iotClass": "local_polling",
    "requirements": [
      "pyiskra==0.1.27",
    ],
    "codeowners": [
      "@iskramis",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/iskra",
    "loggers": [
      "pyiskra",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "status": "Smart Gateway REST API polling is implemented natively; Modbus TCP remains explicit unsupported until a native Modbus register client is added.",
      "implemented": [
        "manual local setup for iskra Smart Gateway REST API endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "native REST polling with GET /api, /api/devices, /api/measurement/{index}, and /api/counter/{index}",
        "runtime mapping for iskra power, voltage, current, frequency, and counter sensors from REST API data",
      ],
      "explicitUnsupported": [
        "native Modbus TCP/RTU polling; Home Assistant delegates this path to pyiskra/pymodbus device-specific register maps, and this repo has no shared Modbus helper dependency",
        "claiming live iskra polling success without a Smart Gateway REST host, injected native client, snapshotProvider, snapshot, entities, or rawData",
        "write/control services; Home Assistant exposes iskra as sensors only",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
