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

export const minecraftServerDomain = "minecraft_server";
export const minecraftServerDefaultName = "Minecraft Server";
export const minecraftServerDefaultPort = 25565;
export const minecraftServerDefaultTimeoutMs = 10000;

export type TMinecraftServerType = 'Java Edition' | 'Bedrock Edition' | 'Legacy Java Edition';

export interface IMinecraftServerJavaStatus {
  latency: number;
  motd: string;
  players_max: number;
  players_online: number;
  protocol_version: number;
  version: string;
  players_list?: string[];
  rawResponse?: Record<string, unknown>;
}

export type TMinecraftServerRawData = TSimpleLocalRawData;
export interface IMinecraftServerSnapshot extends ISimpleLocalSnapshot {}
export interface IMinecraftServerConfig extends ISimpleLocalConfig {
  address?: string;
  url?: string;
  serverType?: TMinecraftServerType | string;
  type?: TMinecraftServerType | string;
  protocolVersion?: number;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantMinecraftServerConfig extends IMinecraftServerConfig {}

export const minecraftServerProfile: ISimpleLocalIntegrationProfile = {
  domain: minecraftServerDomain,
  displayName: "Minecraft Server",
  manufacturer: "Minecraft Server",
  model: "Minecraft Server local integration",
  defaultName: minecraftServerDefaultName,
  defaultPort: minecraftServerDefaultPort,
  defaultProtocol: 'tcp',
  status: "read-only-runtime",
  platforms: [
    "binary_sensor",
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "minecraft_server",
    "Minecraft Server",
    "Minecraft",
    "Server",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/minecraft_server",
    "upstreamDomain": "minecraft_server",
    "integrationType": "service",
    "iotClass": "local_polling",
    "qualityScale": "silver",
    "requirements": [
      "mcstatus==13.1.0",
    ],
    "codeowners": [
      "@elmurato",
      "@zachdeibert",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/minecraft_server",
    "loggers": [
      "dnspython",
      "mcstatus",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "binary_sensor",
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "live local TCP Java Edition server-list ping for direct host:port endpoints",
        "Home Assistant-style Java Edition status, version, MOTD, player count, and latency entities",
        "manual local setup for Minecraft Server endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Minecraft Server device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor only",
      ],
      "explicitUnsupported": [
        "Bedrock Edition polling because it requires the UDP Bedrock status protocol used through mcstatus in Home Assistant",
        "Legacy Java Edition ping fallback and DNS SRV auto-lookup without an injected native client",
        "pairing, subscription, or write/control services",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without injected client.execute or commandExecutor",
      ],
    },
  },
};
