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

export const iometerDomain = "iometer";
export const iometerDefaultName = "IOmeter";
export const iometerDefaultPort = 80;
export const iometerDefaultTimeoutMs = 60000;

export interface IIometerRegister extends Record<string, unknown> {
  obis: string;
  value: number;
  unit: string;
}

export interface IIometerReadingResponse extends Record<string, unknown> {
  meter?: {
    number?: string;
    reading?: {
      time?: string;
      registers?: IIometerRegister[];
    };
  };
}

export interface IIometerStatusResponse extends Record<string, unknown> {
  meter?: { number?: string } | null;
  device?: {
    id?: string;
    bridge?: {
      rssi?: number;
      version?: string;
    };
    core?: {
      connectionStatus?: string;
      rssi?: number | null;
      version?: string | null;
      powerStatus?: string | null;
      attachmentStatus?: string | null;
      pinStatus?: string | null;
      batteryLevel?: number | null;
    };
  };
}

export interface IIometerRawApiData extends Record<string, unknown> {
  reading?: IIometerReadingResponse;
  status?: IIometerStatusResponse;
  fetchedAt?: string;
}

export type TIometerRawData = TSimpleLocalRawData | IIometerRawApiData;
export interface IIometerSnapshot extends ISimpleLocalSnapshot {}
export interface IIometerConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
}

export const iometerProfile: ISimpleLocalIntegrationProfile = {
  domain: iometerDomain,
  displayName: "IOmeter",
  manufacturer: "IOmeter",
  model: "IOmeter local integration",
  defaultName: iometerDefaultName,
  defaultPort: iometerDefaultPort,
  defaultHttpPath: '/v1/status',
  defaultProtocol: 'http',
  status: "read-only-runtime",
  platforms: [
    "binary_sensor",
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "iometer",
    "IOmeter",
    "Iometer",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/iometer",
    "upstreamDomain": "iometer",
    "integrationType": "device",
    "iotClass": "local_polling",
    "qualityScale": "bronze",
    "requirements": [
      "iometer==0.4.0",
    ],
    "codeowners": [
      "@jukrebs",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/iometer",
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "binary_sensor",
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "manual local setup for IOmeter host/url, snapshots, rawData, snapshotProvider, and injected native clients",
        "live local HTTP polling from /v1/reading and /v1/status",
        "runtime mapping for IOmeter reading/status data into sensor and binary_sensor metadata",
        "safe command delegation to injected client.execute or commandExecutor only",
      ],
      "explicitUnsupported": [
        "write/control services; the Home Assistant IOmeter 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",
      ],
    },
  },
};
