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

export const moehlenhoffAlpha2Domain = "moehlenhoff_alpha2";
export const moehlenhoffAlpha2DefaultName = "Mohlenhoff Alpha 2";
export const moehlenhoffAlpha2DefaultPort = 80;
export const moehlenhoffAlpha2DefaultTimeoutMs = 10000;

export interface IMoehlenhoffAlpha2HeatArea extends Record<string, unknown> {
  ID: string;
  NR: number;
  HEATAREA_NAME?: string;
  HEATAREA_MODE?: number;
  T_ACTUAL?: number;
  T_TARGET?: number;
  T_TARGET_MIN?: number;
  T_TARGET_MAX?: number;
  _HEATCTRL_STATE?: number;
}

export interface IMoehlenhoffAlpha2HeatControl extends Record<string, unknown> {
  ID: string;
  NR: number;
  INUSE?: boolean;
  HEATAREA_NR?: number;
  ACTOR_PERCENT?: number;
  _HEATAREA_ID?: string | null;
}

export interface IMoehlenhoffAlpha2IoDevice extends Record<string, unknown> {
  ID: string;
  NR: number;
  HEATAREA_NR?: number;
  BATTERY?: number;
  _HEATAREA_ID?: string | null;
}

export interface IMoehlenhoffAlpha2RawApiData extends Record<string, unknown> {
  base?: {
    id?: string;
    name?: string;
    cooling?: boolean;
  };
  heatAreas?: IMoehlenhoffAlpha2HeatArea[];
  heatControls?: IMoehlenhoffAlpha2HeatControl[];
  ioDevices?: IMoehlenhoffAlpha2IoDevice[];
  fetchedAt?: string;
}

export type TMoehlenhoffAlpha2RawData = TSimpleLocalRawData | IMoehlenhoffAlpha2RawApiData;
export interface IMoehlenhoffAlpha2Snapshot extends ISimpleLocalSnapshot {}
export interface IMoehlenhoffAlpha2Config extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantMoehlenhoffAlpha2Config extends IMoehlenhoffAlpha2Config {}

export const moehlenhoffAlpha2Profile: ISimpleLocalIntegrationProfile = {
  domain: moehlenhoffAlpha2Domain,
  displayName: "Mohlenhoff Alpha 2",
  manufacturer: "Mohlenhoff Alpha 2",
  model: "Mohlenhoff Alpha 2 local integration",
  defaultName: moehlenhoffAlpha2DefaultName,
  defaultPort: moehlenhoffAlpha2DefaultPort,
  defaultHttpPath: '/data/static.xml',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "climate",
    "sensor",
  ],
  serviceDomains: [
    "button",
    "climate",
  ],
  controlServices: [
    "press",
    "set_hvac_mode",
    "set_temperature",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "moehlenhoff_alpha2",
    "Mohlenhoff Alpha 2",
    "Moehlenhoff Alpha2",
    "Mohlenhoff",
    "Alpha",
    "2",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/moehlenhoff_alpha2",
    "upstreamDomain": "moehlenhoff_alpha2",
    "integrationType": "hub",
    "iotClass": "local_push",
    "requirements": [
      "moehlenhoff-alpha2==1.4.0",
    ],
    "codeowners": [
      "@j-a-n",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/moehlenhoff_alpha2",
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "press",
        "set_hvac_mode",
        "set_temperature",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "climate",
        "sensor",
      ],
      "controls": "native local HTTP /data/changes.xml when config.host/config.url is configured; otherwise delegated to injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Mohlenhoff Alpha 2 endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "live local HTTP polling from /data/static.xml as used by moehlenhoff-alpha2 and the vendor XML interface",
        "live local HTTP writes to /data/changes.xml for cooling mode, heat-area target temperature, and base datetime commands",
        "runtime mapping for Mohlenhoff Alpha 2 climate, button, sensor, and binary_sensor metadata",
        "safe command delegation to injected client.execute or commandExecutor when supplied",
      ],
      "explicitUnsupported": [
        "push subscription semantics; HA labels the integration local_push, but moehlenhoff-alpha2 polls /data/static.xml every update",
        "pairing or cloud portal flows; HA only uses the local XML interface once a host is known",
        "claiming live Mohlenhoff Alpha 2 polling or write success without config.host/config.url, an injected native client, or snapshotProvider",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without a real /data/changes.xml response, injected client.execute, or commandExecutor",
      ],
    },
  },
};
