'use server';
import { resolve } from '../../lib/server-container.js';
/**
 * Map FeatureArtifact openQuestions into the PrdQuestionnaireData shape
 * expected by the PrdQuestionnaireDrawer component.
 */
function toQuestionnaireData(artifact) {
    return {
        question: 'Goal',
        context: artifact.oneLiner,
        questions: artifact.openQuestions.map((oq, idx) => ({
            id: `q-${idx}`,
            question: oq.question,
            type: 'select',
            options: (oq.options ?? []).map((opt, optIdx) => ({
                id: `q-${idx}-opt-${optIdx}`,
                label: opt.option,
                rationale: opt.description,
                ...(opt.selected ? { recommended: true } : {}),
            })),
        })),
        finalAction: {
            id: 'approve-reqs',
            label: 'Approve Requirements',
            description: 'Finalize and lock the requirements for implementation',
        },
    };
}
/**
 * Map FeatureArtifact openQuestions into a read-only summary for the
 * Product Decisions tab in the tech review drawer.
 */
function toProductDecisionsData(artifact) {
    return {
        question: 'Goal',
        context: artifact.oneLiner,
        questions: artifact.openQuestions
            .filter((oq) => oq.resolved)
            .map((oq) => {
            const selected = oq.options?.find((o) => o.selected);
            return {
                question: oq.question,
                selectedOption: selected?.option ?? oq.answer ?? 'N/A',
                rationale: oq.selectionRationale ?? selected?.description ?? '',
                wasRecommended: false,
            };
        }),
    };
}
export async function getFeatureArtifact(featureId) {
    if (!featureId.trim()) {
        return { error: 'Feature id is required' };
    }
    try {
        const useCase = resolve('GetFeatureArtifactUseCase');
        const artifact = await useCase.execute(featureId);
        const questionnaire = toQuestionnaireData(artifact);
        const productDecisions = toProductDecisionsData(artifact);
        return { questionnaire, productDecisions, artifact };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to load feature artifact';
        return { error: message };
    }
}
