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

export const otbrDomain = "otbr";
export const otbrDefaultName = "Open Thread Border Router";

export type TOtbrRawData = TSimpleLocalRawData;
export interface IOtbrSnapshot extends ISimpleLocalSnapshot {}
export interface IOtbrConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantOtbrConfig extends IOtbrConfig {}

export const otbrDefaultTimeoutMs = 10000;

export interface IOtbrDatasetRaw {
  json?: Record<string, unknown>;
  tlvs?: string;
  channel?: number;
  networkName?: string;
  panId?: string | number;
  extendedPanId?: string;
}

export interface IOtbrRawApiData {
  device: {
    id: string;
    name: string;
    manufacturer: string;
    model: string;
    host?: string;
    port?: number;
    protocol: 'http' | 'https';
  };
  borderAgentId?: string;
  extendedAddress?: string;
  activeDataset?: IOtbrDatasetRaw;
  coprocessorVersion?: string;
  keyFormat: 'camel' | 'pascal';
  entities: ISimpleLocalSnapshot['entities'];
  fetchedAt: string;
}

export const otbrProfile: ISimpleLocalIntegrationProfile = {
  domain: otbrDomain,
  displayName: "Open Thread Border Router",
  manufacturer: "Open Thread Border Router",
  model: "Open Thread Border Router local integration",
  defaultName: otbrDefaultName,
  defaultProtocol: 'local',
  status: "read-only-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "otbr",
    "Open Thread Border Router",
    "Otbr",
    "Open",
    "Thread",
    "Border",
    "Router",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/otbr",
    "upstreamDomain": "otbr",
    "integrationType": "service",
    "iotClass": "local_polling",
    "requirements": [
      "python-otbr-api==2.10.0",
    ],
    "dependencies": [
      "homeassistant_hardware",
      "thread",
    ],
    "afterDependencies": [
      "hassio",
      "homeassistant_yellow",
      "zha",
    ],
    "codeowners": [
      "@home-assistant/core",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/otbr",
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "native local HTTP polling for the OpenThread Border Router REST API used by python-otbr-api==2.10.0 (/api/actions, /node/ba-id, /node/dataset/active, /node/ext-address, /node/coprocessor/version)",
        "manual local setup for Open Thread Border Router endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Open Thread Border Router device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor for OTBR write operations not exposed as runtime services here",
      ],
      "explicitUnsupported": [
        "Thread dataset creation/import, channel changes, factory reset, ZHA multiprotocol coordination, Home Assistant repair issue management, and websocket APIs beyond read-only REST polling unless called through an injected executor",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without injected client.execute or commandExecutor",
      ],
    },
  },
};
