<?php

use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\Accounting\BankReconciliationController;
use App\Http\Controllers\Admin\Accounting\ChartOfAccountsController;
use App\Http\Controllers\Admin\Accounting\FixedAssetController;
use App\Http\Controllers\Admin\Accounting\GeneralLedgerController;
use App\Http\Controllers\Admin\Accounting\JournalController;
use App\Http\Controllers\Admin\Accounting\TrialBalanceController;
use App\Http\Controllers\Admin\Communication\DocumentEmailController;
use App\Http\Controllers\Admin\Communication\EmailCenterController;
use App\Http\Controllers\Admin\Communication\EmailSettingsController;
use App\Http\Controllers\Admin\Communication\EmailTemplateController;
use App\Http\Controllers\Admin\Extras\EventController;
use App\Http\Controllers\Admin\Extras\HostelController;
use App\Http\Controllers\Admin\Extras\InventoryController;
use App\Http\Controllers\Admin\Extras\MessageController;
use App\Http\Controllers\Admin\Extras\TransportController;
use App\Http\Controllers\Admin\Finance\BillingController;
use App\Http\Controllers\Admin\Finance\BudgetController;
use App\Http\Controllers\Admin\Finance\BulkInvoiceController;
use App\Http\Controllers\Admin\Finance\ExpenseController;
use App\Http\Controllers\Admin\Finance\FinancialReportController;
use App\Http\Controllers\Admin\Finance\InvoiceController;
use App\Http\Controllers\Admin\Finance\PaymentReconciliationController;
use App\Http\Controllers\Admin\Finance\PayrollController;
use App\Http\Controllers\Admin\Finance\ReceiptController;
use App\Http\Controllers\Admin\School\AttendanceController;
use App\Http\Controllers\Admin\School\ClassController;
use App\Http\Controllers\Admin\School\ClubController;
use App\Http\Controllers\Admin\School\DisciplinaryCaseController;
use App\Http\Controllers\Admin\School\AcademicOversightController;
use App\Http\Controllers\Admin\School\DisciplineController;
use App\Http\Controllers\Admin\School\LeadershipController;
use App\Http\Controllers\Admin\School\ExamController;
use App\Http\Controllers\Admin\School\GradeController;
use App\Http\Controllers\Admin\School\GradingScaleController;
use App\Http\Controllers\Admin\School\LibraryController;
use App\Http\Controllers\Admin\School\ReportCardController;
use App\Http\Controllers\Admin\School\StaffController;
use App\Http\Controllers\Admin\School\StudentController;
use App\Http\Controllers\Admin\School\TimetableController;
use App\Http\Controllers\Admin\System\AcademicYearController;
use App\Http\Controllers\Admin\System\AnalyticsController;
use App\Http\Controllers\Admin\System\AnnouncementController;
use App\Http\Controllers\Admin\System\AuditController;
use App\Http\Controllers\Admin\System\BackupController;
use App\Http\Controllers\Admin\System\DataExportController;
use App\Http\Controllers\Admin\System\DataImportController;
use App\Http\Controllers\Admin\System\ImportExportHistoryController;
use App\Http\Controllers\Admin\System\ReportBuilderController;
use App\Http\Controllers\Admin\System\SettingsController;
use App\Http\Controllers\Admin\System\UserController;
use Illuminate\Support\Facades\Route;

