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

export const octoprintDomain = "octoprint";
export const octoprintDefaultName = "OctoPrint";
export const octoprintDefaultPort = 80;
export const octoprintDefaultPath = '/';
export const octoprintDefaultTimeoutMs = 5000;

export interface IOctoprintRawData {
  job?: Record<string, unknown>;
  printer?: Record<string, unknown> | null;
  settings?: Record<string, unknown>;
  printerOffline?: boolean;
  fetchedAt?: string;
  errors?: Record<string, string>;
  [key: string]: unknown;
}

export type TOctoprintRawData = IOctoprintRawData | TSimpleLocalRawData;
export interface IOctoprintSnapshot extends ISimpleLocalSnapshot {}
export interface IOctoprintConfig extends ISimpleLocalConfig {
  url?: string;
  ssl?: boolean;
  verifySsl?: boolean;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantOctoprintConfig extends IOctoprintConfig {}

export const octoprintProfile: ISimpleLocalIntegrationProfile = {
  domain: octoprintDomain,
  displayName: "OctoPrint",
  manufacturer: "OctoPrint",
  model: "OctoPrint local integration",
  defaultName: octoprintDefaultName,
  defaultPort: octoprintDefaultPort,
  defaultHttpPath: octoprintDefaultPath,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "button",
    "number",
    "sensor",
  ],
  serviceDomains: [
    "button",
    "number",
  ],
  controlServices: [
    "press",
    "printer_connect",
    "set_value",
  ],
  discoverySources: [
    'manual',
    'mdns',
    'ssdp',
    'custom',
  ],
  discoveryKeywords: [
    "octoprint",
    "OctoPrint",
    "Octoprint",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/octoprint",
    "upstreamDomain": "octoprint",
    "integrationType": "service",
    "iotClass": "local_polling",
    "requirements": [
      "pyoctoprintapi==0.1.14",
    ],
    "codeowners": [
      "@rfleming71",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/octoprint",
    "loggers": [
      "pyoctoprintapi",
    ],
    "runtime": {
      "type": "control-runtime",
      "polling": "local OctoPrint REST API at /api/job, /api/printer, and /api/settings",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "press",
        "printer_connect",
        "set_value",
      ],
      "platforms": [
        "binary_sensor",
        "button",
        "number",
        "sensor",
      ],
      "controls": "local OctoPrint REST commands with X-Api-Key or delegated injected client.execute/commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for OctoPrint HTTP(S) endpoints, API keys, snapshots, rawData, snapshotProvider, and injected native clients",
        "live REST reads for job, printer, and settings using the same endpoints used by pyoctoprintapi",
        "runtime mapping for OctoPrint job, printer state, temperatures, writable target temperatures, and action buttons",
        "real REST writes for job pause/resume/cancel, system shutdown/reboot/restart, printer connect, and bed/tool target temperatures",
        "safe command delegation to injected client.execute or commandExecutor",
      ],
      "explicitUnsupported": [
        "claiming write success for static snapshots or manual rawData without a live REST endpoint, injected client.execute, or commandExecutor",
        "interactive application-key pairing/approval flow; provide config.apiKey or inject a client after pairing",
        "camera entities because the shared runtime entity model has no camera platform",
        "Home Assistant entity registry migrations and repair flows",
        "TLS certificate bypass through Node fetch; use HTTP, a valid certificate, or an injected client if OctoPrint uses a self-signed HTTPS certificate",
      ],
    },
  },
};
