import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
import { join, relative } from 'node:path';
import chalk from 'chalk';
import { getRepoRoot, getGitHubRepoUrl } from '../git.js';
import { getMajorVersion } from '../../utils/index.js';
/**
 * Template for warden.toml configuration file.
 */
function generateWardenToml() {
    return `version = 1
`;
}
/**
 * Template for GitHub Actions workflow file.
 */
function generateWorkflowYaml() {
    const majorVersion = getMajorVersion();
    return `name: Warden

on:
  pull_request:
    types: [opened, synchronize, reopened]

permissions:
  contents: read
  pull-requests: write
  checks: write

jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: getsentry/warden@v${majorVersion}
        with:
          anthropic-api-key: \${{ secrets.WARDEN_ANTHROPIC_API_KEY }}
`;
}
/**
 * Check for existing warden configuration files.
 */
function checkExistingFiles(repoRoot) {
    const wardenTomlPath = join(repoRoot, 'warden.toml');
    const workflowPath = join(repoRoot, '.github', 'workflows', 'warden.yml');
    return {
        hasWardenToml: existsSync(wardenTomlPath),
        hasWorkflow: existsSync(workflowPath),
    };
}
/**
 * Run the init command to scaffold warden configuration.
 */
export async function runInit(options, reporter) {
    const cwd = process.cwd();
    // Find repo root
    let repoRoot;
    try {
        repoRoot = getRepoRoot(cwd);
    }
    catch {
        reporter.error('Not a git repository. Run this command from a git repository.');
        return 1;
    }
    // Check for existing files
    const existing = checkExistingFiles(repoRoot);
    let filesCreated = 0;
    // Create warden.toml
    const wardenTomlPath = join(repoRoot, 'warden.toml');
    if (existing.hasWardenToml && !options.force) {
        reporter.skipped(relative(cwd, wardenTomlPath), 'already exists');
    }
    else {
        const content = generateWardenToml();
        writeFileSync(wardenTomlPath, content, 'utf-8');
        reporter.created(relative(cwd, wardenTomlPath));
        filesCreated++;
    }
    // Create .github/workflows directory if needed
    const workflowDir = join(repoRoot, '.github', 'workflows');
    if (!existsSync(workflowDir)) {
        mkdirSync(workflowDir, { recursive: true });
    }
    // Create workflow file
    const workflowPath = join(workflowDir, 'warden.yml');
    if (existing.hasWorkflow && !options.force) {
        reporter.skipped(relative(cwd, workflowPath), 'already exists');
    }
    else {
        const content = generateWorkflowYaml();
        writeFileSync(workflowPath, content, 'utf-8');
        reporter.created(relative(cwd, workflowPath));
        filesCreated++;
    }
    if (filesCreated === 0) {
        reporter.blank();
        reporter.tip('All configuration files already exist. Use --force to overwrite.');
        return 0;
    }
    // Print next steps
    reporter.blank();
    reporter.bold('Next steps:');
    reporter.text(`  1. Add a skill: ${chalk.cyan('warden add <skill-name>')}`);
    reporter.text(`  2. Set ${chalk.cyan('WARDEN_ANTHROPIC_API_KEY')} in .env.local`);
    reporter.text(`  3. Add ${chalk.cyan('WARDEN_ANTHROPIC_API_KEY')} to repository secrets`);
    // Show GitHub secrets URL if available
    const githubUrl = getGitHubRepoUrl(repoRoot);
    if (githubUrl) {
        reporter.text(`     ${chalk.dim(githubUrl + '/settings/secrets/actions')}`);
    }
    reporter.text('  4. Commit and open a PR to test');
    return 0;
}
//# sourceMappingURL=init.js.map