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

export const imeonInverterDomain = "imeon_inverter";
export const imeonInverterDefaultName = "Imeon Inverter";

export type TImeonInverterModeCode = 'smg' | 'bup' | 'ong' | 'ofg';
export type TImeonInverterMode = 'smart_grid' | 'backup' | 'on_grid' | 'off_grid';

export interface IImeonInverterStorage extends Record<string, unknown> {
  battery?: Record<string, unknown>;
  grid?: Record<string, unknown>;
  pv?: Record<string, unknown>;
  input?: Record<string, unknown>;
  output?: Record<string, unknown>;
  meter?: Record<string, unknown>;
  temp?: Record<string, unknown>;
  monitoring?: Record<string, unknown>;
  monitoring_minute?: Record<string, unknown>;
  manager?: Record<string, unknown>;
  inverter?: Record<string, unknown>;
  timeline?: Record<string, unknown>;
  smartload?: Record<string, unknown>;
  energy?: Record<string, unknown>;
  forecast?: Record<string, unknown>;
}

export interface IImeonInverterRawData extends Record<string, unknown> {
  storage?: IImeonInverterStorage;
  flattened?: Record<string, string | number | boolean | null>;
}

export type TImeonInverterRawData = TSimpleLocalRawData | IImeonInverterRawData;
export interface IImeonInverterSnapshot extends ISimpleLocalSnapshot {}
export interface IImeonInverterConfig extends ISimpleLocalConfig {
  url?: string;
  deviceId?: string;
  monitoringTime?: 'hour' | 'day' | 'month' | 'year' | 'minute';
  timedDataTime?: 'minute' | 'quarter';
  performSave?: boolean;
}

export const imeonInverterModeByCode: Record<TImeonInverterModeCode, TImeonInverterMode> = {
  smg: 'smart_grid',
  bup: 'backup',
  ong: 'on_grid',
  ofg: 'off_grid',
};

export const imeonInverterCodeByMode: Record<TImeonInverterMode, TImeonInverterModeCode> = {
  smart_grid: 'smg',
  backup: 'bup',
  on_grid: 'ong',
  off_grid: 'ofg',
};

export const imeonInverterProfile: ISimpleLocalIntegrationProfile = {
  domain: imeonInverterDomain,
  displayName: "Imeon Inverter",
  manufacturer: "Imeon Inverter",
  model: "Imeon Inverter local integration",
  defaultName: imeonInverterDefaultName,
  defaultPort: 80,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "select",
    "sensor",
  ],
  serviceDomains: [
    "select",
  ],
  controlServices: [
    "select_option",
  ],
  discoverySources: [
    'manual',
    'ssdp',
    'custom',
  ],
  discoveryKeywords: [
    "imeon_inverter",
    "Imeon Inverter",
    "Imeon",
    "Inverter",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/imeon_inverter",
    "upstreamDomain": "imeon_inverter",
    "integrationType": "device",
    "iotClass": "local_polling",
    "qualityScale": "bronze",
    "requirements": [
      "imeon_inverter_api==0.4.0",
    ],
    "codeowners": [
      "@Imeon-Energy",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/imeon_inverter",
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "select_option",
      ],
      "platforms": [
        "select",
        "sensor",
      ],
      "controls": "native manager_inverter_mode select_option via Imeon HTTP API when endpoint credentials are configured; otherwise delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "native local Imeon OS One HTTP API login via POST /login and polling via /data plus /api/battery, /api/grid, /api/pv, /api/input, /api/output, /api/em, /api/temp, /api/timeline, /api/energy, /api/forecast, /api/monitor, /api/manager, and /api/smartload",
        "native manager_inverter_mode select_option using POST /api/set with inverter_mode values smg, bup, ong, and ofg",
        "manual local setup for Imeon Inverter endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Imeon Inverter sensor/select metadata from Home Assistant platform keys",
      ],
      "explicitUnsupported": [
        "Imeon WiFi NAT access; upstream API documents ETH port access only",
        "Imeon OS One versions older than v1.8.1.0 where the local HTTP API is unavailable",
        "inverter settings beyond Home Assistant's manager_inverter_mode select entity",
        "claiming live Imeon Inverter polling or write success without config.host/config.url plus config.username and config.password, an injected native client, commandExecutor, snapshotProvider, snapshot, or rawData",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
