// tools/suggest-tooling/dagger-catalog.ts — SPEC-026a TD-P15-01
// Builds Dagger CI tooling recommendation lines for suggest_tooling.
// Dagger (dagger.io) — portable CI pipelines via Go/Python/TypeScript SDK.
import { access } from 'node:fs/promises';
import { glob } from 'glob';
/**
 * Returns true when dagger.json is present at the project root.
 */
async function hasDaggerJson(projectPath) {
    try {
        await access(`${projectPath}/dagger.json`);
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Returns true when the project has a complex CI setup:
 * multiple GitHub Actions workflow files OR a Makefile with CI targets.
 */
async function hasComplexCi(projectPath) {
    try {
        const workflowFiles = await glob('**/.github/workflows/*.yml', {
            cwd: projectPath,
            absolute: false,
        });
        if (workflowFiles.length >= 2) {
            return true;
        }
        // Check for Makefile with CI-related targets
        const makefiles = await glob('**/Makefile', { cwd: projectPath, absolute: false });
        if (makefiles.length === 0) {
            return false;
        }
        const { readFile } = await import('node:fs/promises');
        for (const makefile of makefiles) {
            const content = await readFile(`${projectPath}/${makefile}`, 'utf-8');
            const ciTargetPattern = /^(ci|build|test|lint|deploy|release)\s*:/m;
            if (ciTargetPattern.test(content)) {
                return true;
            }
        }
        return false;
    }
    catch {
        return false;
    }
}
/**
 * Build Dagger CI tooling recommendation lines.
 *
 * When dagger.json is detected → emits existing Dagger usage tips.
 * When complex CI is detected but no Dagger → recommends Dagger as improvement.
 */
export async function buildDaggerCatalogLines(knowledge) {
    const projectPath = knowledge.projectPath;
    const daggerDetected = await hasDaggerJson(projectPath);
    const complexCi = daggerDetected ? false : await hasComplexCi(projectPath);
    if (!daggerDetected && !complexCi) {
        return [];
    }
    if (daggerDetected) {
        return [
            '## Dagger CI (detected: dagger.json)',
            '',
            '**Dagger** — Portable CI pipelines as code (Go/Python/TypeScript SDK)',
            '- Detected: `dagger.json` found at project root',
            '- Docs: https://docs.dagger.io',
            '',
            '### Dagger Best Practices',
            '',
            '- **Caching**: use `dag.CacheVolume()` for package manager caches (npm, pip, cargo, go)',
            '  - Example: `cache := dag.CacheVolume("node-modules")`',
            '- **SDK consistency**: pick one SDK (Go/Python/TypeScript) across all pipeline modules',
            '- **Local parity**: run `dagger call build` locally to validate before pushing — identical to CI',
            '- **Secrets**: use `dag.SetSecret()` — never pass secrets as plain env vars in pipeline code',
            '- **Multi-platform**: `dagger call build --platform linux/amd64,linux/arm64` for cross-arch builds',
            '',
            '### Dagger Modules (2026)',
            '',
            '- Search community modules: https://daggerverse.dev',
            '- Popular: `golang`, `python`, `node`, `docker`, `helm`, `terraform` modules',
            '- Install module: `dagger install github.com/dagger/dagger/modules/go@latest`',
            '',
        ];
    }
    // complexCi detected — recommend Dagger as improvement
    return [
        '## Dagger CI (recommended)',
        '',
        '> Complex CI setup detected (multiple workflows or Makefile CI targets).',
        '> Dagger makes CI pipelines portable, testable locally, and language-agnostic.',
        '',
        '**Dagger** — Portable pipelines as code (dagger.io)',
        '- Install CLI: `curl -L https://dl.dagger.io/dagger/install.sh | sh`',
        '- Docs: https://docs.dagger.io/quickstart',
        '',
        '### Why Dagger over plain YAML',
        '',
        '| Feature | YAML CI | Dagger |',
        '|---------|---------|--------|',
        '| Local debug | Slow (push + wait) | `dagger call test` in seconds |',
        '| Type safety | None | Full (Go/Python/TypeScript SDK) |',
        '| Reuse | Copy-paste | Versioned modules from daggerverse.dev |',
        '| CI portability | Vendor lock-in | GitHub Actions, GitLab, Jenkins, local |',
        '',
        '### SDK Options',
        '',
        '- **Go SDK**: `dagger init --sdk go` — best for Go projects or multi-language monorepos',
        '- **Python SDK**: `dagger init --sdk python` — familiar for Python/ML teams',
        '- **TypeScript SDK**: `dagger init --sdk typescript` — natural for Node.js/web projects',
        '',
    ];
}
//# sourceMappingURL=dagger-catalog.js.map