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

export const iotawattDomain = "iotawatt";
export const iotawattDefaultName = "IoTaWatt";
export const iotawattDefaultPort = 80;
export const iotawattDefaultTimeoutMs = 10000;
export const iotawattDefaultTimespanSeconds = 30;

export interface IIotawattStatusResponse extends Record<string, unknown> {
  wifi?: {
    mac?: string;
  };
  inputs?: Array<Record<string, unknown>>;
  outputs?: Array<Record<string, unknown>>;
}

export interface IIotawattSeriesDefinition extends Record<string, unknown> {
  name?: string;
  unit?: string;
}

export interface IIotawattQuerySeriesResponse extends Record<string, unknown> {
  series?: IIotawattSeriesDefinition[];
}

export interface IIotawattSensorSnapshot extends Record<string, unknown> {
  key: string;
  channel: string | number;
  baseName: string;
  sourceName: string;
  name: string;
  type: 'Input' | 'Output';
  unit: string;
  value?: number | string | boolean | null;
  begin?: string;
  fromStart?: boolean;
}

export interface IIotawattRawApiData extends Record<string, unknown> {
  status?: IIotawattStatusResponse;
  series?: IIotawattSeriesDefinition[];
  sensors?: Record<string, IIotawattSensorSnapshot>;
  fetchedAt?: string;
}

export type TIotawattRawData = TSimpleLocalRawData | IIotawattRawApiData;
export interface IIotawattSnapshot extends ISimpleLocalSnapshot {}
export interface IIotawattConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  timespanSeconds?: number;
  integratedInterval?: string;
  includeNonTotalSensors?: boolean;
}

export const iotawattProfile: ISimpleLocalIntegrationProfile = {
  domain: iotawattDomain,
  displayName: "IoTaWatt",
  manufacturer: "IoTaWatt",
  model: "IoTaWatt local integration",
  defaultName: iotawattDefaultName,
  defaultPort: iotawattDefaultPort,
  defaultHttpPath: '/status?wifi=yes',
  defaultProtocol: 'http',
  status: "read-only-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "iotawatt",
    "IoTaWatt",
    "Iotawatt",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/iotawatt",
    "upstreamDomain": "iotawatt",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "ha-iotawattpy==0.1.2",
    ],
    "codeowners": [
      "@gtdiehl",
      "@jyavenard",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/iotawatt",
    "loggers": [
      "iotawattpy",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "manual local setup for IoTaWatt host/url, snapshots, rawData, snapshotProvider, and injected native clients",
        "live local HTTP polling from /status and /query endpoints used by ha-iotawattpy",
        "HTTP Digest authentication for local IoTaWatt devices when username/password are configured",
        "runtime mapping for IoTaWatt inputs/outputs/query series into sensor metadata",
        "safe command delegation to injected client.execute or commandExecutor only",
      ],
      "explicitUnsupported": [
        "write/control services; the Home Assistant IoTaWatt integration is read-only",
        "claiming live polling success without config.host/config.url, an injected native client, or snapshotProvider",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without injected client.execute or commandExecutor",
      ],
    },
  },
};
