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

export const peblarDomain = "peblar";
export const peblarDefaultName = "Peblar";
export const peblarDefaultPort = 80;
export const peblarDefaultTimeoutMs = 8000;

export interface IPeblarRawApiData extends Record<string, unknown> {
  systemInformation?: Record<string, unknown>;
  userConfiguration?: Record<string, unknown>;
  currentVersions?: Record<string, unknown>;
  availableVersions?: Record<string, unknown>;
  ev?: Record<string, unknown>;
  meter?: Record<string, unknown>;
  system?: Record<string, unknown>;
  fetchedAt?: string;
}
export type TPeblarRawData = TSimpleLocalRawData | IPeblarRawApiData;
export interface IPeblarSnapshot extends ISimpleLocalSnapshot {}
export interface IPeblarConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  enableRestApi?: boolean;
  restApiAccessMode?: 'ReadOnly' | 'ReadWrite';
}
export interface IHomeAssistantPeblarConfig extends IPeblarConfig {}

export const peblarProfile: ISimpleLocalIntegrationProfile = {
  domain: peblarDomain,
  displayName: "Peblar",
  manufacturer: "Peblar",
  model: "Peblar local integration",
  defaultName: peblarDefaultName,
  defaultPort: peblarDefaultPort,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "number",
    "select",
    "sensor",
    "switch",
    "update",
  ],
  serviceDomains: [
    "button",
    "number",
    "select",
    "switch",
  ],
  controlServices: [
    "press",
    "select_option",
    "set_value",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'mdns',
    'custom',
  ],
  discoveryKeywords: [
    "peblar",
    "Peblar",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/peblar",
    "upstreamDomain": "peblar",
    "integrationType": "device",
    "iotClass": "local_polling",
    "qualityScale": "platinum",
    "requirements": [
      "peblar==0.5.1",
    ],
    "codeowners": [
      "@frenck",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/peblar",
    "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",
        "update",
      ],
      "controls": "native Peblar HTTP APIs when config.host/config.url and password or API token are provided; injected executors remain supported",
    },
    "localApi": {
      "implemented": [
        "native Peblar HTTP login against /api/v1/auth/login with manual cookie handling",
        "native Peblar local REST token retrieval and polling against /api/wlac/v1/evinterface, /meter, and /system",
        "native Peblar controls for charge current, charge switch, force single phase, smart charging, identify, reboot, and socket unlock",
        "manual snapshots, rawData, snapshotProvider, and injected native clients remain supported",
      ],
      "explicitUnsupported": [
        "Peblar devices whose firmware reports LocalRestApiAllowed=false cannot be controlled through the local REST API",
        "Modbus API support is not implemented; this port uses the documented HTTP APIs from peblar==0.5.1",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
