﻿﻿??????????????
﻿﻿??????????????
<?php
session_start();
header('Content-Type: application/json');

if (!isset($_SESSION['student_id'])) {
    echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
    exit;
}

$student_id = $_SESSION['student_id'];

if (!isset($_POST['attempt_id'], $_POST['question_id'], $_POST['selected_option_id'])) {
    echo json_encode(['status' => 'error', 'message' => 'Missing parameters']);
    exit;
}

$attempt_id = intval($_POST['attempt_id']);
$question_id = intval($_POST['question_id']);
$selected_option_id = intval($_POST['selected_option_id']);

require 'connection.php';

try {
    // ----------------------------------------------------
    // Validate Attempt Belongs to This Student + In-Progress
    // ----------------------------------------------------
    $stmt = $pdo->prepare("
        SELECT sa.*, a.id AS assignment_id
        FROM student_attempts sa
        JOIN assignments a ON sa.assignment_id = a.id
        WHERE sa.id = ? AND sa.student_id = ?
        LIMIT 1
    ");
    $stmt->execute([$attempt_id, $student_id]);
    $attempt = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$attempt) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid attempt']);
        exit;
    }

    if ($attempt['status'] !== 'in_progress') {
        echo json_encode(['status' => 'error', 'message' => 'Attempt already submitted']);
        exit;
    }

    $assignment_id = $attempt['assignment_id'];

    // ----------------------------------------------------
    // Validate Question belongs to this Assignment
    // ----------------------------------------------------
    $stmt = $pdo->prepare("SELECT * FROM questions WHERE id = ? AND assignment_id = ?");
    $stmt->execute([$question_id, $assignment_id]);
    $question = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$question) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid question']);
        exit;
    }

    // ----------------------------------------------------
    // Validate Option belongs to this Question
    // ----------------------------------------------------
    $stmt = $pdo->prepare("SELECT * FROM options WHERE id = ? AND question_id = ?");
    $stmt->execute([$selected_option_id, $question_id]);
    $option = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$option) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid option']);
        exit;
    }

    // ----------------------------------------------------
    // Determine Correctness + Marks
    // ----------------------------------------------------
    $is_correct = $option['is_correct'] ? 1 : 0;
    $obtained_marks = $is_correct ? floatval($question['weight']) : 0;

    // ----------------------------------------------------
    // Insert or Update attempt_answers
    // ----------------------------------------------------
    $stmt = $pdo->prepare("SELECT id FROM attempt_answers WHERE attempt_id = ? AND question_id = ?");
    $stmt->execute([$attempt_id, $question_id]);
    $existing = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($existing) {
        // Update existing
        $stmt = $pdo->prepare("
            UPDATE attempt_answers
            SET selected_option_id = ?, is_correct = ?, obtained_marks = ?
            WHERE id = ?
        ");
        $stmt->execute([$selected_option_id, $is_correct, $obtained_marks, $existing['id']]);
    } else {
        // Insert new
        $stmt = $pdo->prepare("
            INSERT INTO attempt_answers (attempt_id, question_id, selected_option_id, is_correct, obtained_marks)
            VALUES (?, ?, ?, ?, ?)
        ");
        $stmt->execute([$attempt_id, $question_id, $selected_option_id, $is_correct, $obtained_marks]);
    }

    echo json_encode([
        'status' => 'success',
        'message' => 'Answer saved',
        'is_correct' => $is_correct,
        'obtained_marks' => $obtained_marks
    ]);
    exit;

} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
    exit;
}
?>