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

export const obihaiDomain = "obihai";
export const obihaiDefaultName = "Obihai";
export const obihaiDefaultPort = 80;
export const obihaiDefaultUsername = 'admin';
export const obihaiDefaultPassword = 'admin';
export const obihaiDefaultStatusPath = 'DI_S_.xml';
export const obihaiDefaultLinePath = 'PI_FXS_1_Stats.xml';
export const obihaiDefaultCallStatusPath = 'callstatus.htm';
export const obihaiDefaultRebootPath = 'rebootgetconfig.htm';
export const obihaiDefaultTimeoutMs = 2000;

export interface IObihaiRawData {
  deviceInfo?: Record<string, unknown>;
  services?: Record<string, unknown>;
  lineServices?: Record<string, unknown>;
  callDirection?: Record<string, unknown>;
  fetchedAt?: string;
  errors?: Record<string, string>;
  [key: string]: unknown;
}

export type TObihaiRawData = IObihaiRawData | TSimpleLocalRawData;
export interface IObihaiSnapshot extends ISimpleLocalSnapshot {}
export interface IObihaiConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantObihaiConfig extends IObihaiConfig {}

export const obihaiProfile: ISimpleLocalIntegrationProfile = {
  domain: obihaiDomain,
  displayName: "Obihai",
  manufacturer: "Obihai",
  model: "Obihai local integration",
  defaultName: obihaiDefaultName,
  defaultPort: obihaiDefaultPort,
  defaultHttpPath: obihaiDefaultStatusPath,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "button",
    "sensor",
  ],
  serviceDomains: [
    "button",
  ],
  controlServices: [
    "press",
  ],
  discoverySources: [
    'manual',
    'dhcp',
    'custom',
  ],
  discoveryKeywords: [
    "obihai",
    "Obihai",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/obihai",
    "upstreamDomain": "obihai",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "pyobihai==1.4.2",
    ],
    "codeowners": [
      "@dshokouhi",
      "@ejpenney",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/obihai",
    "loggers": [
      "pyobihai",
    ],
    "runtime": {
      "type": "control-runtime",
      "polling": "local Obihai HTTP Digest endpoints DI_S_.xml, PI_FXS_1_Stats.xml, and callstatus.htm",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "press",
      ],
      "platforms": [
        "button",
        "sensor",
      ],
      "controls": "local HTTP reboot endpoint or delegated injected client.execute/commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Obihai HTTP endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "live HTTP Digest reads for DI_S_.xml, PI_FXS_1_Stats.xml, and callstatus.htm using the same paths used by pyobihai",
        "runtime mapping for Obihai service, line, call-direction, reboot, and Home Assistant platform metadata",
        "real HTTP reboot request to rebootgetconfig.htm when a live endpoint is configured",
        "safe command delegation to injected client.execute or commandExecutor",
      ],
      "explicitUnsupported": [
        "claiming reboot success for static snapshots or manual rawData without a live HTTP endpoint, injected client.execute, or commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
        "non-HTTP provisioning, pairing, or cloud account flows; pyobihai exposes only local HTTP Digest pages for this integration",
      ],
    },
  },
};
