import type { ControlPlaneRegistrySpec } from "./hwlab-node-control-plane-model";

export function controlPlaneRegistrySummary(registry: ControlPlaneRegistrySpec): Record<string, unknown> {
  if (registry.mode === "host-docker") {
    return {
      mode: registry.mode,
      endpoint: registry.endpoint,
    };
  }
  return {
    mode: registry.mode,
    endpoint: registry.endpoint,
    namespace: registry.namespace,
    deploymentName: registry.deploymentName,
    serviceName: registry.serviceName,
    pvcName: registry.pvcName,
    storage: registry.storage,
    image: registry.image,
    imagePullPolicy: registry.imagePullPolicy,
    containerPort: registry.containerPort,
    listenHost: registry.listenHost,
    listenPort: registry.listenPort,
    hostNetwork: registry.hostNetwork,
    deleteEnabled: registry.deleteEnabled,
  };
}

export function registryInfraManifest(registry: ControlPlaneRegistrySpec, labels: Record<string, string>): Record<string, unknown>[] {
  if (registry.mode !== "k8s-workload") return [];
  const workloadLabels = { ...labels, "app.kubernetes.io/name": registry.deploymentName };
  const podSpec: Record<string, unknown> = {
    containers: [{
      name: "registry",
      image: registry.image,
      imagePullPolicy: registry.imagePullPolicy,
      env: [
        { name: "REGISTRY_HTTP_ADDR", value: `${registry.listenHost}:${registry.listenPort}` },
        { name: "REGISTRY_STORAGE_DELETE_ENABLED", value: String(registry.deleteEnabled) },
      ],
      ports: [{ name: "http", containerPort: registry.containerPort }],
      volumeMounts: [{ name: "storage", mountPath: "/var/lib/registry" }],
      readinessProbe: { httpGet: { path: "/v2/", port: "http", host: registry.listenHost }, initialDelaySeconds: 3, periodSeconds: 10 },
      livenessProbe: { httpGet: { path: "/v2/", port: "http", host: registry.listenHost }, initialDelaySeconds: 10, periodSeconds: 30 },
    }],
    volumes: [{ name: "storage", persistentVolumeClaim: { claimName: registry.pvcName } }],
  };
  if (registry.hostNetwork) {
    podSpec.hostNetwork = true;
    podSpec.dnsPolicy = "ClusterFirstWithHostNet";
  }
  return [
    {
      apiVersion: "v1",
      kind: "PersistentVolumeClaim",
      metadata: { name: registry.pvcName, namespace: registry.namespace, labels: workloadLabels },
      spec: { accessModes: ["ReadWriteOnce"], resources: { requests: { storage: registry.storage } } },
    },
    {
      apiVersion: "v1",
      kind: "Service",
      metadata: { name: registry.serviceName, namespace: registry.namespace, labels: workloadLabels },
      spec: { type: "ClusterIP", selector: { "app.kubernetes.io/name": registry.deploymentName }, ports: [{ name: "http", port: registry.containerPort, targetPort: "http" }] },
    },
    {
      apiVersion: "apps/v1",
      kind: "Deployment",
      metadata: { name: registry.deploymentName, namespace: registry.namespace, labels: workloadLabels },
      spec: {
        replicas: 1,
        selector: { matchLabels: { "app.kubernetes.io/name": registry.deploymentName } },
        strategy: { type: "Recreate" },
        template: {
          metadata: { labels: workloadLabels },
          spec: podSpec,
        },
      },
    },
  ];
}
