export const RELEASE_MANIFEST_FILE = "release-manifest.json";
export const DEFAULT_RELEASE_PLATFORM = "win32-x64";

export function createReleaseManifest({
  packageName = "codex-harness",
  version,
  zipFile,
  sha256File,
  sha256,
  size,
  releaseTag = `v${version}`,
  generatedAt,
  owner = "",
  repo = "",
  sourceCommit = ""
}) {
  if (!version) {
    throw new Error("release manifest version is required.");
  }

  if (!zipFile || !sha256File || !sha256) {
    throw new Error("release manifest assets are incomplete.");
  }

  return {
    package: packageName,
    version,
    releaseTag,
    generatedAt: generatedAt ?? new Date().toISOString(),
    source: {
      owner,
      repo,
      commit: sourceCommit
    },
    platforms: {
      [DEFAULT_RELEASE_PLATFORM]: {
        zip: zipFile,
        sha256File,
        sha256,
        size
      }
    }
  };
}

export function getReleasePlatformEntry(manifest, platform = DEFAULT_RELEASE_PLATFORM) {
  if (!manifest || typeof manifest !== "object") {
    throw new Error("release manifest is missing.");
  }

  if (String(manifest.package || "").trim() !== "codex-harness") {
    throw new Error("release manifest package must be codex-harness.");
  }

  const version = String(manifest.version || "").trim();
  if (!version) {
    throw new Error("release manifest version is missing.");
  }

  const entry = manifest.platforms?.[platform];
  if (!entry) {
    throw new Error(`release manifest does not include platform ${platform}.`);
  }

  if (!entry.zip || !entry.sha256File || !entry.sha256) {
    throw new Error(`release manifest platform ${platform} is incomplete.`);
  }

  return {
    version,
    zip: entry.zip,
    sha256File: entry.sha256File,
    sha256: entry.sha256,
    size: entry.size ?? null
  };
}
