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

export const lametricDomain = "lametric";
export const lametricDefaultName = "LaMetric";
export const lametricDefaultHttpsPort = 4343;
export const lametricDefaultHttpPort = 8080;
export const lametricDefaultTimeoutMs = 8000;

export type TLametricProtocol = 'http' | 'https';
export type TLametricRawData = TSimpleLocalRawData | ILametricDeviceData;

export interface ILametricRangeData extends Record<string, unknown> {
  min?: number;
  max?: number;
}

export interface ILametricAudioData extends Record<string, unknown> {
  available?: boolean;
  volume?: number;
  volume_range?: ILametricRangeData;
  volume_limit?: ILametricRangeData;
}

export interface ILametricBluetoothData extends Record<string, unknown> {
  active?: boolean;
  available?: boolean;
  discoverable?: boolean;
  pairable?: boolean;
  name?: string;
  mac?: string;
  address?: string;
}

export interface ILametricDisplayData extends Record<string, unknown> {
  brightness?: number;
  brightness_mode?: 'auto' | 'manual' | string;
  brightness_range?: ILametricRangeData;
  brightness_limit?: ILametricRangeData;
  height?: number;
  width?: number;
  type?: string;
}

export interface ILametricWifiData extends Record<string, unknown> {
  active?: boolean;
  available?: boolean;
  encryption?: string;
  essid?: string;
  ssid?: string;
  ip?: string;
  ipv4?: string;
  address?: string;
  mac?: string;
  mode?: string;
  netmask?: string;
  rssi?: number;
  strength?: number;
  signal_strength?: number;
}

export interface ILametricDeviceData extends Record<string, unknown> {
  id?: string | number;
  name?: string;
  serial_number?: string;
  serialNumber?: string;
  os_version?: string;
  osVersion?: string;
  mode?: string;
  model?: string;
  update_available?: { version?: string } | null;
  update?: { version?: string } | null;
  audio?: ILametricAudioData | null;
  bluetooth?: ILametricBluetoothData | null;
  display?: ILametricDisplayData;
  wifi?: ILametricWifiData;
}

export interface ILametricSnapshot extends ISimpleLocalSnapshot {}
export interface ILametricConfig extends ISimpleLocalConfig {
  url?: string;
  protocol?: TLametricProtocol;
  verifyTls?: boolean;
  macAddress?: string;
  deviceId?: string;
  serialNumber?: string;
  model?: string;
  rawData?: TLametricRawData;
  client?: ISimpleLocalClientLike;
  commandExecutor?: ISimpleLocalCommandExecutor;
}

export const lametricProfile: ISimpleLocalIntegrationProfile = {
  domain: lametricDomain,
  displayName: "LaMetric",
  manufacturer: "LaMetric",
  model: "LaMetric local integration",
  defaultName: lametricDefaultName,
  defaultPort: lametricDefaultHttpsPort,
  defaultHttpPath: '/api/v2/device',
  defaultProtocol: 'https',
  status: "control-runtime",
  platforms: [
    "button",
    "number",
    "select",
    "sensor",
    "switch",
    "update",
  ],
  serviceDomains: [
    "button",
    "number",
    "select",
    "switch",
  ],
  controlServices: [
    "chart",
    "message",
    "press",
    "select_option",
    "set_value",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "lametric",
    "LaMetric",
    "Lametric",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/lametric",
    "upstreamDomain": "lametric",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "demetriek==1.3.0",
    ],
    "dependencies": [
      "application_credentials",
    ],
    "codeowners": [
      "@robbiet480",
      "@frenck",
      "@bachya",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/lametric",
    "loggers": [
      "demetriek",
    ],
    "runtime": {
      "type": "control-runtime",
      "status": "native local HTTPS Device API polling and controls when host plus device API key are configured; snapshot/rawData/injected-client modes remain read-only unless a real executor is injected",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "chart",
        "message",
        "press",
        "select_option",
        "set_value",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "button",
        "number",
        "select",
        "sensor",
        "switch",
        "update",
      ],
      "controls": "native local Device API controls for message/chart/app navigation/display/audio/bluetooth/notification dismissal, or delegated to injected client.execute/commandExecutor",
    },
    "localApi": {
      "implemented": [
        "GET https://host:4343/api/v2/device with Basic dev:<apiKey> authentication for local device snapshots",
        "PUT /api/v2/device/display, /audio, /bluetooth for brightness, brightness mode, volume, and Bluetooth state",
        "PUT /api/v2/device/apps/next and /prev for app navigation buttons",
        "POST /api/v2/device/notifications for message and chart notifications; GET/DELETE notifications for dismiss buttons",
        "runtime mapping for LaMetric sensor, number, select, switch, button, and update platform metadata",
        "snapshot/rawData/injected-client operation without claiming live writes for static data",
      ],
      "explicitUnsupported": [
        "LaMetric cloud OAuth device import and developer portal API-key retrieval; local API requires an already configured device API key",
        "streaming API websocket/video frame streaming",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success for static snapshots/manual rawData without a host, device API key, injected client.execute, or commandExecutor",
      ],
    },
  },
};
