// =========================================================================
// KALASUTRA V3 — ASHTAKAVARGA ENGINE (Phase 1: Chandrashtakavarga only)
// Standard classical Bhinnashtakavarga bindu (benefic-point) tables per
// BPHS/Phaladeepika — these are fixed classical constants, not something
// specific to your desktop engine's implementation, so they're safe to
// hard-code the same way every KP/Vedic software does.
//
// Each of the 7 grahas + Lagna contributes 1 "bindu" (point) to specific
// houses counted FROM ITS OWN sign. Sum of all 8 contributors' points
// landing on a given sign = that sign's Ashtakavarga score for Moon
// (0-8). Phase 1 use case: look up the score for the CURRENT TRANSIT
// MOON SIGN against the user's natal Chandrashtakavarga.
// =========================================================================

import { getSign, SIGNS } from './kp-core.js';

// House-counted-from-self contribution lists for Moon's Ashtakavarga (Chandrashtakavarga)
const CHANDRA_ASHTAKAVARGA_CONTRIBUTIONS = {
    Su: [3, 6, 10, 11],
    Mo: [1, 3, 6, 7, 9, 10, 11],
    Ma: [3, 6, 11],
    Me: [1, 3, 4, 5, 7, 8, 10, 11],
    Ju: [1, 4, 7, 8, 9, 10, 11, 12],
    Ve: [3, 4, 5, 7, 9, 10, 11],
    Sa: [3, 5, 6, 11],
    Lagna: [3, 6, 9, 10, 11]
};

/**
 * @param {Object} natalPlanetSigns  { Su:0-11, Mo:0-11, Ma:0-11, Me:0-11, Ju:0-11, Ve:0-11, Sa:0-11 } (0=Aries)
 * @param {number} natalLagnaSign    0-11
 * Returns: { bindus: [12 ints, index 0=Aries], byContributor: {...} }
 */
export function computeChandraAshtakavarga(natalPlanetSigns, natalLagnaSign) {
    const bindus = new Array(12).fill(0);
    const byContributor = {};

    for (const [source, houseList] of Object.entries(CHANDRA_ASHTAKAVARGA_CONTRIBUTIONS)) {
        const fromSign = (source === 'Lagna') ? natalLagnaSign : natalPlanetSigns[source];
        if (fromSign === undefined || fromSign === null) continue;
        const hits = [];
        houseList.forEach(houseNum => {
            const targetSign = (fromSign + houseNum - 1) % 12;
            bindus[targetSign] += 1;
            hits.push(targetSign);
        });
        byContributor[source] = hits;
    }
    return { bindus, byContributor };
}

/**
 * Phase 1 lookup: given a precomputed natal Chandrashtakavarga bindu table
 * and the CURRENT transit Moon longitude, returns the point value (0-8)
 * for the sign the transit Moon currently occupies.
 */
export function getTransitMoonAshtakavargaPoints(chandraAshtakavargaBindus, transitMoonLong) {
    const sign = getSign(transitMoonLong);
    return {
        sign: SIGNS[sign],
        signIdx: sign,
        points: chandraAshtakavargaBindus[sign],
        maxPoints: 8
    };
}