Route::middleware(['auth', 'admin.permission'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');

    Route::prefix('accounting')->name('accounting.')->group(function () {
        Route::resource('coa', ChartOfAccountsController::class)
            ->except(['show'])
            ->parameters(['coa' => 'account']);
        Route::get('journal', [JournalController::class, 'index'])->name('journal.index');
        Route::get('journal/create', [JournalController::class, 'create'])->name('journal.create');
        Route::post('journal', [JournalController::class, 'store'])->name('journal.store');
        Route::get('journal/{journalEntry}', [JournalController::class, 'show'])->name('journal.show');
        Route::post('journal/{journalEntry}/void', [JournalController::class, 'void'])->name('journal.void');
        Route::get('general-ledger', [GeneralLedgerController::class, 'index'])->name('gl.index');
        Route::get('general-ledger/export/{format}', [GeneralLedgerController::class, 'export'])->name('gl.export');
        Route::get('trial-balance', [TrialBalanceController::class, 'index'])->name('trial-balance.index');
        Route::get('trial-balance/export/{format}', [TrialBalanceController::class, 'export'])->name('trial-balance.export');
        Route::get('fixed-assets', [FixedAssetController::class, 'index'])->name('fixed-assets.index');
        Route::get('fixed-assets/create', [FixedAssetController::class, 'create'])->name('fixed-assets.create');
        Route::post('fixed-assets', [FixedAssetController::class, 'store'])->name('fixed-assets.store');
        Route::get('fixed-assets/{fixedAsset}/edit', [FixedAssetController::class, 'edit'])->name('fixed-assets.edit');
        Route::put('fixed-assets/{fixedAsset}', [FixedAssetController::class, 'update'])->name('fixed-assets.update');
        Route::post('fixed-assets/{fixedAsset}/depreciate', [FixedAssetController::class, 'depreciate'])->name('fixed-assets.depreciate');
        Route::get('bank-reconciliation', [BankReconciliationController::class, 'index'])->name('bank-reconciliation.index');
        Route::get('bank-reconciliation/create', [BankReconciliationController::class, 'create'])->name('bank-reconciliation.create');
        Route::post('bank-reconciliation', [BankReconciliationController::class, 'store'])->name('bank-reconciliation.store');
        Route::get('bank-reconciliation/{bankReconciliation}', [BankReconciliationController::class, 'show'])->name('bank-reconciliation.show');
        Route::post('bank-reconciliation/{bankReconciliation}/lines', [BankReconciliationController::class, 'storeLine'])->name('bank-reconciliation.lines.store');
        Route::post('bank-reconciliation/{bankReconciliation}/match', [BankReconciliationController::class, 'match'])->name('bank-reconciliation.match');
        Route::post('bank-reconciliation/{bankReconciliation}/complete', [BankReconciliationController::class, 'complete'])->name('bank-reconciliation.complete');
        Route::post('bank-reconciliation/{bankReconciliation}/import', [BankReconciliationController::class, 'importCsv'])->name('bank-reconciliation.import');
    });

    Route::prefix('finance')->name('finance.')->group(function () {
        Route::get('reports', [FinancialReportController::class, 'index'])->name('reports.index');
        Route::get('reports/income-statement', [FinancialReportController::class, 'incomeStatement'])->name('reports.income-statement');
        Route::get('reports/balance-sheet', [FinancialReportController::class, 'balanceSheet'])->name('reports.balance-sheet');
        Route::get('reports/cashflow', [FinancialReportController::class, 'cashflow'])->name('reports.cashflow');

        Route::get('billing', [BillingController::class, 'index'])->name('billing.index');
        Route::get('billing/create', [BillingController::class, 'create'])->name('billing.create');
        Route::post('billing', [BillingController::class, 'store'])->name('billing.store');
        Route::get('billing/{student}/statement', [BillingController::class, 'statement'])->name('billing.statement');

        Route::get('payment-reconciliation', [PaymentReconciliationController::class, 'index'])->name('payment-reconciliation.index');
        Route::get('payment-reconciliation/create', [PaymentReconciliationController::class, 'create'])->name('payment-reconciliation.create');
        Route::post('payment-reconciliation', [PaymentReconciliationController::class, 'store'])->name('payment-reconciliation.store');
        Route::post('payment-reconciliation/{payment}/confirm', [PaymentReconciliationController::class, 'confirm'])->name('payment-reconciliation.confirm');
        Route::post('payment-reconciliation/{payment}/reject', [PaymentReconciliationController::class, 'reject'])->name('payment-reconciliation.reject');

        Route::get('invoices', [InvoiceController::class, 'index'])->name('invoices.index');
        Route::get('invoices/create', [InvoiceController::class, 'create'])->name('invoices.create');
        Route::post('invoices', [InvoiceController::class, 'store'])->name('invoices.store');
        Route::get('invoices/{invoice}', [InvoiceController::class, 'show'])->name('invoices.show');
        Route::get('invoices/{invoice}/pdf', [InvoiceController::class, 'pdf'])->name('invoices.pdf');
        Route::get('quotations/create', [InvoiceController::class, 'createQuotation'])->name('quotations.create');
        Route::post('quotations', [InvoiceController::class, 'storeQuotation'])->name('quotations.store');
        Route::get('quotations/{quotation}/pdf', [InvoiceController::class, 'pdfQuotation'])->name('quotations.pdf');
        Route::post('quotations/{quotation}/convert', [InvoiceController::class, 'convertQuotation'])->name('quotations.convert');

        Route::get('expenses', [ExpenseController::class, 'index'])->name('expenses.index');
        Route::get('expenses/create', [ExpenseController::class, 'create'])->name('expenses.create');
        Route::post('expenses', [ExpenseController::class, 'store'])->name('expenses.store');
        Route::post('expenses/{expense}/approve', [ExpenseController::class, 'approve'])->name('expenses.approve');
        Route::post('expenses/{expense}/reject', [ExpenseController::class, 'reject'])->name('expenses.reject');
        Route::get('suppliers/create', [ExpenseController::class, 'createSupplier'])->name('suppliers.create');
        Route::post('suppliers', [ExpenseController::class, 'storeSupplier'])->name('suppliers.store');

        Route::get('budgets', [BudgetController::class, 'index'])->name('budgets.index');
        Route::get('budgets/create', [BudgetController::class, 'create'])->name('budgets.create');
        Route::post('budgets', [BudgetController::class, 'store'])->name('budgets.store');
        Route::get('budgets/{budget}', [BudgetController::class, 'show'])->name('budgets.show');

        Route::get('receipts', [ReceiptController::class, 'index'])->name('receipts.index');
        Route::get('receipts/create', [ReceiptController::class, 'create'])->name('receipts.create');
        Route::post('receipts', [ReceiptController::class, 'store'])->name('receipts.store');
        Route::get('receipts/{receipt}/pdf', [ReceiptController::class, 'pdf'])->name('receipts.pdf');

        Route::get('bulk-invoices', [BulkInvoiceController::class, 'index'])->name('bulk-invoices.index');
        Route::get('bulk-invoices/create', [BulkInvoiceController::class, 'create'])->name('bulk-invoices.create');
        Route::post('bulk-invoices', [BulkInvoiceController::class, 'store'])->name('bulk-invoices.store');

        Route::get('payroll', [PayrollController::class, 'index'])->name('payroll.index');
        Route::get('payroll/create', [PayrollController::class, 'create'])->name('payroll.create');
        Route::post('payroll', [PayrollController::class, 'store'])->name('payroll.store');
    });

    Route::prefix('school')->name('school.')->group(function () {
        Route::resource('classes', ClassController::class);
        Route::post('classes/{class}/enroll', [ClassController::class, 'enroll'])->name('classes.enroll');
        Route::delete('classes/{class}/students/{student}', [ClassController::class, 'removeStudent'])->name('classes.remove-student');

        Route::resource('clubs', ClubController::class);
        Route::post('clubs/{club}/assign', [ClubController::class, 'assign'])->name('clubs.assign');
        Route::delete('clubs/{club}/students/{student}', [ClubController::class, 'removeStudent'])->name('clubs.remove-student');

        Route::resource('students', StudentController::class);
        Route::get('students-export/csv', [StudentController::class, 'exportCsv'])->name('students.export');
        Route::get('students-import', [StudentController::class, 'importForm'])->name('students.import');
        Route::post('students-import', [StudentController::class, 'importCsv'])->name('students.import.store');
        Route::post('students/{student}/documents', [StudentController::class, 'storeDocument'])->name('students.documents.store');
        Route::resource('staff', StaffController::class)->parameters(['staff' => 'staff']);
        Route::resource('timetable', TimetableController::class)->except(['show'])->parameters(['timetable' => 'slot']);
        Route::get('attendance/report', [AttendanceController::class, 'termReport'])->name('attendance.term-report');
        Route::resource('attendance', AttendanceController::class)->only(['index', 'create', 'store']);
        Route::get('library/books/create', [LibraryController::class, 'create'])->name('library.create');
        Route::post('library/books', [LibraryController::class, 'store'])->name('library.store');
        Route::get('library/books/{book}/edit', [LibraryController::class, 'edit'])->name('library.edit');
        Route::put('library/books/{book}', [LibraryController::class, 'update'])->name('library.update');
        Route::delete('library/books/{book}', [LibraryController::class, 'destroy'])->name('library.destroy');
        Route::get('library', [LibraryController::class, 'index'])->name('library.index');
        Route::get('library/books/{book}/borrow', [LibraryController::class, 'borrowForm'])->name('library.borrow');
        Route::post('library/books/{book}/borrow', [LibraryController::class, 'borrow'])->name('library.borrow.store');
        Route::post('library/loans/{loan}/return', [LibraryController::class, 'returnLoan'])->name('library.return');
        Route::resource('discipline', DisciplineController::class)->except(['show'])->parameters(['discipline' => 'record']);

        Route::get('cases', [DisciplinaryCaseController::class, 'index'])->name('cases.index');
        Route::get('cases/{case}', [DisciplinaryCaseController::class, 'show'])->name('cases.show');
        Route::post('cases/{case}/advance', [DisciplinaryCaseController::class, 'advance'])->name('cases.advance');
        Route::post('cases/{case}/evidence', [DisciplinaryCaseController::class, 'attachEvidence'])->name('cases.evidence');

        Route::get('leadership', [LeadershipController::class, 'index'])->name('leadership.index');
        Route::get('leadership/{student}/edit', [LeadershipController::class, 'edit'])->name('leadership.edit');
        Route::put('leadership/{student}', [LeadershipController::class, 'update'])->name('leadership.update');

        Route::resource('grading-scales', GradingScaleController::class)->except(['show'])->parameters(['grading-scales' => 'gradingScale']);

        Route::get('grades', [GradeController::class, 'index'])->name('grades.index');
        Route::get('grades/entry', [GradeController::class, 'entry'])->name('grades.entry');
        Route::post('grades/entry', [GradeController::class, 'storeEntry'])->name('grades.entry.store');

        Route::get('exams', [ExamController::class, 'index'])->name('exams.index');
        Route::get('exams/results', [ExamController::class, 'results'])->name('exams.results');
        Route::post('exams/results/compile', [ExamController::class, 'compile'])->name('exams.results.compile');
        Route::post('exams/results/submit', [ExamController::class, 'submit'])->name('exams.results.submit');
        Route::post('exams/results/approve', [ExamController::class, 'approve'])->name('exams.results.approve');

        Route::get('academic-oversight', [AcademicOversightController::class, 'index'])->name('academic-oversight.index');
        Route::get('academic-oversight/results', [AcademicOversightController::class, 'results'])->name('academic-oversight.results');
        Route::post('academic-oversight/approve', [AcademicOversightController::class, 'approve'])->name('academic-oversight.approve');
        Route::post('academic-oversight/reject', [AcademicOversightController::class, 'reject'])->name('academic-oversight.reject');
        Route::post('academic-oversight/release', [AcademicOversightController::class, 'release'])->name('academic-oversight.release');
        Route::post('academic-oversight/reopen', [AcademicOversightController::class, 'reopen'])->name('academic-oversight.reopen');

        Route::get('report-cards', [ReportCardController::class, 'index'])->name('report-cards.index');
        Route::post('report-cards/generate', [ReportCardController::class, 'generate'])->name('report-cards.generate');
        Route::get('report-cards/{reportCard}/pdf', [ReportCardController::class, 'pdf'])->name('report-cards.pdf');
        Route::post('report-cards/{reportCard}/publish', [ReportCardController::class, 'publish'])->name('report-cards.publish');
        Route::put('report-cards/{reportCard}/remarks', [ReportCardController::class, 'updateRemarks'])->name('report-cards.remarks');
    });

    Route::prefix('system')->name('system.')->group(function () {
        Route::get('analytics', [AnalyticsController::class, 'index'])->name('analytics.index');
        Route::get('settings', [SettingsController::class, 'index'])->name('settings.index');
        Route::put('settings', [SettingsController::class, 'update'])->name('settings.update');
        Route::put('settings/email', [EmailSettingsController::class, 'update'])->name('email-settings.update');
        Route::post('settings/email/test', [EmailSettingsController::class, 'test'])->name('email-settings.test');
        Route::get('users', [UserController::class, 'index'])->name('users.index');
        Route::post('users/invite', [UserController::class, 'invite'])->name('users.invite');
        Route::put('users/{user}/roles', [UserController::class, 'assignRoles'])->name('users.assign-roles');
        Route::get('audit', [AuditController::class, 'index'])->name('audit.index');
        Route::get('backup', [BackupController::class, 'index'])->name('backup.index');
        Route::post('backup', [BackupController::class, 'run'])->name('backup.run');
        Route::get('academic-years', [AcademicYearController::class, 'index'])->name('academic-years.index');
        Route::get('report-builder', [ReportBuilderController::class, 'index'])->name('report-builder.index');
        Route::get('report-builder/export', [ReportBuilderController::class, 'export'])->name('report-builder.export');
        Route::get('import-export-history', [ImportExportHistoryController::class, 'index'])->name('import-export.history');

        Route::resource('announcements', AnnouncementController::class)->except(['show']);
    });

    Route::prefix('data')->name('data.')->group(function () {
        Route::get('export/{module}/{format}', [DataExportController::class, 'export'])->name('export');
        Route::get('import/{module}/template', [DataImportController::class, 'template'])->name('import.template');
        Route::get('import/{module}', [DataImportController::class, 'index'])->name('import.index');
        Route::post('import/{module}/upload', [DataImportController::class, 'upload'])->name('import.upload');
        Route::post('import/{module}/confirm', [DataImportController::class, 'confirm'])->name('import.confirm');
    });

    Route::prefix('communication')->name('communication.')->group(function () {
        Route::get('emails', [EmailCenterController::class, 'index'])->name('emails.index');
        Route::get('emails/compose', [EmailCenterController::class, 'compose'])->name('emails.compose');
        Route::post('emails', [EmailCenterController::class, 'store'])->name('emails.store');
        Route::get('emails/{emailMessage}', [EmailCenterController::class, 'show'])->name('emails.show');
        Route::post('emails/{emailMessage}/retry', [EmailCenterController::class, 'retry'])->name('emails.retry');
        Route::post('emails/preview-template', [EmailCenterController::class, 'previewTemplate'])->name('emails.preview-template');

        Route::get('email-templates', [EmailTemplateController::class, 'index'])->name('email-templates.index');
        Route::get('email-templates/create', [EmailTemplateController::class, 'create'])->name('email-templates.create');
        Route::post('email-templates', [EmailTemplateController::class, 'store'])->name('email-templates.store');
        Route::get('email-templates/{emailTemplate}/edit', [EmailTemplateController::class, 'edit'])->name('email-templates.edit');
        Route::put('email-templates/{emailTemplate}', [EmailTemplateController::class, 'update'])->name('email-templates.update');
        Route::post('email-templates/{emailTemplate}/duplicate', [EmailTemplateController::class, 'duplicate'])->name('email-templates.duplicate');
        Route::delete('email-templates/{emailTemplate}', [EmailTemplateController::class, 'destroy'])->name('email-templates.destroy');

        Route::get('emails/invoice/{invoice}/compose', [DocumentEmailController::class, 'composeInvoice'])->name('emails.invoice.compose');
        Route::post('emails/invoice/{invoice}/send', [DocumentEmailController::class, 'sendInvoice'])->name('emails.invoice.send');
        Route::get('emails/quotation/{quotation}/compose', [DocumentEmailController::class, 'composeQuotation'])->name('emails.quotation.compose');
        Route::post('emails/quotation/{quotation}/send', [DocumentEmailController::class, 'sendQuotation'])->name('emails.quotation.send');
        Route::get('emails/receipt/{receipt}/compose', [DocumentEmailController::class, 'composeReceipt'])->name('emails.receipt.compose');
        Route::post('emails/receipt/{receipt}/send', [DocumentEmailController::class, 'sendReceipt'])->name('emails.receipt.send');
    });

    Route::prefix('extras')->name('extras.')->group(function () {
        Route::resource('transport', TransportController::class)->except(['show'])->parameters(['transport' => 'route']);
        Route::resource('hostel', HostelController::class)->except(['show'])->parameters(['hostel' => 'room']);
        Route::resource('events', EventController::class);
        Route::post('events/{event}/approve', [EventController::class, 'approve'])->name('events.approve');
        Route::post('events/{event}/reject', [EventController::class, 'reject'])->name('events.reject');
        Route::resource('messages', MessageController::class)->except(['show']);
        Route::get('inventory', [InventoryController::class, 'index'])->name('inventory.index');
    });
});

