// tools/create-spec/post-creation.ts — Git setup + contradiction detection + post-creation suggestions.
// Extracted from create-spec.ts to keep file sizes within limits.
import { specStore } from '../../storage/index.js';
import { detectContradictions } from '../../engine/contradiction-detector.js';
import { detectCiCd } from '../../engine/detectors/cicd-detector.js';
import { tryAutoSetupGit } from '../create-spec-helpers.js';
/** Auto-setup git branch (non-blocking). Returns branch info or undefined. */
export async function setupGitBranch(projectId, specId) {
    const result = await tryAutoSetupGit(projectId, specId);
    /* v8 ignore start -- requires real git repo */
    if (result) {
        return { branch: result.branch, data: result.data };
    }
    /* v8 ignore stop */
    return undefined;
}
/** Brief contradiction check (non-blocking). Returns a warning hint or undefined. */
export async function checkContradictions(projectId, specId, description) {
    try {
        const allSpecs = await specStore.listSpecs(projectId);
        const specInputs = allSpecs.map((s) => ({
            id: s.id,
            title: s.title,
            content: s.title + ' ' + description,
        }));
        const contradictions = detectContradictions(specInputs);
        const newSpecContradictions = contradictions.filter((c) => c.specA === specId || c.specB === specId);
        if (newSpecContradictions.length === 0) {
            return undefined;
        }
        const blockers = newSpecContradictions.filter((c) => c.severity === 'blocker');
        return blockers.length > 0
            ? `[CONFLICT] ${blockers.length} potential blocker contradiction(s) detected. Run detect_contradictions for details.`
            : `[WARNING] ${newSpecContradictions.length} potential contradiction(s) detected. Run detect_contradictions for details.`;
    }
    catch {
        return undefined;
    }
}
/** Dispatches the on_spec_created hook event (fire-and-forget). */
export function fireSpecCreatedHook(projectId, spec) {
    // Dynamic imports to avoid circular dependency issues
    void (async () => {
        const { dispatchHookEvent } = await import('../manage-hooks.js');
        const { buildSpecCreatedPayload } = await import('../../engine/hooks/triggers.js');
        void dispatchHookEvent(projectId, buildSpecCreatedPayload(projectId, spec.id, spec.title, spec.type));
    })();
}
// === Template pattern matching ===
const TEMPLATE_PATTERNS = [
    { pattern: /\b(?:crud|create\s+read\s+update\s+delete|rest\s+api)\b/i, category: 'crud' },
    { pattern: /\b(?:auth|login|signup|authentication)\b/i, category: 'auth' },
    { pattern: /\b(?:payment|checkout|billing|stripe)\b/i, category: 'payments' },
    { pattern: /\b(?:notification|email|push\s+notification)\b/i, category: 'notifications' },
];
/** Generates post-creation suggestions based on project state and spec description. Best-effort: never throws. */
export async function generatePostCreationSuggestions(projectPath, description) {
    const suggestions = [];
    // Item 1: Suggest generate_ci when no CI detected
    try {
        const ciResult = await detectCiCd(projectPath);
        if (ciResult.platform === null) {
            suggestions.push({
                tool: 'generate_planu_ci',
                reason: 'No CI/CD configuration detected. Run generate_planu_ci to set up automated testing and deployment.',
            });
        }
    }
    catch {
        // Best-effort: if detection fails, skip suggestion
    }
    // Item 2: Suggest templates when description matches known patterns
    for (const { pattern, category } of TEMPLATE_PATTERNS) {
        if (pattern.test(description)) {
            suggestions.push({
                tool: 'apply_template',
                reason: `Description matches "${category}" pattern. Consider using an ${category} template.`,
                templateCategory: category,
            });
        }
    }
    return suggestions;
}
//# sourceMappingURL=post-creation.js.map