<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\TeacherController;
use App\Http\Controllers\FeeController;
use App\Http\Controllers\ResultController;
use App\Http\Controllers\RoutineController;
use App\Http\Controllers\LibraryController;
use App\Http\Controllers\NoticeController;
use App\Http\Controllers\ClassesController;
use App\Http\Controllers\SmartAttendanceController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// ==================== PUBLIC ROUTES ====================
Route::get('/', function () {
    return redirect()->route('login');
});

Auth::routes();

Route::get('/logout', function () {
    Auth::logout();
    return redirect('/login');
})->name('get.logout');

// ==================== PROTECTED ROUTES ====================
Route::middleware(['auth'])->group(function () {

    // ড্যাশবোর্ড
    Route::get('/home', [HomeController::class, 'index'])->name('home');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // ==================== ছাত্র/ছাত্রী ম্যানেজমেন্ট ====================
    Route::prefix('students')->name('students.')->group(function () {
        Route::post('/search', [StudentController::class, 'search'])->name('search');
        Route::get('/class/{classId}', [StudentController::class, 'getByClass'])->name('class-list');
        Route::get('/id-card/{id}', [StudentController::class, 'downloadIdCard'])->name('id-card');
        Route::get('/check-roll', [StudentController::class, 'checkRoll'])->name('check-roll');
    });
    Route::resource('students', StudentController::class);

    // ==================== শিক্ষক ম্যানেজমেন্ট ====================
    Route::prefix('teachers')->name('teachers.')->group(function () {
        Route::get('/', [TeacherController::class, 'index'])->name('index');
        Route::get('/create', [TeacherController::class, 'create'])->name('create');
        Route::post('/', [TeacherController::class, 'store'])->name('store');
        Route::get('/{id}', [TeacherController::class, 'show'])->name('show');
        Route::get('/{id}/edit', [TeacherController::class, 'edit'])->name('edit');
        Route::put('/{id}', [TeacherController::class, 'update'])->name('update');
        Route::delete('/{id}', [TeacherController::class, 'destroy'])->name('destroy');
        
        Route::get('/attendance', [TeacherController::class, 'attendance'])->name('attendance');
        Route::post('/attendance', [TeacherController::class, 'storeAttendance'])->name('attendance.store');
        
        Route::get('/salary', [TeacherController::class, 'salary'])->name('salary');
        Route::post('/salary/generate', [TeacherController::class, 'generateSalary'])->name('salary.generate');
        Route::post('/salary/pay/{id}', [TeacherController::class, 'paySalary'])->name('salary.pay');
        Route::get('/salary/slip/{id}', [TeacherController::class, 'downloadSalarySlip'])->name('salary.slip');
        
        Route::get('/leaves', [TeacherController::class, 'leaves'])->name('leaves');
        Route::post('/leaves', [TeacherController::class, 'applyLeave'])->name('leaves.apply');
        Route::post('/leaves/{id}/process', [TeacherController::class, 'processLeave'])->name('leaves.process');
        
        Route::get('/report', [TeacherController::class, 'report'])->name('report');
    });

    // ==================== ফি/বিল ম্যানেজমেন্ট ====================
    Route::prefix('fees')->name('fees.')->group(function () {
        Route::get('/', [FeeController::class, 'index'])->name('index');
        Route::post('/search', [FeeController::class, 'search'])->name('search');
        Route::get('/{id}/pay', [FeeController::class, 'pay'])->name('pay');
        Route::post('/{id}/process-payment', [FeeController::class, 'processPayment'])->name('process-payment');
        Route::get('/download-certificate/{id}', [FeeController::class, 'downloadCertificate'])->name('download-certificate');
        Route::get('/receipt/{id}', [FeeController::class, 'receipt'])->name('receipt');
        Route::match(['get', 'post'], '/bulk-generate', [FeeController::class, 'bulkGenerate'])->name('bulk-generate');
        Route::get('/collection-report', [FeeController::class, 'collectionReport'])->name('collection-report');
        Route::get('/due-report', [FeeController::class, 'dueReport'])->name('due-report');
        Route::get('/report', [FeeController::class, 'report'])->name('report');
    });

    // ==================== ফলাফল ম্যানেজমেন্ট ====================
    Route::prefix('results')->name('results.')->group(function () {
        Route::get('/', [ResultController::class, 'index'])->name('index');
        Route::post('/search', [ResultController::class, 'search'])->name('search');
        Route::get('/class-entry', [ResultController::class, 'classResult'])->name('class-entry-form');
        Route::post('/store-bulk', [ResultController::class, 'storeBulk'])->name('store-bulk');
        Route::get('/download-marksheet/{studentId}/{examType}/{examYear}', [ResultController::class, 'downloadMarksheet'])->name('download-marksheet');
        Route::get('/tabulation', [ResultController::class, 'tabulationSheet'])->name('tabulation-form');
        Route::get('/tabulation-sheet', [ResultController::class, 'tabulationSheet'])->name('tabulation-sheet');
        Route::get('/analytics', [ResultController::class, 'analytics'])->name('analytics-form');
        Route::get('/analytics-report', [ResultController::class, 'analyticsReport'])->name('analytics-report');
        Route::post('/send-sms', [ResultController::class, 'sendSMS'])->name('send-sms');
        Route::get('/class-entry/{classId}/{examType}/{examYear}', [ResultController::class, 'classResult'])->name('class-entry');
    });
    Route::resource('results', ResultController::class);

    // ==================== রুটিন ম্যানেজমেন্ট ====================
    Route::prefix('routines')->name('routines.')->group(function () {
        Route::get('/', [RoutineController::class, 'index'])->name('index');
        Route::post('/get', [RoutineController::class, 'getRoutine'])->name('get');
        Route::post('/store', [RoutineController::class, 'store'])->name('store');
        Route::put('/{id}', [RoutineController::class, 'update'])->name('update');
        Route::delete('/{id}', [RoutineController::class, 'destroy'])->name('destroy');
        Route::post('/bulk-update', [RoutineController::class, 'bulkUpdate'])->name('bulk-update');
        Route::get('/generate-template', [RoutineController::class, 'generateTemplate'])->name('generate-template');
        Route::get('/print/{classId}/{section}', [RoutineController::class, 'printRoutine'])->name('print');
        Route::get('/pdf/{classId}/{section}', [RoutineController::class, 'exportPdf'])->name('pdf');
        Route::get('/teacher/{teacherId}', [RoutineController::class, 'teacherRoutine'])->name('teacher');
        Route::get('/room/{roomNumber}', [RoutineController::class, 'roomRoutine'])->name('room');
    });

    // ==================== লাইব্রেরি ম্যানেজমেন্ট ====================
    Route::prefix('library')->name('library.')->group(function () {
        Route::get('/', [LibraryController::class, 'index'])->name('index');
        Route::get('/books', [LibraryController::class, 'books'])->name('books');
        Route::post('/books/add', [LibraryController::class, 'addBook'])->name('books.add');
        Route::get('/books/download', [LibraryController::class, 'downloadBookList'])->name('books.download');
        Route::get('/books/search', [LibraryController::class, 'searchBooks'])->name('books.search');
        Route::get('/books/barcode/{id}', [LibraryController::class, 'generateBarcode'])->name('books.barcode');
        
        Route::get('/issues', [LibraryController::class, 'issues'])->name('issues');
        Route::post('/issue', [LibraryController::class, 'issueBook'])->name('issue');
        Route::post('/return/{id}', [LibraryController::class, 'returnBook'])->name('return');
        
        Route::get('/categories', [LibraryController::class, 'categories'])->name('categories');
        Route::post('/categories/add', [LibraryController::class, 'addCategory'])->name('categories.add');
        
        Route::get('/student/{studentId}', [LibraryController::class, 'studentHistory'])->name('student.history');
        
        Route::get('/settings', [LibraryController::class, 'settings'])->name('settings');
        Route::post('/settings/update', [LibraryController::class, 'updateSettings'])->name('settings.update');
    });

    // ==================== হাজিরা (Attendance) ম্যানেজমেন্ট ====================
    Route::prefix('attendance')->name('attendance.')->group(function () {
        Route::get('/', [SmartAttendanceController::class, 'index'])->name('index');
        Route::get('/cards', [SmartAttendanceController::class, 'cards'])->name('cards');
        Route::get('/cards/{id}', [SmartAttendanceController::class, 'showCard'])->name('cards.show');
        Route::post('/cards/issue', [SmartAttendanceController::class, 'issueCard'])->name('cards.issue');
        Route::post('/cards/{id}/status', [SmartAttendanceController::class, 'updateCardStatus'])->name('cards.status');
        
        Route::get('/devices', [SmartAttendanceController::class, 'devices'])->name('devices');
        Route::post('/devices/register', [SmartAttendanceController::class, 'registerDevice'])->name('devices.register');
        
        Route::get('/manual', [SmartAttendanceController::class, 'manualEntry'])->name('manual');
        Route::post('/manual', [SmartAttendanceController::class, 'manualEntry'])->name('manual.store');
        
        Route::get('/report', [SmartAttendanceController::class, 'report'])->name('report');
        Route::get('/daily', [SmartAttendanceController::class, 'dailySheet'])->name('daily');
        Route::get('/export', [SmartAttendanceController::class, 'export'])->name('export');
    });

    // ==================== শ্রেণি ম্যানেজমেন্ট ====================
    Route::resource('classes', ClassesController::class);

    // ==================== নোটিশ ম্যানেজমেন্ট ====================
    Route::get('/notices', [NoticeController::class, 'index'])->name('notices.index');

});