<?php
// calculate_lei.php
// RS Learning Lab – Learning Effectiveness Index (LEI)
// Phase-1 Basic | No Marks | No Rank

header('Content-Type: application/json');

// ------------------------------------
// INPUT (POST)
// ------------------------------------
$student_name = $_POST['name'] ?? null;
$roll_no      = $_POST['roll'] ?? null;

// Normalized inputs (0–100)
$practice_effort    = isset($_POST['practice_effort']) ? floatval($_POST['practice_effort']) : null;
$reinforcement_gain = isset($_POST['reinforcement_gain']) ? floatval($_POST['reinforcement_gain']) : null;
$final_consistency  = isset($_POST['final_consistency']) ? floatval($_POST['final_consistency']) : null;

// ------------------------------------
// VALIDATION
// ------------------------------------
if (
    $practice_effort === null ||
    $reinforcement_gain === null ||
    $final_consistency === null
) {
    http_response_code(400);
    echo json_encode([
        "status" => "error",
        "message" => "Missing LEI input values"
    ]);
    exit;
}

// Safety clamp (0–100)
$practice_effort    = max(0, min(100, $practice_effort));
$reinforcement_gain = max(0, min(100, $reinforcement_gain));
$final_consistency  = max(0, min(100, $final_consistency));

// ------------------------------------
// LEI FORMULA (RS LEARNING LAB LOGIC)
// ------------------------------------
// Practice > Reinforcement > Consistency

$weight_practice      = 0.40;
$weight_reinforcement = 0.35;
$weight_consistency   = 0.25;

$lei_internal_score =
    ($practice_effort * $weight_practice) +
    ($reinforcement_gain * $weight_reinforcement) +
    ($final_consistency * $weight_consistency);

$lei_internal_score = round($lei_internal_score, 1);

// ------------------------------------
// MAP TO LEI SIGNAL (NO NUMBERS SHOWN)
// ------------------------------------
if ($lei_internal_score >= 80) {
    $lei_signal = "Strong";
    $lei_description = "Consistent effort with strong learning improvement patterns";
} elseif ($lei_internal_score >= 60) {
    $lei_signal = "Moderate";
    $lei_description = "Learning is progressing with scope for reinforcement";
} else {
    $lei_signal = "Emerging";
    $lei_description = "Early-stage learning signals; needs sustained practice";
}

// ------------------------------------
// FINAL RESPONSE
// ------------------------------------
$response = [
    "status" => "success",
    "student_name" => $student_name,
    "roll_no" => $roll_no,

    // INTERNAL (can be hidden from UI)
    "lei_score_internal" => $lei_internal_score,

    // WHAT RS LEARNING LAB SHOWS
    "lei_signal" => $lei_signal,
    "lei_description" => $lei_description,

    // Transparency for research / audit
    "inputs" => [
        "practice_effort" => $practice_effort,
        "reinforcement_gain" => $reinforcement_gain,
        "final_consistency" => $final_consistency
    ]
];

echo json_encode($response);
exit;
