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

export const mfiDomain = "mfi";
export const mfiDefaultName = "Ubiquiti mFi mPort";
export const mfiDefaultHttpsPort = 6443;
export const mfiDefaultHttpPort = 6080;
export const mfiDefaultTimeoutMs = 8000;

export interface IMfiNativeRawData {
  devices?: Array<Record<string, unknown>>;
  sensors?: Array<Record<string, unknown>>;
  fetchedAt?: string;
}

export interface IMfiPortSnapshot {
  id: string;
  label: string;
  model: string;
  tag?: string;
  value?: unknown;
  output?: unknown;
  mac?: string;
  port?: string;
  data: Record<string, unknown>;
}

export type TMfiRawData = TSimpleLocalRawData | IMfiNativeRawData;
export interface IMfiSnapshot extends ISimpleLocalSnapshot {}
export interface IMfiConfig extends ISimpleLocalConfig {
  url?: string;
  ssl?: boolean;
  verifySsl?: boolean;
  macAddress?: string;
  deviceId?: string;
}
export const mfiProfile: ISimpleLocalIntegrationProfile = {
  domain: mfiDomain,
  displayName: "Ubiquiti mFi mPort",
  manufacturer: "Ubiquiti mFi mPort",
  model: "Ubiquiti mFi mPort local integration",
  defaultName: mfiDefaultName,
  defaultPort: mfiDefaultHttpsPort,
  defaultProtocol: 'https',
  status: "control-runtime",
  platforms: [
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "switch",
  ],
  controlServices: [
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "mfi",
    "Ubiquiti mFi mPort",
    "Mfi",
    "Ubiquiti",
    "mFi",
    "mPort",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/mfi",
    "upstreamDomain": "mfi",
    "iotClass": "local_polling",
    "qualityScale": "legacy",
    "requirements": [
      "mficlient==0.5.0",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/mfi",
    "loggers": [
      "mficlient",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "sensor",
        "switch",
      ],
      "polling": "local mFi Controller HTTP API using /api/v1.0/stat/device and /api/v1.0/list/sensors after /login",
      "controls": "local mFi Controller HTTP /api/v1.0/cmd/devmgr for switch outputs, or injected executor/client",
    },
    "localApi": {
      "implemented": [
        "local mFi Controller login via GET / and POST /login with username/password form credentials",
        "local polling through GET /api/v1.0/stat/device and POST /api/v1.0/list/sensors, compatible with mficlient==0.5.0",
        "runtime mapping for Ubiquiti mFi mPort sensor and switch ports using Home Assistant model/tag rules",
        "local switch control through POST /api/v1.0/cmd/devmgr for Outlet, Output 5v, Output 12v, Output 24v, and Dimmer Switch ports",
        "manual snapshots/rawData/snapshotProvider and injected native clients/executors",
      ],
      "explicitUnsupported": [
        "disabling TLS verification for self-signed mFi Controller HTTPS certificates when using global fetch; use HTTP, a trusted certificate, or an injected client",
        "mFi Controller pairing/adoption and controller setup flows beyond username/password login",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without a configured local HTTP endpoint, injected client.execute, or commandExecutor",
      ],
    },
  },
};
