/**
 * Review Feature Use Case
 *
 * Reads spec.yaml open questions for a feature waiting for approval,
 * returning structured data for the TUI review wizard.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { injectable, inject } from 'tsyringe';
import yaml from 'js-yaml';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { AgentRunStatus } from '../../../domain/generated/output.js';
let ReviewFeatureUseCase = class ReviewFeatureUseCase {
    featureRepository;
    agentRunRepository;
    constructor(featureRepository, agentRunRepository) {
        this.featureRepository = featureRepository;
        this.agentRunRepository = agentRunRepository;
    }
    async execute(featureId, repoPath) {
        // Find feature (try exact match, then prefix)
        const feature = (await this.featureRepository.findById(featureId)) ??
            (await this.featureRepository.findByIdPrefix(featureId));
        if (!feature)
            return { success: false, reason: 'Feature not found' };
        // Find agent run
        if (!feature.agentRunId)
            return { success: false, reason: 'Feature has no agent run' };
        const run = await this.agentRunRepository.findById(feature.agentRunId);
        if (!run || run.status !== AgentRunStatus.waitingApproval) {
            return {
                success: false,
                reason: `Feature is not waiting for approval (status: ${run?.status ?? 'unknown'})`,
            };
        }
        // Read spec.yaml using feature's stored specPath
        if (!feature.specPath) {
            return { success: false, reason: 'Feature has no spec path' };
        }
        const specPath = join(feature.specPath, 'spec.yaml');
        let specContent;
        try {
            specContent = readFileSync(specPath, 'utf-8');
        }
        catch {
            return { success: false, reason: 'Could not read spec.yaml' };
        }
        // Parse openQuestions
        const spec = yaml.load(specContent);
        const openQuestions = spec?.openQuestions;
        if (!Array.isArray(openQuestions) || openQuestions.length === 0) {
            return { success: false, reason: 'No open questions found in spec.yaml' };
        }
        const questions = openQuestions.map((q) => ({
            question: String(q.question ?? ''),
            options: Array.isArray(q.options) ? q.options : [],
            selectedOption: String(q.answer ?? ''),
            selectionRationale: q.selectionRationale ? String(q.selectionRationale) : undefined,
            answer: q.answer ? String(q.answer) : undefined,
        }));
        return {
            success: true,
            questions,
            featureName: feature.name,
            phase: 'requirements',
            runId: run.id,
            repoPath: run.repositoryPath ?? repoPath,
        };
    }
};
ReviewFeatureUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IAgentRunRepository')),
    __metadata("design:paramtypes", [Object, Object])
], ReviewFeatureUseCase);
export { ReviewFeatureUseCase };
