import {
  decideCicdDeliveryMutation,
  gitRepositoryIdentity,
  pacManualReleaseFix,
  pacReadOnlyNext,
  resolveCicdDeliveryAuthority,
  type CicdDeliveryAuthority,
  type CicdDeliveryMutationDecision,
} from "../cicd-delivery-authority";
import { hwlabRuntimeLaneConfigPath, type HwlabRuntimeLaneSpec } from "../hwlab-node-lanes";

export interface HwlabNodeDeliveryTarget {
  readonly node: string;
  readonly lane: string;
  readonly spec: HwlabRuntimeLaneSpec;
}

export function hwlabNodeDeliveryAuthority(target: HwlabNodeDeliveryTarget): CicdDeliveryAuthority {
  return resolveCicdDeliveryAuthority({
    node: target.node,
    lane: target.lane,
    sourceRepository: gitRepositoryIdentity(target.spec.gitUrl),
    sourceBranch: target.spec.sourceBranch,
    declaredSourceAuthorityMode: target.spec.sourceAuthority?.mode ?? null,
    declaredConfigRef: hwlabRuntimeLaneConfigPath(),
  });
}

export function hwlabNodeDeliveryMutationDecision(
  target: HwlabNodeDeliveryTarget,
  command: string,
  action: string,
): CicdDeliveryMutationDecision {
  return decideCicdDeliveryMutation(hwlabNodeDeliveryAuthority(target), {
    command,
    statusCommand: `bun scripts/cli.ts hwlab nodes control-plane status --node ${target.node} --lane ${target.lane}`,
    action,
    node: target.node,
    lane: target.lane,
  });
}

export function hwlabNodeDeliveryNext(
  target: HwlabNodeDeliveryTarget,
  authority: CicdDeliveryAuthority,
  runtimeStatusFull: string,
): Record<string, unknown> {
  if (authority.kind === "pac-pr-merge") {
    return {
      runtimeStatusFull,
      ...pacReadOnlyNext(authority.consumer.node, authority.consumer.consumerId),
    };
  }
  if (authority.kind === "unknown") {
    return {
      runtimeStatusFull,
      fixManualRelease: pacManualReleaseFix(authority),
      valuesPrinted: false,
    };
  }
  return {
    full: runtimeStatusFull,
    plan: `bun scripts/cli.ts hwlab nodes control-plane plan --node ${target.node} --lane ${target.lane}`,
    triggerCurrent: `bun scripts/cli.ts hwlab nodes control-plane trigger-current --node ${target.node} --lane ${target.lane} --confirm`,
    gitMirrorFlush: `bun scripts/cli.ts hwlab nodes git-mirror flush --node ${target.node} --lane ${target.lane} --confirm --wait`,
    webProbe: `bun scripts/cli.ts web-probe run --node ${target.node} --lane ${target.lane}`,
  };
}

export function hwlabNodePlanNext(
  target: HwlabNodeDeliveryTarget,
  authority: CicdDeliveryAuthority,
): Record<string, unknown> {
  const status = `bun scripts/cli.ts hwlab nodes control-plane status --node ${target.node} --lane ${target.lane}`;
  if (authority.kind === "pac-pr-merge") {
    return pacReadOnlyNext(authority.consumer.node, authority.consumer.consumerId);
  }
  if (authority.kind === "unknown") {
    return {
      status,
      history: `bun scripts/cli.ts platform-infra pipelines-as-code history --target ${target.node} --limit 10`,
      fixManualRelease: pacManualReleaseFix(authority),
      valuesPrinted: false,
    };
  }
  return {
    status,
    platformMaintenanceHelp: "bun scripts/cli.ts hwlab nodes control-plane platform-maintenance --help",
    legacyCicdHelp: "bun scripts/cli.ts hwlab nodes control-plane legacy-cicd --help",
    valuesPrinted: false,
  };
}
