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

export const musicAssistantDomain = "music_assistant";
export const musicAssistantDefaultName = "Music Assistant";
export const musicAssistantDefaultPort = 8095;
export const musicAssistantDefaultTimeoutMs = 10000;
export const musicAssistantAuthSchemaVersion = 28;

export interface IMusicAssistantServerInfo extends Record<string, unknown> {
  server_id: string;
  server_version: string;
  schema_version: number;
  min_supported_schema_version: number;
  base_url: string;
  homeassistant_addon?: boolean;
  onboard_done?: boolean;
  name?: string | null;
  status?: string;
}

export interface IMusicAssistantPlayer extends Record<string, unknown> {
  player_id: string;
  provider?: string;
  type?: string;
  name?: string;
  display_name?: string;
  available?: boolean;
  playback_state?: string;
  state?: string;
  powered?: boolean | null;
  volume_level?: number | null;
  volume_muted?: boolean | null;
  active_source?: string | null;
  group_members?: string[];
  group_childs?: string[];
  expose_to_ha?: boolean;
  current_media?: Record<string, unknown> | null;
  device_info?: {
    model?: string;
    manufacturer?: string;
    software_version?: string | null;
    identifiers?: Record<string, string>;
    ip_address?: string | null;
    mac_address?: string | null;
  };
  options?: Array<Record<string, unknown>>;
}

export interface IMusicAssistantPlayerQueue extends Record<string, unknown> {
  queue_id: string;
  active?: boolean;
  display_name?: string;
  available?: boolean;
  items?: number;
  shuffle_enabled?: boolean;
  repeat_mode?: string;
  current_index?: number | null;
  elapsed_time?: number;
  elapsed_time_last_updated?: number;
  state?: string;
  current_item?: Record<string, unknown> | null;
  next_item?: Record<string, unknown> | null;
}

export interface IMusicAssistantNativeRawData extends Record<string, unknown> {
  info: IMusicAssistantServerInfo;
  players: IMusicAssistantPlayer[];
  queues: IMusicAssistantPlayerQueue[];
  fetchedAt?: string;
}

export type TMusicAssistantRawData = TSimpleLocalRawData | IMusicAssistantNativeRawData;
export interface IMusicAssistantSnapshot extends ISimpleLocalSnapshot {}
export interface IMusicAssistantConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantMusicAssistantConfig extends IMusicAssistantConfig {}

export const musicAssistantProfile: ISimpleLocalIntegrationProfile = {
  domain: musicAssistantDomain,
  displayName: "Music Assistant",
  manufacturer: "Music Assistant",
  model: "Music Assistant local integration",
  defaultName: musicAssistantDefaultName,
  defaultPort: musicAssistantDefaultPort,
  defaultHttpPath: '/info',
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "button",
    "media_player",
    "number",
    "select",
    "switch",
    "text",
  ],
  serviceDomains: [
    "button",
    "media_player",
    "number",
    "select",
    "switch",
    "text",
  ],
  controlServices: [
    "get_library",
    "get_queue",
    "media_pause",
    "media_play",
    "play_announcement",
    "play_media",
    "press",
    "search",
    "select_option",
    "set_value",
    "toggle",
    "transfer_queue",
    "turn_off",
    "turn_on",
    "volume_down",
    "volume_up",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "music_assistant",
    "Music Assistant",
    "Music",
    "Assistant",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/music_assistant",
    "upstreamDomain": "music_assistant",
    "integrationType": "service",
    "iotClass": "local_push",
    "qualityScale": "bronze",
    "requirements": [
      "music-assistant-client==1.3.5",
    ],
    "dependencies": [
      "auth",
    ],
    "afterDependencies": [
      "media_source",
    ],
    "codeowners": [
      "@music-assistant",
      "@arturpragacz",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/music_assistant",
    "loggers": [
      "music_assistant",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "get_library",
        "get_queue",
        "media_pause",
        "media_play",
        "play_announcement",
        "play_media",
        "press",
        "search",
        "select_option",
        "set_value",
        "toggle",
        "transfer_queue",
        "turn_off",
        "turn_on",
        "volume_down",
        "volume_up",
      ],
      "platforms": [
        "button",
        "media_player",
        "number",
        "select",
        "switch",
        "text",
      ],
      "controls": "native local WebSocket commands when config.host/config.url is provided; otherwise delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "status": "native local HTTP /info discovery and Music Assistant WebSocket JSON command client implemented for reachable local servers; schema 28+ requires a local Music Assistant token.",
      "implemented": [
        "manual local setup for Music Assistant host/url endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "native local HTTP /info server discovery",
        "native local WebSocket JSON commands for auth, players/all, player_queues/all, player controls, queue controls, search, library, and queue actions",
        "runtime mapping for Music Assistant players, queues, and player options into Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor when no reachable local Music Assistant endpoint is configured",
      ],
      "explicitUnsupported": [
        "browser redirect pairing/reauth flow; provide an existing Music Assistant token for schema 28+ servers or username/password for local /auth/login",
        "long-lived Music Assistant event subscription/push listener; this runtime performs explicit request/response WebSocket commands and snapshots only",
        "claiming live Music Assistant command success without config.host/config.url, an injected native client, or commandExecutor",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success for snapshot-only configs without a live endpoint, injected client.execute, or commandExecutor",
      ],
    },
  },
};
