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

export const mysensorsDomain = "mysensors";
export const mysensorsDefaultName = "MySensors";
export const mysensorsDefaultTcpPort = 5003;
export const mysensorsDefaultTimeoutMs = 5000;
export const mysensorsDefaultCollectMs = 750;

export type TMysensorsGatewayType = 'Serial' | 'TCP' | 'MQTT';

export type TMysensorsRawData = TSimpleLocalRawData;
export interface IMysensorsSnapshot extends ISimpleLocalSnapshot {}
export interface IMysensorsConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  gatewayType?: TMysensorsGatewayType;
  protocolVersion?: string;
  collectMs?: number;
  nodeId?: number;
  childId?: number;
  valueType?: number | string;
  ack?: boolean | number;
}
export interface IHomeAssistantMysensorsConfig extends IMysensorsConfig {}

export const mysensorsProfile: ISimpleLocalIntegrationProfile = {
  domain: mysensorsDomain,
  displayName: "MySensors",
  manufacturer: "MySensors",
  model: "MySensors local integration",
  defaultName: mysensorsDefaultName,
  defaultPort: mysensorsDefaultTcpPort,
  defaultTcpCommand: '0;255;3;0;2;',
  defaultProtocol: 'tcp',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "climate",
    "cover",
    "light",
    "sensor",
    "switch",
    "text",
  ],
  serviceDomains: [
    "button",
    "climate",
    "cover",
    "light",
    "switch",
    "text",
  ],
  controlServices: [
    "close_cover",
    "open_cover",
    "press",
    "set_hvac_mode",
    "set_temperature",
    "set_value",
    "stop_cover",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "mysensors",
    "MySensors",
    "Mysensors",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/mysensors",
    "upstreamDomain": "mysensors",
    "integrationType": "hub",
    "iotClass": "local_push",
    "requirements": [
      "pymysensors==0.26.0",
    ],
    "afterDependencies": [
      "mqtt",
    ],
    "codeowners": [
      "@MartinHjelmare",
      "@functionpointer",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/mysensors",
    "loggers": [
      "mysensors",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "close_cover",
        "open_cover",
        "press",
        "set_hvac_mode",
        "set_temperature",
        "set_value",
        "stop_cover",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "climate",
        "cover",
        "light",
        "sensor",
        "switch",
        "text",
      ],
      "controls": "delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "native local TCP gateway connection to the documented MySensors serial API on the Ethernet gateway port",
        "passive TCP collection and parsing of node-id;child-id;command;ack;type;payload messages into snapshot entities",
        "explicit TCP set commands when nodeId, childId, valueType, and value are supplied or derivable from a single captured entity",
        "manual local setup for MySensors endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for MySensors device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor when supplied",
      ],
      "explicitUnsupported": [
        "native serial gateway access without an injected client, because serial device ownership and pyserial semantics are outside this runtime",
        "native MQTT gateway access without an injected MQTT client/broker runtime, because Home Assistant delegates MQTT publish/subscribe to its MQTT integration",
        "full pymysensors persistence, smart-sleep queueing, OTA firmware updates, and protocol validation beyond safe documented TCP line framing",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
