<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\CourseController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Student\StudentController;
use App\Http\Controllers\Teacher\TeacherController;
use App\Http\Controllers\MediaController;
use App\Http\Controllers\EnrollmentController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// Public routes
Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/courses', [HomeController::class, 'courses'])->name('courses.public');
Route::get('/about', [HomeController::class, 'about'])->name('about');
Route::get('/contact', [HomeController::class, 'contact'])->name('contact');

// Course public routes
Route::get('/courses/{course:slug}', [CourseController::class, 'show'])->name('courses.show');
Route::get('/courses/{course:slug}/session/{sessionNumber}', [CourseController::class, 'showSession'])->name('courses.session');

// Enrollment routes
Route::post('/enrollments', [EnrollmentController::class, 'store'])->name('enrollments.store')->middleware('auth');
Route::delete('/courses/{course}/unenroll', [EnrollmentController::class, 'unenroll'])->name('courses.unenroll')->middleware('auth');

require __DIR__.'/auth.php';

// Authenticated routes
Route::middleware('auth')->group(function () {
    // Enrollment routes
    Route::post('/courses/{course}/enroll', [EnrollmentController::class, 'enroll'])->name('courses.enroll');
    Route::delete('/courses/{course}/unenroll', [EnrollmentController::class, 'unenroll'])->name('courses.unenroll');
    Route::get('/my-courses', [EnrollmentController::class, 'myCourses'])->name('my-courses');
    Route::get('/courses/{course}/progress', [EnrollmentController::class, 'show'])->name('courses.progress');

    // Admin routes
    Route::middleware('role:admin')->prefix('admin')->name('admin.')->group(function () {
        Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
    });

    // Teacher routes
    Route::middleware('role:teacher,admin')->prefix('teacher')->name('teacher.')->group(function () {
        Route::get('/dashboard', [TeacherController::class, 'dashboard'])->name('dashboard');

        // Course Management Routes
        Route::get('/courses', [TeacherController::class, 'courses'])->name('courses.index');
        Route::get('/courses/create', [TeacherController::class, 'createCourse'])->name('courses.create');
        Route::post('/courses', [TeacherController::class, 'storeCourse'])->name('courses.store');
        Route::get('/courses/{course}/edit', [TeacherController::class, 'editCourse'])->name('courses.edit');
        Route::put('/courses/{course}', [TeacherController::class, 'updateCourse'])->name('courses.update');
        Route::delete('/courses/{course}', [TeacherController::class, 'destroyCourse'])->name('courses.destroy');

        // Session Management Routes
        Route::get('/courses/{course}/sessions', [TeacherController::class, 'sessions'])->name('courses.sessions');
        Route::get('/courses/{course}/sessions/create', [TeacherController::class, 'createSession'])->name('sessions.create');
        Route::post('/courses/{course}/sessions', [TeacherController::class, 'storeSession'])->name('sessions.store');
        Route::get('/sessions/{session}/edit', [TeacherController::class, 'editSession'])->name('sessions.edit');
        Route::put('/sessions/{session}', [TeacherController::class, 'updateSession'])->name('sessions.update');
        Route::delete('/sessions/{session}', [TeacherController::class, 'destroySession'])->name('sessions.destroy');

        // Material Management Routes
        Route::get('/sessions/{session}/materials', [TeacherController::class, 'materials'])->name('sessions.materials');
        Route::post('/sessions/{session}/materials', [TeacherController::class, 'storeMaterial'])->name('materials.store');
        Route::put('/materials/{material}', [TeacherController::class, 'updateMaterial'])->name('materials.update');
        Route::delete('/materials/{material}', [TeacherController::class, 'destroyMaterial'])->name('materials.destroy');
    });

    // Student routes
    Route::middleware('role:student,teacher,admin')->prefix('student')->name('student.')->group(function () {
        Route::get('/dashboard', [StudentController::class, 'dashboard'])->name('dashboard');
    });

    // Media upload routes (for teachers and admins)
    Route::middleware('role:teacher,admin')->group(function () {
        Route::post('/media/upload', [MediaController::class, 'upload'])->name('media.upload');
        Route::get('/media/files', [MediaController::class, 'getFiles'])->name('media.files');
        Route::delete('/media/{mediaFile}', [MediaController::class, 'destroy'])->name('media.destroy');
    });
});

// Public media access
Route::get('/media/{mediaFile}', [MediaController::class, 'show'])->name('media.show');

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
