// tools/legal-compliance-report.ts — SPEC-131: Legal compliance report tool
// Read-only tool that generates a legal compliance report for a project.
import { knowledgeStore, specStore } from '../storage/index.js';
import { buildLegalProfile, getQuestionsForDomains, shouldInjectLegal, } from '../engine/legal-compliance/index.js';
import { t } from '../i18n/index.js';
export async function handleLegalComplianceReport(args) {
    const { projectId } = args;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [{ type: 'text', text: t('project.notFound') }],
            isError: true,
        };
    }
    // Build or refresh legal profile
    const profile = knowledge.legalProfile ?? buildLegalProfile(knowledge);
    const questions = getQuestionsForDomains(profile.domains);
    // Check per-spec compliance
    const specs = await specStore.listSpecs(projectId);
    const specCompliance = specs.map((spec) => {
        // Check if spec has legal-related criteria or tags
        const hasLegalTags = spec.tags.some((tag) => ['legal', 'compliance', 'gdpr', 'hipaa', 'pci', 'privacy'].includes(tag.toLowerCase()));
        const legalCriteriaCount = hasLegalTags ? 1 : 0;
        return {
            specId: spec.id,
            title: spec.title,
            legalCriteriaCount,
            status: (legalCriteriaCount > 0
                ? 'compliant'
                : shouldInjectLegal(profile)
                    ? 'missing'
                    : 'compliant'),
        };
    });
    const compliantCount = specCompliance.filter((s) => s.status === 'compliant').length;
    const totalCount = specCompliance.length;
    const complianceScore = totalCount > 0 ? Math.round((compliantCount / totalCount) * 100) : 100;
    const report = {
        projectId,
        profile: {
            domains: profile.domains,
            countries: profile.countries,
            audience: profile.audience,
            dataTypes: profile.dataTypes,
            detectedAt: profile.detectedAt,
        },
        applicableQuestions: questions.map((q) => ({
            id: q.id,
            domain: q.domain,
            question: q.question,
            rationale: q.rationale,
        })),
        specCompliance,
        complianceScore,
        totalQuestions: questions.length,
        generatedAt: new Date().toISOString(),
    };
    return {
        content: [
            { type: 'text', text: `Legal compliance report: ${complianceScore}% compliant` },
            { type: 'text', text: JSON.stringify(report, null, 2) },
        ],
    };
}
//# sourceMappingURL=legal-compliance-report.js.map