/**
 * specialist-cost-estimator.ts
 *
 * Heuristic per-sortie cost estimates for a specialist.  Derives
 * { low, typical, high, unit: 'usd' } from the specialist's domains and
 * authority level.  All values carry a conservative +20 % buffer and are
 * clearly labelled as estimates, not guarantees.
 *
 * Token-cost assumptions (blended input + output at current model pricing):
 *   lightweight  ~8 k tokens  → ~$0.04 typical
 *   standard    ~18 k tokens  → ~$0.07 typical
 *   heavy       ~30 k tokens  → ~$0.10 typical
 *   architect   ~50 k tokens  → ~$0.13 typical
 */
const DOMAIN_TIER = [
    // architect (checked first — highest priority)
    ['architect', 'architect'],
    ['system design', 'architect'],
    ['ai ', 'architect'],
    ['machine learning', 'architect'],
    // heavy
    ['backend', 'heavy'],
    ['api', 'heavy'],
    ['security', 'heavy'],
    ['devops', 'heavy'],
    ['ci/cd', 'heavy'],
    ['infrastructure', 'heavy'],
    ['incident', 'heavy'],
    ['cloud', 'heavy'],
    // standard
    ['frontend', 'standard'],
    ['ui ', 'standard'],
    ['ux ', 'standard'],
    ['testing', 'standard'],
    ['qa', 'standard'],
    ['performance', 'standard'],
    // lightweight
    ['docs', 'lightweight'],
    ['documentation', 'lightweight'],
    ['writing', 'lightweight'],
    ['copywriting', 'lightweight'],
];
const TIER_ORDER = {
    lightweight: 0, standard: 1, heavy: 2, architect: 3,
};
const TIER_COSTS = {
    lightweight: { low: 0.02, typical: 0.04, high: 0.07 },
    standard: { low: 0.04, typical: 0.07, high: 0.12 },
    heavy: { low: 0.06, typical: 0.10, high: 0.16 },
    architect: { low: 0.09, typical: 0.13, high: 0.22 },
};
// Advisory specialists write shorter prompts; mutate specialists iterate more.
const AUTHORITY_MULT = {
    advisory: 0.8,
    review: 1.0,
    mutate: 1.3,
};
function resolveTier(specialist) {
    const domains = (specialist.domains ?? []).join(' ').toLowerCase();
    let best = 'standard';
    for (const [keyword, tier] of DOMAIN_TIER) {
        if (domains.includes(keyword) && TIER_ORDER[tier] > TIER_ORDER[best]) {
            best = tier;
        }
    }
    return best;
}
/** Returns a conservative per-sortie cost estimate for the given specialist. */
export function estimateSpecialistSortieCost(specialist) {
    const tier = resolveTier(specialist);
    const base = TIER_COSTS[tier];
    const mult = AUTHORITY_MULT[specialist.authority ?? 'review'] ?? 1.0;
    const buffer = 1.2; // +20 % conservative buffer
    const round3 = (n) => Math.round(n * 1000) / 1000;
    return {
        low: round3(base.low * mult * buffer),
        typical: round3(base.typical * mult * buffer),
        high: round3(base.high * mult * buffer),
        unit: 'usd',
        basis: `${tier} domain complexity · ${specialist.authority ?? 'review'} authority · +20% buffer`,
    };
}
