import type { DeliveryDeploymentRecord, DeliveryFeatureReceiptRecord, DeliveryFeatureStateRecord } from "../store";
import { ExactGitShaSchema, Sha256DigestSchema, type RuntimeDefinitionResolution } from "./definitions";

export type ReadinessDecision = { admitted: true; deployment: DeliveryDeploymentRecord; acceptanceReceipt: DeliveryFeatureReceiptRecord; smokeReceipt: DeliveryFeatureReceiptRecord } | { admitted: false; reason: string };
export interface ReadinessEvidence { state: DeliveryFeatureStateRecord | null; deployment: DeliveryDeploymentRecord | null; receipts: DeliveryFeatureReceiptRecord[]; }
/** Pure evidence binding: no latest receipt selection and no side effects. */
export function evaluateDeliveryReadiness(definition: RuntimeDefinitionResolution, evidence: ReadinessEvidence): ReadinessDecision {
  if (!definition.available) return { admitted: false, reason: definition.reason };
  if (definition.definition.definition.activationPolicy === "dark-only") return { admitted: false, reason: "dark-only" };
  if (definition.definition.definition.activationPolicy === "product-decision") return { admitted: false, reason: "product-decision-required" };
  const deployment = evidence.deployment;
  if (!deployment || deployment.status !== "ACTIVE") return { admitted: false, reason: "missing-active-deployment" };
  if (!ExactGitShaSchema.safeParse(deployment.deployedSha).success || !ExactGitShaSchema.safeParse(deployment.deployedTree).success || !Sha256DigestSchema.safeParse(deployment.artifactDigest).success) return { admitted: false, reason: "corrupt-deployment-identity" };
  const d = definition.definition;
  if (evidence.state && evidence.state.definitionDigest !== d.digest) return { admitted: false, reason: "stale-definition" };
  const matching = (receipt: DeliveryFeatureReceiptRecord, kind: "ACCEPTANCE" | "SMOKE") => receipt.kind === kind && receipt.result === "PASSED" && receipt.featureId === d.definition.id && receipt.definitionDigest === d.digest && receipt.deploymentId === deployment.deploymentId && receipt.targetId === deployment.targetId && receipt.candidateSha === deployment.deployedSha && receipt.candidateTree === deployment.deployedTree && receipt.deployedSha === deployment.deployedSha && receipt.deployedTree === deployment.deployedTree && receipt.artifactDigest === deployment.artifactDigest;
  const acceptance = evidence.receipts.find((receipt) => matching(receipt, "ACCEPTANCE"));
  if (!acceptance) return { admitted: false, reason: "missing-exact-acceptance-receipt" };
  if (acceptance.candidateSha !== deployment.deployedSha || acceptance.candidateTree !== deployment.deployedTree) return { admitted: false, reason: "acceptance-candidate-mismatch" };
  const smoke = evidence.receipts.find((receipt) => matching(receipt, "SMOKE"));
  if (!smoke) return { admitted: false, reason: "missing-exact-smoke-receipt" };
  const required = d.definition.readinessChecks;
  if (!required.every((check) => evidence.receipts.some((receipt) => matching(receipt, receipt.kind) && receipt.checkId === check))) return { admitted: false, reason: "readiness-check-uncovered" };
  return { admitted: true, deployment, acceptanceReceipt: acceptance, smokeReceipt: smoke };
}
