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

export const osramlightifyDomain = "osramlightify";
export const osramlightifyDefaultName = "Osramlightify";

export type TOsramlightifyRawData = TSimpleLocalRawData;
export interface IOsramlightifySnapshot extends ISimpleLocalSnapshot {}
export interface IOsramlightifyConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  includeGroups?: boolean;
  allowLightifyGroups?: boolean;
  allow_lightify_groups?: boolean;
}
export interface IHomeAssistantOsramlightifyConfig extends IOsramlightifyConfig {}

export const osramlightifyDefaultPort = 4000;
export const osramlightifyDefaultTimeoutMs = 10000;

export interface IOsramlightifyLightRaw {
  address: string;
  typeId: number;
  deviceType: string;
  deviceSubType: string;
  deviceName: string;
  name: string;
  reachable: boolean;
  lastSeenMinutes: number;
  on: boolean;
  luminance: number;
  temperatureKelvin: number;
  rgb: [number, number, number];
  alpha: number;
  groups: number[];
  firmwareVersion: string;
  supportedFeatures: string[];
  index: number;
}

export interface IOsramlightifyGroupRaw {
  id: number;
  name: string;
  lights: string[];
  lightNames: string[];
  scenes: string[];
  reachable: boolean;
  on: boolean;
  luminance: number;
  temperatureKelvin: number;
  rgb: [number, number, number];
  supportedFeatures: string[];
}

export interface IOsramlightifySceneRaw {
  id: number;
  name: string;
  groupId?: number;
}

export interface IOsramlightifyRawApiData {
  device: {
    id: string;
    name: string;
    manufacturer: string;
    model: string;
    host?: string;
    port?: number;
    protocol: 'tcp';
  };
  lights: IOsramlightifyLightRaw[];
  groups: IOsramlightifyGroupRaw[];
  scenes: IOsramlightifySceneRaw[];
  entities: ISimpleLocalSnapshot['entities'];
  fetchedAt: string;
}

export const osramlightifyProfile: ISimpleLocalIntegrationProfile = {
  domain: osramlightifyDomain,
  displayName: "Osramlightify",
  manufacturer: "Osramlightify",
  model: "Osramlightify local integration",
  defaultName: osramlightifyDefaultName,
  defaultPort: osramlightifyDefaultPort,
  defaultProtocol: 'local',
  status: "control-runtime",
  platforms: [
    "light",
  ],
  serviceDomains: [
    "light",
  ],
  controlServices: [
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "osramlightify",
    "Osramlightify",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/osramlightify",
    "upstreamDomain": "osramlightify",
    "iotClass": "local_polling",
    "qualityScale": "legacy",
    "requirements": [
      "lightify==1.0.7.3",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/osramlightify",
    "loggers": [
      "lightify",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "light",
      ],
      "controls": "delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "native local TCP polling/control for OSRAM Lightify gateways on port 4000 using the binary protocol implemented by Home Assistant's lightify==1.0.7.3 dependency",
        "manual local setup for Osramlightify endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Osramlightify device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor when no TCP gateway endpoint is configured",
      ],
      "explicitUnsupported": [
        "Lightify pairing, Zigbee mesh management, sensors beyond gateway-reported values, and cloud account operations not exposed by the local TCP gateway protocol",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without either a configured TCP gateway endpoint or injected client.execute/commandExecutor",
      ],
    },
  },
};
