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

export const maxcubeDomain = "maxcube";
export const maxcubeDefaultName = "eQ-3 MAX!";
export const maxcubeDefaultPort = 62910;
export const maxcubeDefaultTimeoutMs = 5000;

export type TMaxcubeSnapshotSource = 'tcp' | 'client' | 'manual' | 'snapshot' | 'runtime';
export type TMaxcubeDeviceKind = 'thermostat' | 'wall_thermostat' | 'window_shutter' | 'unknown';

export interface IMaxcubeRoom {
  id: number;
  name: string;
  rfAddress?: string;
}

export interface IMaxcubeDevice {
  type: number;
  kind: TMaxcubeDeviceKind;
  rfAddress: string;
  roomId?: number;
  roomName?: string;
  name: string;
  serial?: string;
  batteryLow?: boolean;
  comfortTemperature?: number;
  ecoTemperature?: number;
  maxTemperature?: number;
  minTemperature?: number;
  valvePosition?: number;
  targetTemperature?: number;
  actualTemperature?: number;
  mode?: number;
  modeName?: string;
  isOpen?: boolean;
  initialized?: boolean;
  programme?: Record<string, Array<{ temp: number; until: string }>>;
}

export interface IMaxcubeRawState {
  cube: {
    serial?: string;
    rfAddress?: string;
    firmwareVersion?: string;
    host?: string;
    port?: number;
  };
  rooms: IMaxcubeRoom[];
  devices: IMaxcubeDevice[];
  messages?: string[];
  fetchedAt: string;
}

export type TMaxcubeRawData = IMaxcubeRawState | TSimpleLocalRawData;
export interface IMaxcubeSnapshot extends ISimpleLocalSnapshot {}
export interface IMaxcubeConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  transport?: 'tcp' | 'snapshot';
}
export interface IHomeAssistantMaxcubeConfig extends IMaxcubeConfig {}

export const maxcubeProfile: ISimpleLocalIntegrationProfile = {
  domain: maxcubeDomain,
  displayName: "eQ-3 MAX!",
  manufacturer: "eQ-3 MAX!",
  model: "eQ-3 MAX! local integration",
  defaultName: maxcubeDefaultName,
  defaultPort: maxcubeDefaultPort,
  defaultProtocol: 'tcp',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "climate",
  ],
  serviceDomains: [
    "climate",
  ],
  controlServices: [
    "set_hvac_mode",
    "set_temperature",
    "set_preset_mode",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "maxcube",
    "eQ-3 MAX!",
    "Maxcube",
    "eQ-3",
    "MAX!",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/maxcube",
    "upstreamDomain": "maxcube",
    "iotClass": "local_polling",
    "qualityScale": "legacy",
    "requirements": [
      "maxcube-api==0.4.3",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/maxcube",
    "loggers": [
      "maxcube",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "set_hvac_mode",
        "set_temperature",
        "set_preset_mode",
      ],
      "platforms": [
        "binary_sensor",
        "climate",
      ],
      "controls": "native MAX! Cube TCP set_temperature/set_hvac_mode/set_preset_mode for known thermostats; other commands require an injected executor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for eQ-3 MAX! endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "native TCP polling on port 62910 using MAX! Cube H/M/C/L messages from maxcube-api 0.4.3 protocol source",
        "runtime mapping for MAX! thermostats, wall thermostats, window shutters, and battery diagnostics",
        "native TCP thermostat set_temperature, set_hvac_mode, and set_preset_mode commands using MAX! Cube s: radio messages",
        "safe command delegation to injected client.execute or commandExecutor for non-thermostat commands",
      ],
      "explicitUnsupported": [
        "pairing/enrolling MAX! devices with a Cube; Home Assistant and maxcube-api expose no pairing flow in this integration",
        "weekly programme writes beyond the documented maxcube-api set_programme command surface",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success for unsupported services without a successful Cube S: acknowledgement or injected executor",
      ],
    },
  },
};
