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

export const opengarageDomain = "opengarage";
export const opengarageDefaultName = "OpenGarage";
export const opengarageDefaultPort = 80;

export interface IOpengarageRawApiData extends Record<string, unknown> {
  device?: Record<string, unknown>;
  status: Record<string, unknown>;
  entities?: ISimpleLocalEntitySnapshot[];
  fetchedAt: string;
}

export type TOpengarageRawData = IOpengarageRawApiData | TSimpleLocalRawData;
export interface IOpengarageSnapshot extends ISimpleLocalSnapshot {}
export interface IOpengarageConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  deviceKey?: string;
  device_key?: string;
  verifySsl?: boolean;
}
export interface IHomeAssistantOpengarageConfig extends IOpengarageConfig {}

export const opengarageProfile: ISimpleLocalIntegrationProfile = {
  domain: opengarageDomain,
  displayName: "OpenGarage",
  manufacturer: "OpenGarage",
  model: "OpenGarage local HTTP controller",
  defaultName: opengarageDefaultName,
  defaultPort: opengarageDefaultPort,
  defaultHttpPath: '/jc',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "cover",
    "sensor",
  ],
  serviceDomains: [
    "button",
    "cover",
  ],
  controlServices: [
    "close_cover",
    "open_cover",
    "press",
    "stop_cover",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "opengarage",
    "OpenGarage",
    "Opengarage",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/opengarage",
    "upstreamDomain": "opengarage",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "open-garage==0.2.0",
    ],
    "codeowners": [
      "@danielhiversen",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/opengarage",
    "loggers": [
      "opengarage",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "close_cover",
        "open_cover",
        "press",
        "stop_cover",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "cover",
        "sensor",
      ],
      "controls": "native OpenGarage HTTP /cc commands plus injected client.execute or commandExecutor",
    },
    "localApi": {
      "status": "Native local HTTP polling is implemented using GET /jc. Cover click and restart commands are implemented using GET /cc?dkey=... with click=1 or reboot=1, matching open-garage 0.2.0.",
      "implemented": [
        "native OpenGarage HTTP polling of /jc on port 80",
        "native parsing of OpenGarage door, vehicle, distance, RSSI, temperature, humidity, firmware, MAC, and name data",
        "native OpenGarage cover open/close/stop click control and restart button control through /cc with device key",
        "manual local setup for OpenGarage endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for OpenGarage device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor for unsupported controls",
      ],
      "explicitUnsupported": [
        "OpenGarage pairing or Wi-Fi provisioning flows; Home Assistant only configures an already reachable host and device key",
        "disabling HTTPS certificate verification for self-signed devices; global fetch validates certificates without the Python verify_ssl toggle",
        "OpenGarage controls other than click/restart unless provided by an injected client.execute or commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
