'use server';
import { resolve } from '../../lib/server-container.js';
import { composeUserInput } from './compose-user-input.js';
export async function createFeature(input) {
    const { description, repositoryPath, attachments, sessionId, approvalGates, push, openPr, parentId, fast, pending, forkAndPr, commitSpecs, ciWatchEnabled, enableEvidence, commitEvidence, agentType, model, rebaseBeforeBranch, injectSkills, applicationId, } = input;
    if (!description?.trim()) {
        return { error: 'description is required' };
    }
    if (!repositoryPath?.trim()) {
        return { error: 'repositoryPath is required' };
    }
    const userInput = composeUserInput(description, attachments);
    const gates = {
        allowPrd: approvalGates?.allowPrd ?? false,
        allowPlan: approvalGates?.allowPlan ?? false,
        allowMerge: approvalGates?.allowMerge ?? false,
    };
    try {
        const createFeatureUseCase = resolve('CreateFeatureUseCase');
        // Phase 1 (fast): create DB record with real UUID — returns immediately
        const { feature, shouldSpawn } = await createFeatureUseCase.createRecord({
            userInput,
            repositoryPath,
            approvalGates: gates,
            push: push ?? false,
            openPr: openPr ?? false,
            ...(parentId ? { parentId } : {}),
            description,
            ...(fast ? { fast } : {}),
            ...(pending ? { pending } : {}),
            ...(forkAndPr != null ? { forkAndPr } : {}),
            ...(commitSpecs != null ? { commitSpecs } : {}),
            ...(ciWatchEnabled != null ? { ciWatchEnabled } : {}),
            ...(enableEvidence != null ? { enableEvidence } : {}),
            ...(commitEvidence != null ? { commitEvidence } : {}),
            ...(agentType ? { agentType } : {}),
            ...(model ? { model } : {}),
            ...(rebaseBeforeBranch != null ? { rebaseBeforeBranch } : {}),
            ...(injectSkills != null ? { injectSkills } : {}),
            ...(applicationId ? { applicationId } : {}),
        });
        // Phase 2 (background): metadata generation, worktree, spec, agent spawn
        // Fire-and-forget — the UI gets the real feature ID immediately
        createFeatureUseCase
            .initializeAndSpawn(feature, {
            userInput,
            repositoryPath,
            approvalGates: gates,
            push: push ?? false,
            openPr: openPr ?? false,
            ...(parentId ? { parentId } : {}),
            ...(fast ? { fast } : {}),
            ...(pending ? { pending } : {}),
            ...(forkAndPr != null ? { forkAndPr } : {}),
            ...(commitSpecs != null ? { commitSpecs } : {}),
            ...(ciWatchEnabled != null ? { ciWatchEnabled } : {}),
            ...(enableEvidence != null ? { enableEvidence } : {}),
            ...(commitEvidence != null ? { commitEvidence } : {}),
            ...(agentType ? { agentType } : {}),
            ...(model ? { model } : {}),
            ...(sessionId ? { sessionId } : {}),
            ...(rebaseBeforeBranch != null ? { rebaseBeforeBranch } : {}),
            ...(injectSkills != null ? { injectSkills } : {}),
            ...(applicationId ? { applicationId } : {}),
        }, shouldSpawn)
            .catch((err) => {
            // eslint-disable-next-line no-console
            console.error('[createFeature] initializeAndSpawn failed:', err);
        });
        return { feature };
    }
    catch (error) {
        const message = error instanceof Error ? error.message : 'Failed to create feature';
        return { error: message };
    }
}
