/**
 * GenusCatalog — shared display metadata for server-supplied mould genus candidates.
 *
 * Pure data + string helpers. No React, no DOM, no fetch — safe to unit test and to call
 * from any surface (LocalSpeciesPanel, ScanDetails, future report/export surfaces).
 *
 * WHY THIS EXISTS: under the `aws_lambda_only` engine the genus classifier runs
 * server-side and the browser only ever receives `{label, confidence}` candidates —
 * LocalCNNService's SPECIES_COLORS/SPECIES_DESCRIPTIONS arrays never load (the local
 * engine is dormant), so display colour and description must come from somewhere the
 * server path can reach. This catalog is that somewhere: keyed by the exact label strings
 * the vision API returns.
 *
 * NAMING: user-facing copy says "genus"/"genera" throughout (all six classes are
 * genus-level, `_spp`), but internal field names elsewhere (speciesTop, speciesRanked,
 * speciesSummary, speciesIsUncertain, SPECIES_LIST) deliberately keep the old "species"
 * names — they are already written into IndexedDB scan records and renaming them would
 * orphan every existing scan.
 *
 * HONESTY RULES (plan §6, ADR-0007):
 *   - Never display an overall accuracy figure anywhere. The headline number is carried
 *     by the No_Mould class and does not mean what a reader would take it to mean.
 *   - Genus is descriptive, never a danger ranking — colours deliberately avoid red, and
 *     Stachybotrys gets a neutral stone (not black/red) so it is never presented as
 *     "the toxic one".
 *   - Every summary string ends by requiring laboratory confirmation.
 */

var GenusCatalog = (function () {

    /**
     * Keyed by the server's label strings (vision API `genus.candidates[].label`).
     * Colour + one-sentence description per genus; nothing here ranks or alarms.
     * @type {Record<string, {color: string, description: string}>}
     */
    var ENTRIES = {
        Acremonium: {
            color: '#8b5cf6',
            description: 'Compact, powdery colonies in white, grey or pale pink, often on water-damaged plasterboard, insulation and window sealant.',
        },
        Aspergillus: {
            color: '#d97706',
            description: 'Powdery green, yellow or brown colonies; one of the most common indoor moulds, frequent in damp cupboards, fabrics and HVAC dust.',
        },
        Chaetomium: {
            color: '#0d9488',
            description: 'Cottony colonies maturing from white to grey-olive with a musty odour; favours chronically wet paper, plasterboard and timber.',
        },
        Cladosporium: {
            color: '#2563eb',
            description: 'Olive-green to brown-black speckled growth; common on window frames, painted walls, textiles and in bathrooms.',
        },
        Penicillium: {
            color: '#db2777',
            description: 'Blue-green, velvety colonies with a white margin; often on wallpaper, water-damaged furnishings and stored fabrics.',
        },
        Stachybotrys: {
            color: '#57534e',
            description: 'Dark greenish-black, slimy or sooty patches on persistently wet, cellulose-rich surfaces such as plasterboard and cardboard.',
        },
    };

    /** Collaboration credit — rendered verbatim in the panel footer. */
    var CREDIT = 'Mould Genus Feature for Mould Detect, developed in collaboration with Joseph Langford, Principal Scientist, Civil & Domestic Environmental Sciences.';

    /** Experimental framing — rendered verbatim wherever genus candidates display. */
    var EXPERIMENTAL_NOTE = 'Mould Genus is an experimental feature that only suggests which genera may be present to aid decision making. Independent laboratory testing is always recommended by the Mould Detect team.';

    /**
     * Display metadata for a server label.
     * @param {string} label — e.g. 'Aspergillus'.
     * @returns {{color: string, description: string}|null} null for unknown labels —
     *   callers must handle a genus the catalog has never heard of without breaking.
     */
    function lookup(label) {
        if (typeof label !== 'string') return null;
        return ENTRIES.hasOwnProperty(label) ? ENTRIES[label] : null;
    }

    /**
     * One-sentence hedged summary of ranked candidates. "Most consistent with", never
     * "is" — and always closed with the lab-testing requirement.
     * @param {Array<{label: string, confidence: number}>} candidates — already ranked,
     *   confidence as a percentage 0–100. Only the first three are used.
     * @returns {string} '' for empty/missing input.
     */
    function summary(candidates) {
        if (!candidates || !candidates.length) return '';
        var parts = [];
        for (var i = 0; i < candidates.length && i < 3; i++) {
            parts.push(candidates[i].label + ' (' + candidates[i].confidence.toFixed(0) + '%)');
        }
        if (parts.length === 1) {
            return 'Signals are most consistent with ' + parts[0] + '. Laboratory testing is required to confirm.';
        }
        var alternates = parts.length === 2 ? parts[1] : parts[1] + ' and ' + parts[2];
        return 'Signals are most consistent with ' + parts[0] + ', with ' + alternates +
            ' also possible. Laboratory testing is required to confirm which genera are present.';
    }

    return {
        ENTRIES: ENTRIES,
        CREDIT: CREDIT,
        EXPERIMENTAL_NOTE: EXPERIMENTAL_NOTE,
        lookup: lookup,
        summary: summary,
    };
})();

window.GenusCatalog = GenusCatalog;
