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

export const openevseDomain = "openevse";
export const openevseDefaultName = "OpenEVSE";
export const openevseDefaultPort = 80;

export interface IOpenevseRawApiData extends Record<string, unknown> {
  device?: Record<string, unknown>;
  status: Record<string, unknown>;
  config: Record<string, unknown>;
  entities?: ISimpleLocalEntitySnapshot[];
  fetchedAt: string;
}

export type TOpenevseRawData = IOpenevseRawApiData | TSimpleLocalRawData;
export interface IOpenevseSnapshot extends ISimpleLocalSnapshot {}
export interface IOpenevseConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  serialNumber?: string;
}
export interface IHomeAssistantOpenevseConfig extends IOpenevseConfig {}

export const openevseProfile: ISimpleLocalIntegrationProfile = {
  domain: openevseDomain,
  displayName: "OpenEVSE",
  manufacturer: "OpenEVSE",
  model: "OpenEVSE local HTTP charger",
  defaultName: openevseDefaultName,
  defaultPort: openevseDefaultPort,
  defaultHttpPath: '/status',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "number",
    "sensor",
  ],
  serviceDomains: [
    "number",
  ],
  controlServices: [
    "set_value",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "openevse",
    "OpenEVSE",
    "Openevse",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/openevse",
    "upstreamDomain": "openevse",
    "integrationType": "device",
    "iotClass": "local_push",
    "qualityScale": "bronze",
    "requirements": [
      "python-openevse-http==0.2.5",
    ],
    "afterDependencies": [
      "zeroconf",
    ],
    "codeowners": [
      "@c00w",
      "@firstof9",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/openevse",
    "loggers": [
      "openevsehttp",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "set_value",
      ],
      "platforms": [
        "number",
        "sensor",
      ],
      "controls": "native OpenEVSE HTTP set_value for charge_rate plus injected client.execute or commandExecutor",
    },
    "localApi": {
      "status": "Native local HTTP polling is implemented using GET /status and GET /config, with optional Basic authentication. The charge_rate number can be written using POST /override on firmware 4.1.2+ or RAPI POST /r on older firmware, matching python-openevse-http 0.2.5 behavior.",
      "implemented": [
        "native OpenEVSE HTTP polling of /status and /config on port 80 with optional Basic authentication",
        "native parsing of OpenEVSE charger status, current, voltage, energy, temperature, vehicle, shaper, safety count, uptime, and charge_rate entities",
        "native OpenEVSE charge_rate set_value through documented HTTP override or RAPI current commands",
        "manual local setup for OpenEVSE endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for OpenEVSE device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor for unsupported controls",
      ],
      "explicitUnsupported": [
        "OpenEVSE websocket push subscriptions; Home Assistant uses python-openevse-http websockets, but this TypeScript client implements safe local polling only",
        "OpenEVSE pairing or Wi-Fi provisioning flows; Home Assistant only configures an already reachable host",
        "OpenEVSE controls other than charge_rate set_value unless provided by an injected client.execute or commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
