<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdminAuthController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ClassController;
use App\Http\Controllers\DestinationController;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ApplicantController;
use App\Http\Controllers\SuperAdminController;
use App\Http\Controllers\CentralBillingController;
use App\Http\Controllers\FlutterwaveController; 
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ParentPaymentController;
use App\Http\Controllers\BankAccountController;
use App\Http\Controllers\ImportController;

//Route::view('/', 'welcome')->name('home');

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

require __DIR__.'/settings.php';

require __DIR__.'/auth.php';

// Public landing page
Route::get('/', function () {
    return view('welcome');
})->name('home');

Route::middleware(['dynamic', 'auth', 'verified'])->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');
    Route::post('/profile/theme', [ProfileController::class, 'updateTheme'])->name('theme.update');
});

// Authentication routes
Route::middleware([
    \App\Http\Middleware\DynamicDatabaseConnection::class,
    \App\Http\Middleware\SetTenantDatabase::class
])->group(function () {
    Route::get('/login', [\App\Http\Controllers\AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [\App\Http\Controllers\AuthController::class, 'login'])->name('login.post');
});
Route::post('/logout', [\App\Http\Controllers\AuthController::class, 'logout'])->name('logout');

// Authenticated routes
Route::middleware(['dynamic', 'auth'])->group(function () {
    Route::get('/dashboard', [\App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');

   // Route::post('/profile/theme', [\App\Http\Controllers\ProfileController::class, 'updateTheme'])->name('theme.update');

    // Core routes — scoped by role inside PaymentController
    Route::resource('payments', \App\Http\Controllers\PaymentController::class);

    // Settings available to admin/super_admin roles
    Route::middleware(\App\Http\Middleware\AdminMiddleware::class)->group(function () {
        Route::resource('classes', \App\Http\Controllers\ClassController::class);
        Route::resource('destinations', \App\Http\Controllers\DestinationController::class);
        Route::resource('students', \App\Http\Controllers\StudentController::class);
        Route::resource('users', \App\Http\Controllers\UserController::class);

        Route::get('/reports/by-class', [\App\Http\Controllers\ReportController::class, 'byClass'])->name('reports.by-class');
        Route::get('/reports/by-destination', [\App\Http\Controllers\ReportController::class, 'byDestination'])->name('reports.by-destination');
        Route::get('/settings', [\App\Http\Controllers\SettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [\App\Http\Controllers\SettingController::class, 'store'])->name('settings.store');

        // Bank accounts (managed from the Settings > Bank Details tab)
        Route::post('/bank-accounts', [\App\Http\Controllers\BankAccountController::class, 'store'])->name('bank-accounts.store');
        Route::put('/bank-accounts/{bankAccount}', [\App\Http\Controllers\BankAccountController::class, 'update'])->name('bank-accounts.update');
        Route::delete('/bank-accounts/{bankAccount}', [\App\Http\Controllers\BankAccountController::class, 'destroy'])->name('bank-accounts.destroy');

        // Import routes
        Route::post('/import', [\App\Http\Controllers\ImportController::class, 'import'])->name('import');
        Route::get('/import/template/{type}', [\App\Http\Controllers\ImportController::class, 'downloadTemplate'])->name('import.template');
    });

    // Parent portal
    Route::middleware('parent')->prefix('parent')->name('parent.')->group(function () {
        Route::get('/dashboard', [\App\Http\Controllers\ParentPaymentController::class, 'index'])->name('dashboard');
        Route::post('/link-child', [\App\Http\Controllers\ParentPaymentController::class, 'linkChild'])->name('linkChild');
        Route::get('/pay/{student}', [\App\Http\Controllers\ParentPaymentController::class, 'create'])->name('pay');
        Route::post('/pay/{student}', [\App\Http\Controllers\ParentPaymentController::class, 'store'])->name('pay.store');
        Route::post('/pay/{student}/flutterwave', [\App\Http\Controllers\ParentPaymentController::class, 'payWithFlutterwave'])->name('pay.flutterwave');
        Route::get('/flutterwave/callback', [\App\Http\Controllers\ParentPaymentController::class, 'flutterwaveCallback'])->name('flutterwave.callback');
    });
});


Route::prefix('admin')->as('admin.')->group(function () {
    Route::get('/', function () {
        return redirect()->route('admin.dashboard');
    });

    // Admin Authentication
    Route::get('/login', [AdminAuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AdminAuthController::class, 'login'])->name('login.post');
    Route::post('/logout', [AdminAuthController::class, 'logout'])->name('logout');

    Route::middleware(['auth', 'admin'])->group(function () {
        Route::get('/dashboard', [CentralBillingController::class, 'dashboard'])->name('dashboard');
        Route::get('/applicants', [SuperAdminController::class, 'applicants'])->name('applicants.index');
        Route::post('/approve/{applicant}', [SuperAdminController::class, 'approve'])->name('approve');
        Route::post('/migrate/{applicant}', [SuperAdminController::class, 'migrate'])->name('migrate');
        Route::post('/reject/{applicant}', [SuperAdminController::class, 'reject'])->name('reject');
        Route::post('/applicants/{applicant}/approve-proof', [SuperAdminController::class, 'approveProof'])->name('applicants.approve-proof');

        // Billing & Subscriptions
        Route::prefix('billing')->name('billing.')->group(function () {
            Route::get('/', [CentralBillingController::class, 'index'])->name('index');
            Route::get('/settings', [CentralBillingController::class, 'settings'])->name('settings');
            Route::post('/settings', [CentralBillingController::class, 'updateSettings'])->name('settings.update');
            Route::post('/pay/{invoice}', [CentralBillingController::class, 'markAsPaid'])->name('pay');
            Route::post('/toggle/{tenant}', [CentralBillingController::class, 'toggleTenantStatus'])->name('toggle');

            // New sections from new folders
            Route::get('/payments', [CentralBillingController::class, 'paymentsIndex'])->name('payments.index');
            Route::get('/payments/uploads', [CentralBillingController::class, 'uploadsIndex'])->name('payments.uploads');
            Route::post('/payments/uploads/{upload}/approve', [CentralBillingController::class, 'approveUpload'])->name('payments.uploads.approve');
            Route::post('/payments/uploads/{upload}/reject', [CentralBillingController::class, 'rejectUpload'])->name('payments.uploads.reject');
            Route::get('/payments/create', [CentralBillingController::class, 'createPayment'])->name('payments.create');
            Route::post('/payments/store', [CentralBillingController::class, 'storePayment'])->name('payments.store');
            Route::get('/payments/search-tenants', [CentralBillingController::class, 'searchTenants'])->name('payments.search-tenants');

            Route::get('/baddebts', [CentralBillingController::class, 'badDebtsIndex'])->name('baddebts.index');
            Route::get('/baddebts/show/{tenant}', [CentralBillingController::class, 'showDebtor'])->name('baddebts.show');
            Route::get('/baddebts/statement/{tenant}', [CentralBillingController::class, 'tenantStatement'])->name('baddebts.statement');
        });
    });

    // Tenant Management
     Route::resource('tenants', App\Http\Controllers\TenantManagementController::class)->except(['create', 'store']);
});