import { hashProjectPath } from '../storage/base-store.js';
import { getSpec } from '../storage/spec-store.js';
import { getPrLinks, savePrLink } from '../storage/pr-store.js';
import { generatePrDescription } from '../engine/pr-description-generator.js';
/**
 * Handle generate_pr_description tool call.
 * Reads the spec and generates a markdown PR description.
 */
export async function handleGeneratePrDescription(input) {
    const { projectPath, specId } = input;
    const platform = input.platform ?? 'github';
    const audience = input.audience ?? 'reviewer';
    const includeChecklist = input.includeChecklist ?? true;
    const projectId = hashProjectPath(projectPath);
    const spec = await getSpec(projectId, specId);
    if (spec === null) {
        return {
            content: [{ type: 'text', text: `Spec "${specId}" not found in project at ${projectPath}` }],
            isError: true,
        };
    }
    const result = generatePrDescription(spec, platform, audience, includeChecklist);
    const lines = [
        `PR Description for ${result.specId}`,
        '='.repeat(60),
        ``,
        `Suggested title:`,
        result.titleSuggestion,
        ``,
        `Labels: ${result.labels.join(', ')}`,
        ``,
        `--- BEGIN PR DESCRIPTION (${platform}) ---`,
        ``,
        result.markdown,
        `--- END PR DESCRIPTION ---`,
        ``,
        `Generated at: ${result.generatedAt}`,
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
/**
 * Handle link_pr_to_spec tool call.
 * Saves a PrLink associating a PR URL with a spec.
 */
export async function handleLinkPrToSpec(input) {
    const { projectPath, specId, prUrl, prNumber } = input;
    const platform = input.platform ?? 'github';
    const projectId = hashProjectPath(projectPath);
    const spec = await getSpec(projectId, specId);
    if (spec === null) {
        return {
            content: [{ type: 'text', text: `Spec "${specId}" not found in project at ${projectPath}` }],
            isError: true,
        };
    }
    const link = {
        specId,
        prUrl,
        platform,
        linkedAt: new Date().toISOString(),
    };
    if (prNumber !== undefined) {
        link.prNumber = prNumber;
    }
    await savePrLink(projectPath, link);
    const prLabel = prNumber !== undefined ? `PR #${prNumber}` : `PR`;
    return {
        content: [
            {
                type: 'text',
                text: `${prLabel} linked to spec ${specId}.\nURL: ${prUrl}\nPlatform: ${platform}\nLinked at: ${link.linkedAt}`,
            },
        ],
    };
}
/**
 * Handle list_spec_prs tool call.
 * Lists all PR links for a project, optionally filtered by specId.
 */
export async function handleListSpecPrs(input) {
    const { projectPath, specId } = input;
    const allLinks = await getPrLinks(projectPath);
    const links = specId !== undefined ? allLinks.filter((l) => l.specId === specId) : allLinks;
    if (links.length === 0) {
        const context = specId !== undefined ? `spec ${specId}` : 'this project';
        return {
            content: [{ type: 'text', text: `No PRs linked to ${context} yet.` }],
        };
    }
    const lines = [
        specId !== undefined ? `PRs linked to spec ${specId}:` : `All linked PRs:`,
        '='.repeat(50),
    ];
    for (const link of links) {
        const prLabel = link.prNumber !== undefined ? `PR #${link.prNumber}` : 'PR';
        lines.push(`${link.specId} — ${prLabel} (${link.platform})`);
        lines.push(`  URL: ${link.prUrl}`);
        lines.push(`  Linked: ${link.linkedAt}`);
        lines.push('');
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=pr-description-handler.js.map