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

export const intellifireDomain = "intellifire";
export const intellifireDefaultName = "IntelliFire";
export const intellifireDefaultPort = 80;
export const intellifireDefaultTimeoutMs = 10000;

export interface IIntellifirePollData extends Record<string, unknown> {
  battery?: number | string | boolean;
  brand?: string;
  errors?: number[];
  fanspeed?: number | string;
  height?: number | string;
  firmware_version?: string;
  firmware_version_string?: string;
  feature_fan?: number | string | boolean;
  feature_light?: number | string | boolean;
  feature_thermostat?: number | string | boolean;
  hot?: number | string | boolean;
  ipv4_address?: string;
  light?: number | string;
  name?: string;
  pilot?: number | string | boolean;
  power?: number | string | boolean;
  power_vent?: number | string | boolean;
  remote_connection_quality?: number | string;
  remote_downtime?: number | string;
  remote_uptime?: number | string;
  serial?: string;
  setpoint?: number | string;
  temperature?: number | string;
  thermostat?: number | string | boolean;
  timer?: number | string | boolean;
  timeremaining?: number | string;
}

export interface IIntellifireRawApiData extends Record<string, unknown> {
  poll?: IIntellifirePollData;
  fetchedAt?: string;
}

export type TIntellifireRawData = TSimpleLocalRawData | IIntellifirePollData | IIntellifireRawApiData;
export interface IIntellifireSnapshot extends ISimpleLocalSnapshot {}
export interface IIntellifireConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  serial?: string;
  ipAddress?: string;
  userId?: string;
  apiKey?: string;
}

export const intellifireProfile: ISimpleLocalIntegrationProfile = {
  domain: intellifireDomain,
  displayName: "IntelliFire",
  manufacturer: "IntelliFire",
  model: "IntelliFire local integration",
  defaultName: intellifireDefaultName,
  defaultPort: intellifireDefaultPort,
  defaultHttpPath: '/poll',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "climate",
    "fan",
    "light",
    "number",
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "climate",
    "fan",
    "light",
    "number",
    "switch",
  ],
  controlServices: [
    "set_hvac_mode",
    "set_temperature",
    "set_value",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "intellifire",
    "IntelliFire",
    "Intellifire",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/intellifire",
    "upstreamDomain": "intellifire",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "intellifire4py==4.4.0",
    ],
    "codeowners": [
      "@jeeftor",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/intellifire",
    "loggers": [
      "intellifire4py",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "set_hvac_mode",
        "set_temperature",
        "set_value",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "binary_sensor",
        "climate",
        "fan",
        "light",
        "number",
        "sensor",
        "switch",
      ],
      "controls": "delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for IntelliFire host/url, snapshots, rawData, snapshotProvider, and injected native clients",
        "live local HTTP polling from /poll using the documented intellifire4py local endpoint",
        "runtime mapping for IntelliFire poll data into binary_sensor, climate, fan, light, number, sensor, and switch metadata",
        "local /get_challenge plus signed /post control when config.userId and config.apiKey are provided",
        "safe command delegation to injected client.execute or commandExecutor",
      ],
      "explicitUnsupported": [
        "cloud login, pairing, and cloud credential acquisition; local control credentials must be supplied from the vendor cloud/app flow",
        "claiming local write success without config.host/config.url plus config.userId and config.apiKey, or an injected native executor",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
