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

export const kwbDomain = "kwb";
export const kwbDefaultName = "KWB Easyfire";
export const kwbDefaultPort = 23;
export const kwbDefaultTimeoutMs = 5000;

export type TKwbSnapshotSource = 'tcp' | 'file' | 'client' | 'manual' | 'snapshot' | 'runtime';
export type TKwbSensorKind = 'temperature' | 'flag' | 'raw';

export interface IKwbSensorSnapshot {
  id: string;
  name: string;
  packet: 'sense' | 'control';
  index: number;
  kind: TKwbSensorKind;
  value: number | string | number[] | null;
  available: boolean;
  unit?: string;
}

export interface IKwbRawData {
  sensors?: IKwbSensorSnapshot[];
  packets?: Array<{
    mode: 'sense' | 'control';
    version: number;
    data: number[];
  }>;
  source?: TKwbSnapshotSource;
  fetchedAt?: string;
}

export type TKwbRawData = TSimpleLocalRawData | IKwbRawData;
export interface IKwbSnapshot extends ISimpleLocalSnapshot {}
export interface IKwbConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  type?: 'tcp' | 'file' | 'serial';
  filePath?: string;
  packetFile?: string;
  raw?: boolean;
  readPacketLimit?: number;
}

export const kwbProfile: ISimpleLocalIntegrationProfile = {
  domain: kwbDomain,
  displayName: "KWB Easyfire",
  manufacturer: "KWB Easyfire",
  model: "KWB Easyfire local integration",
  defaultName: kwbDefaultName,
  defaultPort: kwbDefaultPort,
  defaultProtocol: 'tcp',
  status: "read-only-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "kwb",
    "KWB Easyfire",
    "Kwb",
    "KWB",
    "Easyfire",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/kwb",
    "upstreamDomain": "kwb",
    "iotClass": "local_polling",
    "qualityScale": "legacy",
    "requirements": [
      "pykwb==0.0.8",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/kwb",
    "loggers": [
      "pykwb",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "sensor",
      ],
      "polling": "native KWB Easyfire TCP byte-stream or file capture decoder",
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "native KWB Easyfire TCP byte-stream polling using the pykwb packet decoder semantics",
        "native KWB Easyfire file capture decoding for newline byte dumps or binary captures",
        "runtime mapping for KWB Easyfire device snapshots and Home Assistant platform metadata",
        "snapshot, rawData, snapshotProvider, and injected native client operation",
      ],
      "explicitUnsupported": [
        "serial device polling because Home Assistant uses pykwb with pyserial and this package has no serial-port dependency",
        "KWB write/control operations because the upstream Home Assistant integration exposes sensors only",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success for read-only KWB sensor snapshots",
      ],
    },
  },
};
