<?php

namespace App\Controllers;

class CourseController {
    private $pdo;
    private $config;
    
    public function __construct($pdo, $config) {
        $this->pdo = $pdo;
        $this->config = $config;
    }
    
    public function index() {
        $this->courses();
    }
    
    public function courses() {
        $stmt = $this->pdo->query("SELECT c.*, cc.name as category_name, cl.name as level_name FROM courses c LEFT JOIN course_categories cc ON c.category_id = cc.id LEFT JOIN course_levels cl ON c.level_id = cl.id WHERE c.is_published = 1");
        $courses = $stmt->fetchAll();
        
        $stmt = $this->pdo->query("SELECT * FROM course_categories WHERE is_active = 1");
        $categories = $stmt->fetchAll();
        
        $theme = get_setting('default_theme', 'orbit-dark');
        include __DIR__ . '/../Views/layouts/public.php';
        include __DIR__ . '/../Views/courses/index.php';
        include __DIR__ . '/../Views/layouts/footer.php';
    }
    
    public function course($slug) {
        $stmt = $this->pdo->prepare("SELECT * FROM courses WHERE slug = ? AND is_published = 1");
        $stmt->execute([$slug]);
        $course = $stmt->fetch();
        
        if (!$course) {
            http_response_code(404);
            require_once __DIR__ . '/../Views/errors/404.php';
            exit;
        }
        
        $stmt = $this->pdo->prepare("SELECT * FROM modules WHERE course_id = ? AND is_published = 1 ORDER BY sort_order");
        $stmt->execute([$course['id']]);
        $modules = $stmt->fetchAll();
        
        $theme = get_setting('default_theme', 'orbit-dark');
        include __DIR__ . '/../Views/layouts/public.php';
        include __DIR__ . '/../Views/courses/show.php';
        include __DIR__ . '/../Views/layouts/footer.php';
    }
}
