<?php
session_start();
require_once('../config/db.php');

// 1. تفعيل نظام الترجمة برمجياً (لأنه ملف خلفي JSON)
$current_lang = $_SESSION['lang'] ?? 'ar';
$lang_path = dirname(__DIR__) . "/languages/" . $current_lang . ".php";
$translations = file_exists($lang_path) ? include($lang_path) : [];

function __($key) {
    global $translations;
    return $translations[$key] ?? $key;
}

// إرسال ترويسة JSON
header('Content-Type: application/json');

// التحقق من صلاحية الوصول
if (!isset($_SESSION['user'])) {
    echo json_encode(['success' => false, 'message' => __('auth_error')]);
    exit;
}

if (isset($_GET['course_id'])) {
    $course_id = (int)$_GET['course_id'];

    try {
        // تحديث الحالة من 'نشط' إلى 'مكتمل'
        $stmt = $conn->prepare("UPDATE enrollments SET status = 'مكتمل' WHERE course_id = ? AND status = 'نشط'");
        $stmt->execute([$course_id]);

        echo json_encode([
            'success' => true, 
            'message' => __('success_graduate_msg')
        ]);
    } catch (PDOException $e) {
        echo json_encode([
            'success' => false, 
            'message' => __('error_db_general') . $e->getMessage()
        ]);
    }
} else {
    echo json_encode(['success' => false, 'message' => __('error_missing_id')]);
}