<?php
session_start();
require_once __DIR__ . '/../config/db.php';

$roll = $_GET['roll'] ?? '';
$lang = $_GET['language'] ?? 'python';
$concept = $_GET['concept'] ?? 1;

/* ===== FRIENDLY VALIDATION SCREEN ===== */
if ($roll == '') {
?>
<!DOCTYPE html>
<html>
<head>
<title>Practice Not Started</title>
<style>
body{
margin:0;
font-family:Arial, sans-serif;
background:radial-gradient(circle at top,#020617,#0f172a);
color:#e5e7eb;
display:flex;
align-items:center;
justify-content:center;
height:100vh;
}
.card{
background:#020617;
padding:40px;
border-radius:20px;
box-shadow:0 0 30px rgba(0,0,0,.7);
text-align:center;
}
.btn{
display:inline-block;
margin-top:20px;
padding:12px 22px;
border-radius:10px;
text-decoration:none;
color:#020617;
background:linear-gradient(90deg,#22c55e,#38bdf8);
font-weight:bold;
}
.motivation{
margin-top:15px;
background:#052e2b;
padding:12px;
border-radius:8px;
color:#86efac;
box-shadow:0 0 12px rgba(34,197,94,.15);
}
.correct{
background:#064e3b !important;
border:1px solid #22c55e;
}

.wrong{
background:#3f1d1d !important;
border:1px solid #ef4444;
}

</style>
</head>
<body>
<div class="card">
<h2>Practice Session Not Started</h2>
<p>Please start practice from your dashboard.</p>
<a class="btn" href="/rs_lab/student_dashboard.php">Go to Dashboard</a>
</div>
</body>
</html>
<?php
exit;
}

/* ===== SESSION TRACKING ===== */
if (!isset($_SESSION['used_questions'])) {
    $_SESSION['used_questions'] = [];
}

$placeholders = implode(',', array_fill(0, count($_SESSION['used_questions']), '?'));

if (count($_SESSION['used_questions']) > 0) {
    $sql = "SELECT * FROM practice_questions 
            WHERE language=? AND concept_id=? 
            AND id NOT IN ($placeholders)
            ORDER BY RAND() LIMIT 1";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array_merge([$lang,$concept], $_SESSION['used_questions']));
} else {
    $stmt = $pdo->prepare("SELECT * FROM practice_questions 
        WHERE language=? AND concept_id=? ORDER BY RAND() LIMIT 1");
    $stmt->execute([$lang,$concept]);
}

$q = $stmt->fetch(PDO::FETCH_ASSOC);
/* ===== MOTIVATION LINES ===== */
$motivation_lines = [
    "Good learning takes practice. You're doing great.",
    "Every attempt sharpens your understanding.",
    "No pressure. Explore, try, improve.",
    "Consistency builds confidence.",
    "Mistakes here are stepping stones."
];
$motivation = $motivation_lines[array_rand($motivation_lines)];


if (!$q) {
    $_SESSION['practice_completed'] = true;
?>
<!DOCTYPE html>
<html>
<head>
<title>Practice Completed</title>
<style>
body{background:#020617;color:#4ade80;font-family:Arial;text-align:center;padding-top:100px}
a{color:#38bdf8;font-weight:bold}
</style>
</head>
<body>
<h2>Practice Completed 🎉</h2>
<a href="/rs_lab/reinforcement_track.php">Proceed to Reinforcement</a>
</body>
</html>
<?php
exit;
}

$_SESSION['current_qid'] = $q['id'];
$_SESSION['current_lang'] = $q['language'];
$_SESSION['current_concept'] = $q['concept_id'];
$_SESSION['current_roll'] = $roll;

if(!isset($_SESSION['qno'])) $_SESSION['qno']=1;
?>

<!DOCTYPE html>
<html>
<head>
<title>Practice Track</title>
<style>
body{
background:radial-gradient(circle at top,#020617,#0f172a);
color:#e5e7eb;
font-family:Arial;
padding:40px;
}
.card{
background:#020617;
padding:25px;
border-radius:18px;
box-shadow:0 0 25px rgba(0,0,0,.7);
max-width:700px;
}
.option{
display:block;
margin:10px 0;
padding:10px;
background:#0f172a;
border-radius:10px;
cursor:pointer;
transition:.2s;
}
.option:hover{
box-shadow:0 0 15px rgba(59,130,246,.3);
}
button{
margin-top:15px;
padding:10px 20px;
border-radius:10px;
background:#22c55e;
border:none;
font-weight:bold;
cursor:pointer;
}
.hint{
margin-top:15px;
background:#0e2238;
padding:10px;
border-radius:8px;
}
</style>
</head>
<body>

<div class="card">

<h3>Q<?= $_SESSION['qno'] ?>. <?= htmlspecialchars($q['question']) ?></h3>

<form method="post" action="/rs_lab/tracks/practice_submit.php">

<?php
$lastAns = $_SESSION['last_answer'] ?? null;
$lastCorrect = $_SESSION['last_correct'] ?? null;
$isPrevCorrect = $_SESSION['last_is_correct'] ?? null;

function optClass($opt, $lastAns, $lastCorrect){
    if($lastAns === null) return "";
    if($opt == $lastCorrect) return "correct";
    if($opt == $lastAns && $lastAns != $lastCorrect) return "wrong";
    return "";
}
?>

<label class="option <?= optClass('A',$lastAns,$lastCorrect) ?>">
<input type="radio" name="answer" value="A" required> <?= $q['option_a'] ?>
</label>

<label class="option <?= optClass('B',$lastAns,$lastCorrect) ?>">
<input type="radio" name="answer" value="B"> <?= $q['option_b'] ?>
</label>

<label class="option <?= optClass('C',$lastAns,$lastCorrect) ?>">
<input type="radio" name="answer" value="C"> <?= $q['option_c'] ?>
</label>

<label class="option <?= optClass('D',$lastAns,$lastCorrect) ?>">
<input type="radio" name="answer" value="D"> <?= $q['option_d'] ?>
</label>

<?php if(isset($isPrevCorrect)): ?>
<div style="margin-top:10px;color:<?= $isPrevCorrect?'#22c55e':'#ef4444' ?>">
<?= $isPrevCorrect ? "Good attempt! Correct." : "Not correct. Review and continue." ?>
</div>
<?php endif; ?>

<button type="submit">Submit</button>
</form>

<div class="motivation">
<b>Motivation:</b> <?= $motivation ?>
</div>
<div class="hint">
<b>Hint:</b> <?= $q['hint'] ?>
</div>

</div>

</body>
</html>
