import type { Deployment } from '../../../../api/queries/admin/apps';
import type { DeploymentPageFormState } from '../hooks/useDeploymentPageState';

type DeployedConfigSnapshot = NonNullable<Deployment['deployedConfigSnapshot']>;

/**
 * Maps a deployment snapshot to the page's form-state shape.
 * Used by both the live page's "restore" action and the read-only ConfigPanel
 * in the checkpoint detail drawer.
 *
 * Fields not represented in the snapshot fall back to `defaults`.
 */
export function snapshotToForm(
  snapshot: DeployedConfigSnapshot,
  defaults: DeploymentPageFormState,
): DeploymentPageFormState {
  const a = snapshot.agentConfig ?? {};
  const c = snapshot.containerConfig ?? {};
  return {
    monitoring: {
      engineLevel: (a.engineLevel as string) ?? defaults.monitoring.engineLevel,
      payloadCaptureMode: (a.payloadCaptureMode as string) ?? defaults.monitoring.payloadCaptureMode,
      payloadSize: defaults.monitoring.payloadSize,
      payloadUnit: defaults.monitoring.payloadUnit,
      applicationLogLevel: (a.applicationLogLevel as string) ?? defaults.monitoring.applicationLogLevel,
      agentLogLevel: (a.agentLogLevel as string) ?? defaults.monitoring.agentLogLevel,
      metricsEnabled: (a.metricsEnabled as boolean) ?? defaults.monitoring.metricsEnabled,
      metricsInterval: defaults.monitoring.metricsInterval,
      samplingRate: a.samplingRate !== undefined ? String(a.samplingRate) : defaults.monitoring.samplingRate,
      compressSuccess: (a.compressSuccess as boolean) ?? defaults.monitoring.compressSuccess,
      replayEnabled: defaults.monitoring.replayEnabled,
      routeControlEnabled: defaults.monitoring.routeControlEnabled,
    },
    resources: {
      memoryLimit: c.memoryLimitMb !== undefined ? String(c.memoryLimitMb) : defaults.resources.memoryLimit,
      memoryReserve: c.memoryReserveMb != null ? String(c.memoryReserveMb) : defaults.resources.memoryReserve,
      cpuRequest: c.cpuRequest !== undefined ? String(c.cpuRequest) : defaults.resources.cpuRequest,
      cpuLimit: c.cpuLimit != null ? String(c.cpuLimit) : defaults.resources.cpuLimit,
      appPort: c.appPort !== undefined ? String(c.appPort) : defaults.resources.appPort,
      replicas: c.replicas !== undefined ? String(c.replicas) : defaults.resources.replicas,
      deployStrategy: (c.deploymentStrategy as string) ?? defaults.resources.deployStrategy,
      stripPrefix: c.stripPathPrefix !== undefined ? (c.stripPathPrefix as boolean) : defaults.resources.stripPrefix,
      sslOffloading: c.sslOffloading !== undefined ? (c.sslOffloading as boolean) : defaults.resources.sslOffloading,
      externalRouting: c.externalRouting !== undefined ? (c.externalRouting as boolean) : defaults.resources.externalRouting,
      runtimeType: (c.runtimeType as string) ?? defaults.resources.runtimeType,
      customArgs: c.customArgs !== undefined ? String(c.customArgs ?? '') : defaults.resources.customArgs,
      extraNetworks: Array.isArray(c.extraNetworks) ? (c.extraNetworks as string[]) : defaults.resources.extraNetworks,
    },
    variables: {
      envVars: c.customEnvVars
        ? Object.entries(c.customEnvVars as Record<string, string>).map(([key, value]) => ({ key, value }))
        : defaults.variables.envVars,
    },
    sensitiveKeys: {
      sensitiveKeys: Array.isArray(snapshot.sensitiveKeys)
        ? snapshot.sensitiveKeys
        : Array.isArray(a.sensitiveKeys)
          ? (a.sensitiveKeys as string[])
          : defaults.sensitiveKeys.sensitiveKeys,
    },
  };
}
