import { resolveMaintainerDeliveryPolicy } from "./maintainer-delivery-policy-resolver.js";
import { MAINTAINER_DELIVERY_PROFILE_METADATA_KEY, REQUIRES_PHASE_BRANCH_METADATA_KEY } from "./maintainer-delivery-metadata-keys.js";
import { WISHLIST_INTAKE_TASK_TYPE } from "./wishlist/wishlist-intake.js";
export { MAINTAINER_DELIVERY_PROFILE_METADATA_KEY, REQUIRES_PHASE_BRANCH_METADATA_KEY } from "./maintainer-delivery-metadata-keys.js";
export function toCompactMaintainerDeliveryPolicy(resolved, warnings) {
    const out = {
        schemaVersion: 1,
        profileName: resolved.profileName,
        reviewMode: resolved.reviewMode,
        evidenceMode: resolved.evidenceMode,
        prProvider: resolved.prProvider,
        mergeStrategy: resolved.mergeStrategy,
        phaseToMainMode: resolved.phaseToMainMode,
        requiresPhaseBranch: resolved.requiresPhaseBranch,
        phaseIntegrationBranch: resolved.phaseIntegrationBranch,
        taskBranchExample: resolved.taskBranchExample,
        mergeTargetPattern: resolved.mergeTarget.pattern,
        mergeTargetBranch: resolved.mergeTarget.branch,
        maintainerDeliveryEnforcementMode: resolved.maintainerDeliveryEnforcementMode,
        deliveryEvidenceEnforcementMode: resolved.deliveryEvidenceEnforcementMode
    };
    if (warnings && warnings.length > 0) {
        out.warningCodes = warnings.map((w) => w.code);
    }
    return out;
}
function readProfile(meta) {
    if (!meta)
        return null;
    const v = meta[MAINTAINER_DELIVERY_PROFILE_METADATA_KEY];
    return typeof v === "string" && v.trim().length > 0 ? v.trim() : null;
}
function readRequiresPhaseBranch(meta) {
    if (!meta)
        return false;
    return meta[REQUIRES_PHASE_BRANCH_METADATA_KEY] === true;
}
/**
 * Machine-readable hints for GitHub maintainer delivery (phase branch + task branch + playbook).
 * Safe on every read — no git subprocess.
 */
export function buildMaintainerDeliveryHints(input) {
    const inProgressEntities = input.tasks.filter((t) => t.status === "in_progress" && !isWishlistIntakeEntity(t));
    const inProgressTasks = inProgressEntities.map((t) => ({
        id: t.id,
        title: t.title,
        maintainerDeliveryProfile: readProfile(t.metadata),
        requiresPhaseBranch: readRequiresPhaseBranch(t.metadata)
    }));
    const sn = input.suggestedNext
        ? input.tasks.find((t) => t.id === input.suggestedNext.id)
        : undefined;
    const phaseIntegrationBranch = input.canonicalPhaseKey
        ? `release/phase-${input.canonicalPhaseKey}`
        : null;
    const base = {
        schemaVersion: 1,
        playbookPath: ".ai/playbooks/task-to-phase-branch.md",
        playbookCursorRulePath: ".cursor/rules/playbook-task-to-phase-branch.mdc",
        machinePlaybooksPath: ".ai/MACHINE-PLAYBOOKS.md",
        phaseIntegrationBranch,
        reminder: "GitHub owns branch, PR, review, merge. Branch from the phase integration branch before the first implementation commit; run run-transition start no later than that commit. workspace-kit run-transition owns task lifecycle evidence.",
        inProgressTasks,
        suggestedNextMaintainerDeliveryProfile: readProfile(sn?.metadata),
        suggestedNextRequiresPhaseBranch: readRequiresPhaseBranch(sn?.metadata)
    };
    if (input.effectiveConfig === undefined) {
        return base;
    }
    let resolvedPolicySuggestedNext = null;
    if (sn) {
        const { resolvedPolicy, warnings } = resolveMaintainerDeliveryPolicy({
            effectiveConfig: input.effectiveConfig,
            task: sn
        });
        resolvedPolicySuggestedNext = toCompactMaintainerDeliveryPolicy(resolvedPolicy, warnings);
    }
    const resolvedPolicyInProgress = inProgressEntities.map((t) => {
        const { resolvedPolicy, warnings } = resolveMaintainerDeliveryPolicy({
            effectiveConfig: input.effectiveConfig,
            task: t
        });
        return {
            id: t.id,
            title: t.title,
            resolvedPolicy: toCompactMaintainerDeliveryPolicy(resolvedPolicy, warnings)
        };
    });
    return {
        ...base,
        resolvedPolicySuggestedNext,
        ...(resolvedPolicyInProgress.length > 0 ? { resolvedPolicyInProgress } : {})
    };
}
function isWishlistIntakeEntity(t) {
    return t.type === WISHLIST_INTAKE_TASK_TYPE;
}
