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

export const lidarrDomain = "lidarr";
export const lidarrDefaultName = "Lidarr";
export const lidarrDefaultPort = 8686;
export const lidarrDefaultMaxRecords = 20;

export interface ILidarrSystemStatus extends Record<string, unknown> {
  version?: string;
  appName?: string;
  instanceName?: string;
  startTime?: string;
  branch?: string;
  authentication?: string;
}

export interface ILidarrRootFolder extends Record<string, unknown> {
  id?: number;
  path?: string;
  accessible?: boolean;
  freeSpace?: number;
  totalSpace?: number;
}

export interface ILidarrQueueItem extends Record<string, unknown> {
  title?: string;
  sizeleft?: number;
  timeleft?: string;
  trackedDownloadState?: string;
}

export interface ILidarrPagedRecords<TRecord extends Record<string, unknown>> extends Record<string, unknown> {
  totalRecords?: number;
  records?: TRecord[];
}

export interface ILidarrAlbum extends Record<string, unknown> {
  title?: string;
  artist?: {
    artistName?: string;
  };
}

export interface ILidarrLiveRawData extends Record<string, unknown> {
  systemStatus?: ILidarrSystemStatus;
  rootFolders?: ILidarrRootFolder[];
  queue?: ILidarrPagedRecords<ILidarrQueueItem>;
  wanted?: ILidarrPagedRecords<ILidarrAlbum>;
  albums?: ILidarrAlbum[];
  fetchedAt?: string;
}

export type TLidarrRawData = TSimpleLocalRawData | ILidarrLiveRawData;
export interface ILidarrSnapshot extends ISimpleLocalSnapshot {}
export interface ILidarrConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  apiKey?: string;
  maxRecords?: number;
}
export interface IHomeAssistantLidarrConfig extends ILidarrConfig {}

export const lidarrProfile: ISimpleLocalIntegrationProfile = {
  domain: lidarrDomain,
  displayName: "Lidarr",
  manufacturer: "Lidarr",
  model: "Lidarr local integration",
  defaultName: lidarrDefaultName,
  defaultPort: lidarrDefaultPort,
  defaultProtocol: 'local',
  status: "read-only-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "lidarr",
    "Lidarr",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/lidarr",
    "upstreamDomain": "lidarr",
    "integrationType": "service",
    "iotClass": "local_polling",
    "requirements": [
      "aiopyarr==23.4.0",
    ],
    "codeowners": [
      "@tkdrob",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/lidarr",
    "loggers": [
      "aiopyarr",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "native local Lidarr HTTP API polling with X-Api-Key against /api/v1/system/status, /rootfolder, /queue, /wanted/missing, and /album",
        "manual local setup for Lidarr snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Lidarr version, disk-space, queue, wanted, and album-count sensors",
        "safe command delegation to injected client.execute or commandExecutor only",
      ],
      "explicitUnsupported": [
        "aiopyarr zeroconf API-key discovery; native HTTP polling requires config.apiKey or config.token",
        "Lidarr write/control services; this integration only reads sensors unless an injected executor handles commands",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without injected client.execute or commandExecutor",
      ],
    },
  },
};
