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

export const lektricoDomain = "lektrico";
export const lektricoDefaultName = "Lektrico Charging Station";
export const lektricoDefaultPort = 80;
export const lektricoDeviceTypes = {
  charger1p: '1p7k',
  charger3p: '3p22k',
  energyMeter1p: 'em',
  energyMeter3p: '3em',
} as const;

export type TLektricoDeviceType = typeof lektricoDeviceTypes[keyof typeof lektricoDeviceTypes];
export interface ILektricoSettings {
  type: TLektricoDeviceType;
  serial_number: string | number;
  board_revision?: string;
}
export interface ILektricoRawData extends Record<string, unknown> {
  device: Record<string, unknown>;
  settings: ILektricoSettings;
  info: Record<string, unknown>;
  entities: ISimpleLocalEntitySnapshot[];
  fetchedAt: string;
}
export type TLektricoRawData = ILektricoRawData | TSimpleLocalRawData;
export interface ILektricoSnapshot extends ISimpleLocalSnapshot {}
export interface ILektricoConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  serialNumber?: string;
  boardRevision?: string;
  deviceType?: TLektricoDeviceType;
}

export const lektricoProfile: ISimpleLocalIntegrationProfile = {
  domain: lektricoDomain,
  displayName: "Lektrico Charging Station",
  manufacturer: "Lektrico",
  model: "Lektrico local HTTP device",
  defaultName: lektricoDefaultName,
  defaultPort: lektricoDefaultPort,
  defaultHttpPath: '/rpc/Device_id.Get',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "number",
    "select",
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "button",
    "number",
    "select",
    "switch",
  ],
  controlServices: [
    "press",
    "select_option",
    "set_value",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'mdns',
    'custom',
  ],
  discoveryKeywords: [
    "lektrico",
    "Lektrico Charging Station",
    "Lektrico",
    "Charging",
    "Station",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/lektrico",
    "upstreamDomain": "lektrico",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "lektricowifi==0.1",
    ],
    "codeowners": [
      "@lektrico",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/lektrico",
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "press",
        "select_option",
        "set_value",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "number",
        "select",
        "sensor",
        "switch",
      ],
      "controls": "native local HTTP JSON-RPC plus injected client.execute or commandExecutor",
    },
    "localApi": {
      "status": "Native local HTTP JSON-RPC is implemented for Lektrico Wi-Fi chargers and meters using GET /rpc/*.Get and POST /rpc, matching lektricowifi 0.1.",
      "implemented": [
        "manual and mDNS-hinted local setup for Lektrico HTTP endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "GET /rpc/Device_id.Get plus charger_config.get or M2w_config.Get for device identification",
        "GET /rpc/charger_info.get, app_config.get, active_errors.get, dynamic_current.get for charger snapshots",
        "GET /rpc/Meter_info.Get, App_config.Get, Sw_version.Get for energy meter snapshots",
        "POST /rpc commands for charging, reset, auth, LED brightness, dynamic current, load balancing mode, lock, and relay mode",
      ],
      "explicitUnsupported": [
        "cloud APIs, account auth, historical data, and non-local Lektrico services",
        "TLS/authenticated variants not present in Home Assistant's lektricowifi 0.1 local protocol",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without a successful local HTTP response, injected client.execute, or commandExecutor",
      ],
    },
  },
};
