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

export const pencomDomain = "pencom";
export const pencomDefaultName = "Pencom";
export const pencomRelaysPerBoard = 8;
export const pencomDefaultTimeoutMs = 5000;

export interface IPencomRelayConfig extends Record<string, unknown> {
  id?: string;
  name: string;
  board?: number;
  addr: number;
}

export interface IPencomRelayState extends IPencomRelayConfig {
  state: boolean | null;
}

export interface IPencomRawApiData extends Record<string, unknown> {
  boards?: number;
  relays?: IPencomRelayState[];
  states?: Array<Array<boolean | null>>;
  fetchedAt?: string;
}

export type TPencomRawData = TSimpleLocalRawData | IPencomRawApiData;
export interface IPencomSnapshot extends ISimpleLocalSnapshot {}
export interface IPencomConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  boards?: number;
  relays?: IPencomRelayConfig[];
}
export interface IHomeAssistantPencomConfig extends IPencomConfig {}

export const pencomProfile: ISimpleLocalIntegrationProfile = {
  domain: pencomDomain,
  displayName: "Pencom",
  manufacturer: "Pencom",
  model: "Pencom local integration",
  defaultName: pencomDefaultName,
  defaultProtocol: 'tcp',
  status: "control-runtime",
  platforms: [
    "switch",
  ],
  serviceDomains: [
    "switch",
  ],
  controlServices: [
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "pencom",
    "Pencom",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/pencom",
    "upstreamDomain": "pencom",
    "iotClass": "local_polling",
    "qualityScale": "legacy",
    "requirements": [
      "pencompy==0.0.3",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/pencom",
    "loggers": [
      "pencompy",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "switch",
      ],
      "controls": "native Pencom TCP relay turn_on/turn_off/toggle when host and port are configured; otherwise delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Pencom TCP endpoints, configured relay lists, snapshots, rawData, snapshotProvider, and injected native clients",
        "native TCP polling through RS232-to-Ethernet adapters using pencompy 0.0.3 Pencom commands AR0, BR0, etc. with decimal bitmask responses",
        "native TCP relay writes using pencompy 0.0.3 AHn/ALn board and relay commands followed by a board poll",
        "runtime mapping for Pencom relay bitmasks into switch entities",
        "safe command delegation to injected client.execute or commandExecutor when no TCP endpoint is configured",
      ],
      "explicitUnsupported": [
        "serial-only Pencom connections that are not exposed through a TCP RS232-to-Ethernet adapter; no serial-port transport dependency is available",
        "claiming relay write success without a successful TCP command send plus board poll response, or without an injected native client/commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
