/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { phasePlaybooks } from '../phases/registry.js';
const PLAYBOOK_CATALOG = [
    {
        filename: 'sprint-plan.md',
        triggers: ['sprint', 'planning', 'velocity', 'capacity'],
        riskAreas: ['delivery'],
        roles: ['pm'],
    },
    {
        filename: 'status-report.md',
        triggers: ['status', 'report', 'progress', 'metrics'],
        riskAreas: ['delivery'],
        roles: ['pm'],
    },
    {
        filename: 'risk-register.md',
        triggers: ['risk', 'mitigation', 'contingency'],
        riskAreas: ['delivery', 'security'],
        roles: ['pm'],
    },
    {
        filename: 'delivery-metrics.md',
        triggers: ['metric', 'kpi', 'throughput', 'cycle'],
        riskAreas: ['delivery'],
        roles: ['pm'],
    },
    {
        filename: 'refine-stories.md',
        triggers: ['story', 'refine', 'acceptance', 'criteria', 'requirement'],
        riskAreas: [],
        roles: ['ba'],
    },
    { filename: 'groom-backlog.md', triggers: ['backlog', 'groom', 'priorit', 'icebox'], riskAreas: [], roles: ['ba'] },
    {
        filename: 'qa-plan.md',
        triggers: ['qa', 'test plan', 'coverage', 'strategy'],
        riskAreas: ['quality'],
        roles: ['ba', 'qa'],
    },
    {
        filename: 'adr.md',
        triggers: ['architecture', 'decision', 'adr', 'trade-off', 'pattern'],
        riskAreas: ['maintainability'],
        roles: ['ta'],
    },
    {
        filename: 'architecture-review.md',
        triggers: ['architecture', 'review', 'design', 'diagram'],
        riskAreas: ['maintainability'],
        roles: ['ta'],
    },
    {
        filename: 'data-model-design.md',
        triggers: ['data model', 'schema', 'entity', 'sobject', 'custom object', 'field'],
        riskAreas: ['maintainability'],
        roles: ['ta'],
    },
    {
        filename: 'design-review.md',
        triggers: ['design', 'ux', 'ui', 'wireframe', 'mockup'],
        riskAreas: ['usability'],
        roles: ['ux'],
    },
    {
        filename: 'wireframe.md',
        triggers: ['wireframe', 'prototype', 'layout', 'flow'],
        riskAreas: ['usability'],
        roles: ['ux'],
    },
    {
        filename: 'a11y-audit.md',
        triggers: ['accessibility', 'a11y', 'wcag', 'contrast', 'screen reader'],
        riskAreas: ['usability', 'compliance'],
        roles: ['ux'],
    },
    {
        filename: 'create-apex-class.md',
        triggers: ['apex', 'class', 'service', 'handler', 'controller', 'batch', 'queueable'],
        riskAreas: [],
        roles: ['developer'],
    },
    {
        filename: 'create-trigger.md',
        triggers: ['trigger', 'triggerhandler', 'before', 'after', 'dml'],
        riskAreas: [],
        roles: ['developer'],
    },
    {
        filename: 'create-lwc.md',
        triggers: ['lwc', 'lightning', 'component', 'web component', 'ui', 'frontend'],
        riskAreas: [],
        roles: ['developer'],
    },
    {
        filename: 'declare-story-points.md',
        triggers: ['point', 'sizing', 'estimate', 'story point'],
        riskAreas: [],
        roles: ['developer'],
    },
    {
        filename: 'security-review.md',
        triggers: ['security', 'vulnerability', 'owasp', 'injection', 'auth'],
        riskAreas: ['security'],
        roles: ['security'],
    },
    {
        filename: 'threat-model.md',
        triggers: ['threat', 'attack', 'vector', 'stride', 'trust boundary'],
        riskAreas: ['security'],
        roles: ['security'],
    },
    {
        filename: 'qa-checklist.md',
        triggers: ['qa', 'checklist', 'verify', 'validate', 'test'],
        riskAreas: ['quality'],
        roles: ['qa'],
    },
    {
        filename: 'observable-assertions.md',
        triggers: ['assert', 'observable', 'evidence', 'proof'],
        riskAreas: ['quality'],
        roles: ['qa'],
    },
    {
        filename: 'regression.md',
        triggers: ['regression', 'existing', 'backward', 'compat'],
        riskAreas: ['quality'],
        roles: ['qa'],
    },
    {
        filename: 'smoke-test.md',
        triggers: ['smoke', 'sanity', 'deploy', 'health'],
        riskAreas: ['quality'],
        roles: ['qa'],
    },
    {
        filename: 'run-playwright.md',
        triggers: ['playwright', 'e2e', 'browser', 'end-to-end', 'automation'],
        riskAreas: ['quality'],
        roles: ['qa'],
    },
    {
        filename: 'generate-test-report.md',
        triggers: ['report', 'coverage', 'junit', 'result'],
        riskAreas: ['quality'],
        roles: ['qa'],
    },
    {
        filename: 'release.md',
        triggers: ['release', 'deploy', 'publish', 'ship'],
        riskAreas: ['release'],
        roles: ['devops'],
    },
    {
        filename: 'candidate-release.md',
        triggers: ['candidate', 'rc', 'pre-release', 'staging'],
        riskAreas: ['release'],
        roles: ['devops'],
    },
    {
        filename: 'validate.md',
        triggers: ['validate', 'deploy validate', 'check deploy', 'org'],
        riskAreas: ['release'],
        roles: ['devops'],
    },
    {
        filename: 'doc-review.md',
        triggers: ['doc', 'documentation', 'readme', 'changelog'],
        riskAreas: [],
        roles: ['devops'],
    },
    {
        filename: 'update-jira-ticket.md',
        triggers: ['jira', 'ticket', 'issue', 'status update'],
        riskAreas: [],
        roles: ['ba', 'developer', 'qa', 'devops'],
    },
];
const catalogByFilename = new Map(PLAYBOOK_CATALOG.map((p) => [p.filename, p]));
export function scorePlaybooksForPhase(phase, signals) {
    const filenames = phasePlaybooks(phase);
    const text = normalizeText(signals);
    const activeRoles = collectRoles(signals);
    const scored = filenames.map((filename) => {
        const catalog = catalogByFilename.get(filename);
        if (!catalog)
            return { filename, score: 0, matchedTriggers: [] };
        let score = 0;
        const matchedTriggers = [];
        for (const trigger of catalog.triggers) {
            if (text.includes(trigger)) {
                score += 2;
                matchedTriggers.push(trigger);
            }
        }
        for (const role of catalog.roles) {
            if (activeRoles.has(role)) {
                score += 3;
                matchedTriggers.push(`role:${role}`);
                break;
            }
        }
        for (const risk of catalog.riskAreas) {
            if (text.includes(risk)) {
                score += 1;
                matchedTriggers.push(`risk:${risk}`);
            }
        }
        return { filename, score, matchedTriggers };
    });
    scored.sort((a, b) => b.score - a.score);
    const selected = scored.filter((s) => s.score > 0);
    const skipped = scored.filter((s) => s.score === 0);
    return { phase, selected, skipped };
}
export function scoreAllPhasePlaybooks(phases, signals) {
    return phases.map((phase) => scorePlaybooksForPhase(phase, signals));
}
function normalizeText(signals) {
    const parts = [signals.summary];
    if (signals.owner)
        parts.push(signals.owner);
    if (signals.roles) {
        parts.push(...signals.roles.required, ...signals.roles.optional);
    }
    return parts.join(' ').toLowerCase();
}
function collectRoles(signals) {
    const roles = new Set();
    if (signals.owner) {
        const profileId = signals.owner.replace('setup-agents:', '');
        roles.add(profileId);
    }
    if (signals.roles) {
        for (const r of [...signals.roles.required, ...signals.roles.optional]) {
            roles.add(r);
        }
    }
    return roles;
}
//# sourceMappingURL=skill-scoring.js.map