<?php

use App\Http\Controllers\{ProfileController,UserController,EmailInquiryController,PhoneInquiryController};
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ManufacturerController;    
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\CourseController;
use App\Http\Controllers\CourseEnrollmentController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\AssignmentController;
use App\Http\Controllers\AssignmentSubmissionController; //ChatController

use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\EnquiryController;
use App\Http\Controllers\AuditLogController;
use Spatie\Permission\Models\Role;


/*
|--------------------------------------------------------------------------
| 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!
|
*/

Route::get('/', function () {
    // Agar user login hai, dashboard pe bhejo
    if (Auth::check()) {
        return redirect()->route('dashboard'); // dashboard route ka name
    }
    
    // Agar login nahi hai, login page dikhao
    return view('auth.login');
})->name('index');



Route::middleware('auth')->group(function () {
   Route::get('/profile', [UserController::class, 'profile'])->name('profile');

   
    Route::get('/dashboard', [UserController::class, 'dashboard'])->name('dashboard');
    // 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');
    
});


$roles = \Spatie\Permission\Models\Role::pluck('name')->toArray(); 


$roleMiddleware = implode('|', $roles);
Route::middleware(['auth', 'role:' . $roleMiddleware])->group(function () {
       // Change Password
    Route::get('/change-password', [UserController::class, 'change_password'])->name('change_password');
    Route::post('/change-password/update', [UserController::class, 'change_password_update'])->name('change_password_update');
    // Route::get('/dashboard', [UserController::class, 'dashboard'])->name('index');
    Route::resource('permissions',  App\Http\Controllers\PermissionController::class);
    Route::get('permissions/{permissionId}/delete', [App\Http\Controllers\PermissionController::class, 'destroy']);

    Route::resource('roles', App\Http\Controllers\RoleController::class);
    Route::get('roles/{roleId}/delete', [App\Http\Controllers\RoleController::class, 'destroy']);
    Route::get('roles/{roleId}/give-permissions', [App\Http\Controllers\RoleController::class, 'addPermissionToRole']);
    Route::put('roles/{roleId}/give-permissions', [App\Http\Controllers\RoleController::class, 'givePermissionToRole']);

    Route::resource('users', App\Http\Controllers\UserController::class);
    Route::get('users/{userId}/delete', [App\Http\Controllers\UserController::class, 'destroy']);


     // Categories
    Route::resource('categories', CategoryController::class);

    // Courses
    Route::resource('courses', CourseController::class);

    // Approval routes (Master Admin)
    Route::patch('courses/{course}/approve', [CourseController::class, 'approve'])->name('courses.approve');
    Route::patch('courses/{course}/reject',  [CourseController::class, 'reject'])->name('courses.reject');

    // // Course Enrollments (student enroll/unenroll)
    // Route::post('courses/{course}/enroll', [CourseEnrollmentController::class, 'store']);
    // Route::delete('courses/{course}/unenroll', [CourseEnrollmentController::class, 'destroy']);
    // Route::get('courses/{course}/students', [CourseEnrollmentController::class, 'students']); // optional list

    Route::resource('course-enrollments', CourseEnrollmentController::class);

    // Assignments (nested under course)
  Route::resource('assignments', AssignmentController::class);

    // Assignment Submissions (nested under assignment)
   Route::resource('assignment-submissions', AssignmentSubmissionController::class);

    // Chats
    Route::resource('chats', ChatController::class);

    Route::patch('chats/{chat}/flag', [ChatController::class, 'flag'])->name('chats.flag'); // toggle flag
    // Route::get('courses/{course}/chats', [ChatController::class, 'courseChats'])->name('courses.chats'); // course logs
    Route::get('chats/conversation/{user}', [ChatController::class, 'conversation'])->name('chats.conversation');
    Route::get('courses/{course}/chat-students', [ChatController::class, 'students'])->name('courses.chat.students');



     // ✅ NEW: fetch messages ajax
    Route::get('chats/conversation/{user}/messages', [ChatController::class, 'fetchMessages'])->name('chats.conversation.messages');
    // ✅ NEW: send message ajax
    Route::post('chats/send-ajax', [ChatController::class, 'sendAjax'])->name('chats.sendAjax');
     Route::get('teacher/chats/students', [ChatController::class, 'teacherStudentsIndex'])->name('teacher.chats.students');

    // ✅ Conversation (already)
    // Route::get('chats/conversation/{user}', [ChatController::class, 'conversation'])
    //     ->name('chats.conversation');

    // ✅ Send message (if not already)
    // Route::post('chats', [ChatController::class, 'store'])->name('chats.store');
    
    // Analytics (per course)
    Route::get('courses/{course}/analytics', [AnalyticsController::class, 'show']);
    Route::put('courses/{course}/analytics', [AnalyticsController::class, 'update']); // or patch

    Route::Resource('audit-logs', AuditLogController::class);
    // Route::get('/dashboard', [AuditLogController::class, 'index'])->name('dashboard');

    
});

   

require __DIR__.'/auth.php';
