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

export const philipsJsDomain = "philips_js";
export const philipsJsDefaultName = "Philips TV";
export const philipsJsDefaultHttpPort = 1925;
export const philipsJsDefaultHttpsPort = 1926;
export const philipsJsDefaultApiVersion = 1;
export const philipsJsDefaultTimeoutMs = 5000;

export interface IPhilipsJsSystemInfo extends Record<string, unknown> {
  name?: string;
  serialnumber?: string;
  softwareversion?: string;
  model?: string;
  deviceid?: string;
  api_version?: {
    Major?: number;
    Minor?: number;
    Patch?: number;
  };
  featuring?: {
    jsonfeatures?: Record<string, string[]>;
    systemfeatures?: Record<string, unknown>;
  };
}

export interface IPhilipsJsRawApiData extends Record<string, unknown> {
  system?: IPhilipsJsSystemInfo;
  powerstate?: Record<string, unknown>;
  volume?: Record<string, unknown>;
  sources?: Record<string, unknown>;
  sourceCurrent?: Record<string, unknown>;
  channel?: Record<string, unknown>;
  activity?: Record<string, unknown>;
  applications?: Record<string, unknown>;
  application?: Record<string, unknown>;
  context?: Record<string, unknown>;
  screenstate?: Record<string, unknown>;
  ambilightMode?: Record<string, unknown>;
  ambilightPower?: Record<string, unknown>;
  ambilightCurrentConfiguration?: Record<string, unknown>;
  hueLampPower?: Record<string, unknown>;
  recordings?: Record<string, unknown>;
  fetchedAt?: string;
}

export type TPhilipsJsRawData = TSimpleLocalRawData | IPhilipsJsRawApiData;
export interface IPhilipsJsSnapshot extends ISimpleLocalSnapshot {}
export interface IPhilipsJsConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  apiVersion?: number;
  allowNotify?: boolean;
}
export interface IHomeAssistantPhilipsJsConfig extends IPhilipsJsConfig {}

export const philipsJsProfile: ISimpleLocalIntegrationProfile = {
  domain: philipsJsDomain,
  displayName: "Philips TV",
  manufacturer: "Philips TV",
  model: "Philips TV local integration",
  defaultName: philipsJsDefaultName,
  defaultPort: philipsJsDefaultHttpPort,
  defaultHttpPath: '/1/system',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "light",
    "media_player",
    "switch",
  ],
  serviceDomains: [
    "light",
    "media_player",
    "remote",
    "switch",
  ],
  controlServices: [
    "media_pause",
    "media_play",
    "media_play_pause",
    "media_stop",
    "press",
    "select_source",
    "send_command",
    "toggle",
    "turn_off",
    "turn_on",
    "volume_mute",
    "volume_down",
    "volume_set",
    "volume_up",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "philips_js",
    "Philips TV",
    "Philips Js",
    "Philips",
    "TV",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/philips_js",
    "upstreamDomain": "philips_js",
    "integrationType": "device",
    "iotClass": "local_polling",
    "requirements": [
      "ha-philipsjs==3.2.4",
    ],
    "codeowners": [
      "@elupus",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/philips_js",
    "loggers": [
      "haphilipsjs",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "media_pause",
        "media_play",
        "media_play_pause",
        "media_stop",
        "press",
        "select_source",
        "send_command",
        "toggle",
        "turn_off",
        "turn_on",
        "volume_mute",
        "volume_down",
        "volume_set",
        "volume_up",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "light",
        "media_player",
        "switch",
      ],
      "controls": "native JointSpace HTTP input/key, audio/volume, powerstate, source, and ambilight writes when host/url is configured and the TV accepts unauthenticated access or supplied digest credentials; otherwise delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Philips TV JointSpace host/url, apiVersion, snapshots, rawData, snapshotProvider, and injected native clients",
        "native local JointSpace HTTP polling from /<api>/system plus powerstate, audio/volume, source/activity, screenstate, ambilight, HueLamp, application, and recording endpoints when supported",
        "HTTP Digest authentication retry for previously paired API v6 username/password credentials",
        "native local JointSpace control for input/key, audio/volume, powerstate, source selection, screenstate, HueLamp, and ambilight endpoints",
        "runtime mapping for Philips TV device snapshots, media player, remote, light, switch, and binary sensor metadata",
        "safe command delegation to injected client.execute or commandExecutor when no JointSpace endpoint is configured",
      ],
      "explicitUnsupported": [
        "initial PIN pairing credential acquisition for API v6 digest_auth_pairing; provide previously paired username/password or an injected native client instead",
        "Wake-on-LAN or IR blaster turn-on fallback for TVs that do not accept JointSpace powerstate On while in standby",
        "TLS certificate verification bypass for self-signed HTTPS TVs; global fetch uses the Node/platform trust store",
        "claiming live Philips TV polling or write success without a reachable JointSpace HTTP(S) endpoint, accepted digest credentials when required, or an injected native client/snapshotProvider/commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
      ],
    },
  },
};
