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

export const immichDomain = "immich";
export const immichDefaultName = "Immich";

export interface IImmichUserInfo {
  id?: string;
  name?: string;
  email?: string;
  isAdmin?: boolean;
  [key: string]: unknown;
}

export interface IImmichServerAbout {
  version?: string;
  licensed?: boolean;
  versionUrl?: string;
  [key: string]: unknown;
}

export interface IImmichServerStorage {
  diskAvailableRaw?: number;
  diskAvailable?: string;
  diskSizeRaw?: number;
  diskSize?: string;
  diskUsagePercentage?: number;
  diskUseRaw?: number;
  diskUse?: string;
  [key: string]: unknown;
}

export interface IImmichServerStatistics {
  photos?: number;
  videos?: number;
  usage?: number;
  usagePhotos?: number;
  usageVideos?: number;
  usageByUser?: Array<Record<string, unknown>>;
  [key: string]: unknown;
}

export interface IImmichServerVersionCheck {
  checkedAt?: string | null;
  releaseVersion?: string | null;
  [key: string]: unknown;
}

export interface IImmichRawData extends Record<string, unknown> {
  user?: IImmichUserInfo;
  server_about?: IImmichServerAbout;
  server_storage?: IImmichServerStorage;
  server_usage?: IImmichServerStatistics | null;
  server_version_check?: IImmichServerVersionCheck | null;
}

export type TImmichRawData = TSimpleLocalRawData | IImmichRawData;
export interface IImmichSnapshot extends ISimpleLocalSnapshot {}
export interface IImmichConfig extends ISimpleLocalConfig {
  url?: string;
  ssl?: boolean;
  verifySsl?: boolean;
  deviceId?: string;
}

export const immichProfile: ISimpleLocalIntegrationProfile = {
  domain: immichDomain,
  displayName: "Immich",
  manufacturer: "Immich",
  model: "Immich local integration",
  defaultName: immichDefaultName,
  defaultPort: 2283,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "sensor",
    "update",
  ],
  serviceDomains: [],
  controlServices: [
    "upload_file",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "immich",
    "Immich",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/immich",
    "upstreamDomain": "immich",
    "integrationType": "service",
    "iotClass": "local_polling",
    "qualityScale": "platinum",
    "requirements": [
      "aioimmich==0.14.0",
    ],
    "dependencies": [
      "http",
    ],
    "codeowners": [
      "@mib1185",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/immich",
    "loggers": [
      "aioimmich",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "upload_file",
      ],
      "platforms": [
        "sensor",
        "update",
      ],
      "controls": "native local upload_file via Immich HTTP API when endpoint and API key are configured; otherwise delegated only to an injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "native local Immich HTTP API polling with x-api-key authentication for /api/users/me, /api/server/about, /api/server/storage, /api/server/statistics, and /api/server/version-check",
        "native upload_file service using local file reads and POST /api/assets, with optional PUT /api/albums/{album_id}/assets",
        "manual local setup for Immich endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Immich server storage, usage, and update metadata",
      ],
      "explicitUnsupported": [
        "Immich media_source browsing/playback proxy endpoints from Home Assistant",
        "disabling TLS certificate verification for self-signed HTTPS endpoints when using global fetch",
        "claiming live Immich polling or upload success without config.url/config.host plus config.apiKey/config.token, an injected native client, commandExecutor, snapshotProvider, snapshot, or rawData",
        "Home Assistant entity registry migrations and repair flows",
        "cloud account discovery; Immich is treated as a user-supplied local HTTP endpoint",
      ],
    },
  },
};
