'use server';
import { resolve } from '../../lib/server-container.js';
export async function triggerReview(input) {
    const { target, owner, repo, repositoryPath, featureId, autoPost } = input;
    if (!target) {
        return { error: 'target is required (PR number or URL)' };
    }
    try {
        const runUseCase = resolve('RunCodeReviewUseCase');
        const result = await runUseCase.execute({
            target,
            owner,
            repo,
            repositoryPath,
            featureId,
        });
        if (!result.ok) {
            return { error: result.error };
        }
        // Optionally post to GitHub
        if (autoPost) {
            try {
                const postUseCase = resolve('PostCodeReviewUseCase');
                const postedReview = await postUseCase.execute(result.review.id);
                return { review: postedReview };
            }
            catch (postError) {
                // Review was completed but posting failed — return the completed review with error
                return {
                    review: result.review,
                    error: `Review completed but posting failed: ${postError instanceof Error ? postError.message : String(postError)}`,
                };
            }
        }
        return { review: result.review };
    }
    catch (error) {
        return { error: error instanceof Error ? error.message : String(error) };
    }
}
