// engine/ai-integration/gemini/policy-generator.ts — Gemini Policy Engine generator (SPEC-267)
import { writeFileSync, mkdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
export function generatePolicyToml(policy) {
    return policy.content;
}
export function generateSddEnforcementPolicy() {
    const content = [
        '[policy]',
        'name = "sdd-enforcement"',
        'description = "Warn when editing src/ without an approved spec branch"',
        '',
        '[[rules]]',
        "condition = \"tool_name in ['write_file', 'edit_file'] and not env.BRANCH matches 'feat/SPEC-'\"",
        'action = "warn"',
        'message = "No active spec branch detected. Create a spec first: /spec [description]"',
        '',
        '[[rules]]',
        "condition = \"tool_name in ['write_file', 'edit_file'] and not env.BRANCH matches 'fix/SPEC-'\"",
        'action = "warn"',
        'message = "No fix spec branch detected. Create a spec first: /spec fix [description]"',
    ].join('\n');
    return {
        name: 'sdd-enforcement',
        description: 'Warn when editing src/ without an approved spec branch',
        content,
    };
}
export function generateBranchProtectionPolicy() {
    const content = [
        '[policy]',
        'name = "branch-protection"',
        'description = "Warn on direct push to main without a completed spec"',
        '',
        '[[rules]]',
        "condition = \"tool_name == 'git_push' and env.BRANCH == 'main'\"",
        'action = "warn"',
        'message = "Direct push to main detected. Merge via develop after spec is done."',
        '',
        '[[rules]]',
        "condition = \"tool_name == 'git_push' and env.BRANCH == 'develop' and not env.SPEC_DONE == 'true'\"",
        'action = "warn"',
        'message = "Pushing to develop without a done spec. Validate your spec first."',
    ].join('\n');
    return {
        name: 'branch-protection',
        description: 'Warn on direct push to main without a completed spec',
        content,
    };
}
export function writeGeminiPolicies(projectPath) {
    const policiesDir = join(projectPath, '.gemini', 'policies');
    /* v8 ignore start */
    if (!existsSync(policiesDir)) {
        mkdirSync(policiesDir, { recursive: true });
    }
    /* v8 ignore stop */
    const sddPolicy = generateSddEnforcementPolicy();
    writeFileSync(join(policiesDir, 'sdd-enforcement.toml'), generatePolicyToml(sddPolicy) + '\n', 'utf8');
    const branchPolicy = generateBranchProtectionPolicy();
    writeFileSync(join(policiesDir, 'branch-protection.toml'), generatePolicyToml(branchPolicy) + '\n', 'utf8');
}
//# sourceMappingURL=policy-generator.js.map