<?php
// db_save_learning_profile.php
// RS Learning Lab – Save Learning Profile (Production Ready)

// ------------------------------------
// DB CONFIG (CHANGE AS PER YOUR SETUP)
// ------------------------------------
$db_host = "localhost";
$db_name = "rs_learning_lab";
$db_user = "root";
$db_pass = "";

// ------------------------------------
// CONNECT TO DATABASE (PDO)
// ------------------------------------
try {
    $pdo = new PDO(
        "mysql:host=$db_host;dbname=$db_name;charset=utf8mb4",
        $db_user,
        $db_pass,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([
        "status" => "error",
        "message" => "Database connection failed"
    ]);
    exit;
}

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

$primary_style   = $_POST['primary']   ?? null;
$secondary_style = $_POST['secondary'] ?? null;

$lei_signal = $_POST['lei'] ?? "Baseline";

// Scores JSON (internal, no marks)
$scores_json = $_POST['scores'] ?? null;

// Signals array → JSON
$signals = $_POST['signals'] ?? [];
$signals_json = json_encode($signals);

// ------------------------------------
// BASIC VALIDATION
// ------------------------------------
if (
    !$student_name ||
    !$roll_no ||
    !$primary_style ||
    !$scores_json
) {
    http_response_code(400);
    echo json_encode([
        "status" => "error",
        "message" => "Missing required learning profile data"
    ]);
    exit;
}

// ------------------------------------
// INSERT OR UPDATE LOGIC
// ------------------------------------
// One active learning profile per student
$sql = "
INSERT INTO learning_profiles
(
    student_name,
    roll_no,
    class,
    primary_style,
    secondary_style,
    lei_signal,
    style_scores,
    learning_signals,
    created_at
)
VALUES
(
    :student_name,
    :roll_no,
    :class,
    :primary_style,
    :secondary_style,
    :lei_signal,
    :style_scores,
    :learning_signals,
    NOW()
)
ON DUPLICATE KEY UPDATE
    primary_style     = VALUES(primary_style),
    secondary_style   = VALUES(secondary_style),
    lei_signal        = VALUES(lei_signal),
    style_scores      = VALUES(style_scores),
    learning_signals  = VALUES(learning_signals),
    updated_at        = NOW()
";

$stmt = $pdo->prepare($sql);

// ------------------------------------
// EXECUTE
// ------------------------------------
try {
    $stmt->execute([
        ':student_name'    => $student_name,
        ':roll_no'         => $roll_no,
        ':class'           => $class,
        ':primary_style'   => $primary_style,
        ':secondary_style' => $secondary_style,
        ':lei_signal'      => $lei_signal,
        ':style_scores'    => $scores_json,
        ':learning_signals'=> $signals_json
    ]);

    echo json_encode([
        "status" => "success",
        "message" => "Learning profile saved successfully"
    ]);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([
        "status" => "error",
        "message" => "Failed to save learning profile"
    ]);
}
