<?php
// teacher/dashboard.php - داشبورد استاد (نسخه نهایی با خطایابی)

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require_once '../config/database.php';
require_once '../includes/db.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once '../includes/settings.php';  // ← این خط اضافه شد

if (!isLoggedIn()) {
    header('Location: ../login.php');
    exit;
}

$user = getCurrentUser();
if ($user['role_level'] != 6) {
    header('Location: ../admin/dashboard.php');
    exit;
}

$pageTitle = 'داشبورد استاد';
$pageSubtitle = 'نمای کلی از وضعیت تدریس شما';

// ===== دریافت آمار =====
$teacherId = $user['id'];

// ===== تعداد دروس =====
$totalCourses = fetchCount("SELECT id FROM courses WHERE teacher_id = ?", [$teacherId]);

// ===== تعداد جلسات =====
$totalSessions = fetchCount(
    "SELECT cs.id FROM course_sessions cs 
     LEFT JOIN courses c ON cs.course_id = c.id 
     WHERE c.teacher_id = ?",
    [$teacherId]
);

// ===== تعداد دانشجویان =====
$totalStudents = fetchOne(
    "SELECT COALESCE(SUM(cs.student_count), 0) as total 
     FROM course_sessions cs 
     LEFT JOIN courses c ON cs.course_id = c.id 
     WHERE c.teacher_id = ?",
    [$teacherId]
);
$totalStudents = $totalStudents['total'] ?? 0;

// ===== تعداد تیکت‌ها =====
$totalTickets = fetchCount("SELECT id FROM tickets WHERE sender_id = ?", [$teacherId]);

// ===== دروس فعال (ترم جاری) =====
$activeTerm = getActiveTerm();
$activeTermId = $activeTerm ? $activeTerm['id'] : 0;

$activeCourses = [];
if ($activeTermId > 0) {
    $activeCourses = fetchAll(
        "SELECT c.*, t.name as term_name, t.code as term_code,
                cs.sessions_count, cs.student_count
         FROM courses c
         LEFT JOIN terms t ON c.term_id = t.id
         LEFT JOIN course_sessions cs ON c.id = cs.course_id AND c.term_id = cs.term_id
         WHERE c.teacher_id = ? AND c.term_id = ?
         ORDER BY c.name ASC",
        [$teacherId, $activeTermId]
    );
}

// ===== تراکنش‌های اخیر =====
$recentTransactions = fetchAll(
    "SELECT t.*, tm.name as term_name 
     FROM transactions t
     LEFT JOIN terms tm ON t.term_id = tm.id
     WHERE t.teacher_id = ?
     ORDER BY t.payment_date DESC
     LIMIT 5",
    [$teacherId]
);

// ===== درخواست‌های گواهی اخیر =====
// بررسی وجود جدول certificate_requests قبل از کوئری
$tableExists = fetchOne("SHOW TABLES LIKE 'certificate_requests'");
$recentCertificates = [];
if ($tableExists) {
    $recentCertificates = fetchAll(
        "SELECT r.*, t.name as term_name, t.code as term_code
         FROM certificate_requests r
         LEFT JOIN terms t ON r.term_id = t.id
         WHERE r.teacher_id = ?
         ORDER BY r.id DESC
         LIMIT 5",
        [$teacherId]
    );
}

// ===== تنظیمات سامانه =====
$settings = getSettings();
$siteName = $settings['site_name'] ?? 'سامانه مدیریت اساتید';
$institutionName = $settings['institution_name'] ?? 'دانشگاه علوم و فناوری';
$logoPath = !empty($settings['logo']) ? '../' . $settings['logo'] : '';
$footerDeveloper = $settings['footer_developer'] ?? '';
$footerInstitution = $settings['footer_institution'] ?? $institutionName;

$content = 'dashboard-content.php';
require_once '../includes/layout.php';
?>