'use server';
import { resolve } from '../../lib/server-container.js';
export async function stopFeature(featureId) {
    if (!featureId.trim()) {
        return { stopped: false, error: 'Feature id is required' };
    }
    try {
        // Find the active agent run for this feature
        const runRepo = resolve('IAgentRunRepository');
        const allRuns = await runRepo.list();
        const activeRun = allRuns.find((r) => r.featureId === featureId &&
            r.status !== 'completed' &&
            r.status !== 'failed' &&
            r.status !== 'interrupted' &&
            r.status !== 'cancelled');
        if (!activeRun) {
            return { stopped: false, error: 'No active agent run found for this feature' };
        }
        const useCase = resolve('StopAgentRunUseCase');
        const result = await useCase.execute(activeRun.id);
        if (!result.stopped) {
            return { stopped: false, error: result.reason };
        }
        return { stopped: true };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to stop agent';
        return { stopped: false, error: message };
    }
}
