<?php

use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminLoanController;
use App\Http\Controllers\Admin\AdminLoanDashboardController;
use App\Http\Controllers\Admin\AdminWithdrawController;
use App\Http\Controllers\Admin\BalanceAdjustController;
use App\Http\Controllers\Admin\BannerController;
use App\Http\Controllers\Admin\CompanySetupController;
use App\Http\Controllers\Admin\DepositMethodController;
use App\Http\Controllers\Admin\DepositRuleController;
use App\Http\Controllers\Admin\InstallmentReportController;
use App\Http\Controllers\Admin\LoanSettingsController;
use App\Http\Controllers\Admin\PaymentMethodController;
use App\Http\Controllers\Admin\UserManageController;
// routes/web.php
use App\Http\Controllers\Admin\WithdrawRequestController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\BellNotificationController;
use App\Http\Controllers\DashboardController;


use App\Http\Controllers\LoanController;


use App\Http\Controllers\LoanDepositController;


// routes/web.php

use App\Http\Controllers\LoanInstallmentController;

// routes/web.php

use App\Http\Controllers\ProfileController;


use App\Http\Controllers\UploadAppController;
use App\Http\Controllers\WithdrawController;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;

Route::get('/storage-link', function () {

    Artisan::call('route:clear');
    Artisan::call('config:clear');
    Artisan::call('cache:clear');
    Artisan::call('view:clear');
    Artisan::call('route:cache');
    Artisan::call('config:cache');
    Artisan::call('view:cache');
    Artisan::call('storage:link');
    return '<h2>Storage Link Successfully!</h2>';
});


Route::get('/migrate', function () {

    Artisan::call('route:clear');
    Artisan::call('config:clear');
    Artisan::call('cache:clear');
    Artisan::call('migrate');
  
    return '<h2>migrate Successfully!</h2>';
});



Route::middleware(['auth'])->group(function () {

    // Full list page (See More)
    Route::get('/notifications', [BellNotificationController::class, 'index'])
        ->name('notifications.index');

    // Header dropdown real-time poll
    Route::get('/notifications/poll', [BellNotificationController::class, 'poll'])
        ->name('notifications.poll');

    // Single notification read
    Route::post('/notifications/{notification}/read', [BellNotificationController::class, 'markRead'])
        ->name('notifications.read');

    // All notifications read
    Route::post('/notifications/read-all', [BellNotificationController::class, 'markAllRead'])
        ->name('notifications.readAll');
});



Route::get('/company-setups',                [CompanySetupController::class, 'index'])->name('company-setups.index');
Route::get('/company-setups/create',         [CompanySetupController::class, 'create'])->name('company-setups.create');
Route::post('/company-setups',               [CompanySetupController::class, 'store'])->name('company-setups.store');
Route::get('/company-setups/{companySetup}/edit', [CompanySetupController::class, 'edit'])->name('company-setups.edit');
Route::put('/company-setups/{companySetup}', [CompanySetupController::class, 'update'])->name('company-setups.update');
Route::delete('/company-setups/{companySetup}', [CompanySetupController::class, 'destroy'])->name('company-setups.destroy');



Route::prefix('admin')->middleware(['auth'])->group(function () {
    Route::get('upload-apps',          [UploadAppController::class, 'index'])->name('upload-apps.index');
    Route::get('upload-apps/create',   [UploadAppController::class, 'create'])->name('upload-apps.create');
    Route::post('upload-apps',         [UploadAppController::class, 'store'])->name('upload-apps.store');
    Route::get('upload-apps/{app}/edit', [UploadAppController::class, 'edit'])->name('upload-apps.edit');
    Route::put('upload-apps/{app}',    [UploadAppController::class, 'update'])->name('upload-apps.update');
    Route::delete('upload-apps/{app}', [UploadAppController::class, 'destroy'])->name('upload-apps.destroy');
});


Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth']) // তোমার admin middleware থাকলে সেটা দিয়ো
    ->group(function () {

        Route::resource('banners', BannerController::class)
            ->except(['show']);

        // শুধু status toggle করার আলাদা রুট
        Route::post('/banners/{banner}/toggle-status', [BannerController::class, 'toggleStatus'])
            ->name('banners.toggle-status');
    });


Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth']) // এখানে তোমার admin middleware থাকলে সেটা ও দাও
    ->group(function () {

        // User list
        Route::get('/users', [UserManageController::class, 'index'])
            ->name('users.index');

        // Edit user
        Route::get('/users/{user}/edit', [UserManageController::class, 'edit'])
            ->name('users.edit');

        // Update user info
        Route::put('/users/{user}', [UserManageController::class, 'update'])
            ->name('users.update');

        // Delete user
        Route::delete('/users/{user}', [UserManageController::class, 'destroy'])
            ->name('users.destroy');

        // Toggle status active/blocked (একটা button এর মাধ্যমে)
        Route::post('/users/{user}/toggle-status', [UserManageController::class, 'toggleStatus'])
            ->name('users.toggle-status');

        // Change password
        Route::put('/users/{user}/password', [UserManageController::class, 'updatePassword'])
            ->name('users.update-password');
    });


Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth']) // তোমার admin middleware থাকলে এখানে দাও
    ->group(function () {

        // Balance adjust page (search + form)
        Route::get('/balance-adjust', [BalanceAdjustController::class, 'index'])
            ->name('balance.adjust.index');

        // নির্দিষ্ট user-এর balance এ action
        Route::post('/balance-adjust/{user}', [BalanceAdjustController::class, 'apply'])
            ->name('balance.adjust.apply');
    });


Route::prefix('admin')->middleware(['auth'])->name('admin.')->group(function () {
    Route::get('/profile', [\App\Http\Controllers\Admin\ProfileController::class, 'edit'])
        ->name('profile.edit');

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

    Route::put('/profile/password', [\App\Http\Controllers\Admin\ProfileController::class, 'updatePassword'])
        ->name('profile.password');
});


Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth'])
    ->group(function () {

        // ... আগের সব admin routes ...

        // Installment report (per user)
        Route::get('/installment-report', [InstallmentReportController::class, 'index'])
            ->name('installments.report');
    });


 

Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth']) // তোমার admin middleware যেটা আছে
    ->group(function () {

        // নির্দিষ্ট loan এর সব কিস্তি
        Route::get('/loans/{loan}/installments', [LoanInstallmentController::class, 'index'])
            ->name('loans.installments');

        // এক কিস্তি edit
        Route::get('/installments/{installment}/edit', [LoanInstallmentController::class, 'edit'])
            ->name('installments.edit');

        // কিস্তি update
        Route::put('/installments/{installment}', [LoanInstallmentController::class, 'update'])
            ->name('installments.update');

        // ✅ latest paid installments list
        Route::get('/installments/paid', [LoanInstallmentController::class, 'paidIndex'])
            ->name('installments.paid');
    });


Route::middleware(['auth'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        Route::get('/withdraw-requests', [WithdrawRequestController::class, 'index'])
            ->name('withdraw.index');

        Route::get('/withdraw-requests/{withdraw}/edit', [WithdrawRequestController::class, 'edit'])
            ->name('withdraw.edit');

        Route::put('/withdraw-requests/{withdraw}', [WithdrawRequestController::class, 'update'])
            ->name('withdraw.update');
    });

 

Route::prefix('admin/deposit-methods')
    ->name('admin.deposit-methods.')
    ->middleware(['auth'])
    ->group(function () {

        // MOBILE
        Route::get('mobile',              [DepositMethodController::class, 'mobileIndex'])->name('mobile.index');
        Route::get('mobile/create',       [DepositMethodController::class, 'mobileCreate'])->name('mobile.create');
        Route::post('mobile',             [DepositMethodController::class, 'mobileStore'])->name('mobile.store');
        Route::get('mobile/{mobileMethod}/edit', [DepositMethodController::class, 'mobileEdit'])->name('mobile.edit');
        Route::put('mobile/{mobileMethod}',      [DepositMethodController::class, 'mobileUpdate'])->name('mobile.update');
        Route::delete('mobile/{mobileMethod}',   [DepositMethodController::class, 'mobileDestroy'])->name('mobile.destroy');

        // BANK
        Route::get('bank',            [DepositMethodController::class, 'bankIndex'])->name('bank.index');
        Route::get('bank/create',     [DepositMethodController::class, 'bankCreate'])->name('bank.create');
        Route::post('bank',           [DepositMethodController::class, 'bankStore'])->name('bank.store');
        Route::get('bank/{bankMethod}/edit', [DepositMethodController::class, 'bankEdit'])->name('bank.edit');
        Route::put('bank/{bankMethod}',      [DepositMethodController::class, 'bankUpdate'])->name('bank.update');
        Route::delete('bank/{bankMethod}',   [DepositMethodController::class, 'bankDestroy'])->name('bank.destroy');
    });

Route::middleware(['auth'])   // অথবা তোমার যেই admin middleware আছে
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        Route::get('loan-settings', [LoanSettingsController::class, 'index'])
            ->name('loan-settings.index');

        // Amount
        Route::post('loan-settings/amounts', [LoanSettingsController::class, 'storeAmount'])
            ->name('loan-settings.amounts.store');
        Route::patch('loan-settings/amounts/{amount}', [LoanSettingsController::class, 'updateAmount'])
            ->name('loan-settings.amounts.update');
        Route::delete('loan-settings/amounts/{amount}', [LoanSettingsController::class, 'destroyAmount'])
            ->name('loan-settings.amounts.destroy');

        // Tenure
        Route::post('loan-settings/tenures', [LoanSettingsController::class, 'storeTenure'])
            ->name('loan-settings.tenures.store');
        Route::patch('loan-settings/tenures/{tenure}', [LoanSettingsController::class, 'updateTenure'])
            ->name('loan-settings.tenures.update');
        Route::delete('loan-settings/tenures/{tenure}', [LoanSettingsController::class, 'destroyTenure'])
            ->name('loan-settings.tenures.destroy');

        // Purposes
        Route::post('loan-settings/purposes', [LoanSettingsController::class, 'storePurpose'])
            ->name('loan-settings.purposes.store');
        Route::patch('loan-settings/purposes/{purpose}', [LoanSettingsController::class, 'updatePurpose'])
            ->name('loan-settings.purposes.update');
        Route::delete('loan-settings/purposes/{purpose}', [LoanSettingsController::class, 'destroyPurpose'])
            ->name('loan-settings.purposes.destroy');
    });

Route::get('/artisan', function () {

    Artisan::call('route:clear');
    Artisan::call('config:clear');
    Artisan::call('cache:clear');
    Artisan::call('view:clear');
    Artisan::call('optimize:clear');

    Artisan::call('route:cache');
    Artisan::call('config:cache');
    Artisan::call('view:cache');

    return '<h2>All Artisan Commands Executed Successfully!</h2>';
});

Route::middleware(['auth', 'role:admin'])->prefix('admin/loans')->group(function () {
    Route::get('/dashboard', [AdminLoanDashboardController::class, 'index'])->name('admin.loans.dashboard');
    Route::post('/installments/{installment}/approve', [AdminLoanDashboardController::class, 'approve'])
        ->name('admin.installments.approve');
});

Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::resource('deposit-rules', DepositRuleController::class)
        ->parameters(['deposit-rules' => 'deposit_rule'])
        ->names('deposit_rules');

    Route::patch('deposit-rules/{deposit_rule}/toggle', [DepositRuleController::class, 'toggle'])
        ->name('deposit_rules.toggle');
});

Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::resource('payment-methods', PaymentMethodController::class)
        ->parameters(['payment-methods' => 'payment_method']) // route-model-binding param
        ->names('payment_methods');

    Route::patch('payment-methods/{payment_method}/toggle', [PaymentMethodController::class, 'toggle'])
        ->name('payment_methods.toggle');
});

// =================admin-route=========================
Route::middleware(['auth', 'role:admin'])->group(function () {
    Route::get('/admin/dashboard', [AdminDashboardController::class, 'admin_dashboard'])->name('admin.dashboard');

});

// =====================user-route=======================
Route::middleware(['auth', 'role:student,teacher,guardian,business,employee,other'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');

    Route::get('/loan/installments', [LoanInstallmentController::class, 'index'])->name('loan.installments.index');

    Route::get('/loan/installments/{installment}/pay', [LoanInstallmentController::class, 'payForm'])->name('loan.installments.pay.form');

    Route::post('/loan/installments/{installment}/pay', [LoanInstallmentController::class, 'payStore'])->name('loan.installments.pay.store');

    Route::get('/loan/installments/{installment}/success', [LoanInstallmentController::class, 'success'])->name('loan.installments.success');

    Route::get('/my-loans', [LoanController::class, 'myLoans'])->name('loan.my');

    Route::get('/loan/deposit', [LoanDepositController::class, 'selectLoan'])->name('loan.deposit.index');

    //   ===========================

    Route::get('/loan/apply', [LoanController::class, 'create'])->name('loan.apply');
    Route::post('/loan/apply', [LoanController::class, 'store'])->name('loan.store');

    Route::get('/loan/{loan}/summary', [LoanController::class, 'summary'])->name('loan.summary');
    Route::post('/loan/{loan}/confirm', [LoanController::class, 'confirmSummary'])->name('loan.confirm');

    Route::get('/loan/{loan}/nid', [LoanController::class, 'nidForm'])->name('loan.nid');
    Route::post('/loan/{loan}/nid', [LoanController::class, 'nidUpload'])->name('loan.nid.upload');

    // Deposit method select
    Route::get('/loan/{loan}/deposit/method', [LoanDepositController::class, 'method'])
        ->name('loan.deposit.method');

    // Gateway form (method = mobile:bkash / bank:islami-bank)
    Route::get('/loan/{loan}/deposit/{method}', [LoanDepositController::class, 'gatewayForm'])
        ->where('method', '(mobile|bank):[A-Za-z0-9_-]+')
        ->name('loan.deposit.gateway');

    // Deposit submit
    Route::post('/loan/{loan}/deposit', [LoanDepositController::class, 'store'])
        ->name('loan.deposit.store');

    // Success page
    Route::get('/loan/{loan}/deposit/success/{deposit}', [LoanDepositController::class, 'success'])
        ->whereNumber('deposit')
        ->name('loan.deposit.success');

    // All transactions
    Route::get('/transactions', [LoanController::class, 'transactions'])->name('transactions');

    // ======================

    Route::get('/withdraw', [WithdrawController::class, 'create'])->name('withdraw.create');
    Route::post('/withdraw', [WithdrawController::class, 'store'])->name('withdraw.store');

    Route::get('/wallet/withdraw/success/{id}', [WithdrawController::class, 'success'])->name('withdraw.success');

    Route::get('/profile', function () {
        return view('frontend.profile.index');
    })->name('profile');

    // photo
    Route::get('/profile/photo', [ProfileController::class, 'editPhoto'])->name('profile.photo.edit');
    Route::post('/profile/photo', [ProfileController::class, 'updatePhoto'])->name('profile.photo.update');

    // password
    Route::get('/profile/password', [ProfileController::class, 'editPassword'])->name('profile.password.edit');
    Route::post('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password.update');
    Route::post('/profile/password/check', [ProfileController::class, 'checkOldPassword'])->name('profile.password.check');

    // info
    Route::get('/profile/info', [ProfileController::class, 'editInfo'])->name('profile.info.edit');
    Route::post('/profile/info', [ProfileController::class, 'updateInfo'])->name('profile.info.update');

});

Route::get('/about', function () {
    return view('frontend.about.index');
})->name('about');

Route::get('/contact', function () {
    return view('frontend.contact.index');
})->name('contact');

Route::middleware(['auth'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {

        // ... আগের loan routes গুলো ...

        // Withdraw admin panel
        Route::get('/withdraws', [AdminWithdrawController::class, 'index'])->name('withdraws.index');
        Route::get('/withdraws/{withdraw}', [AdminWithdrawController::class, 'show'])->name('withdraws.show');
        Route::post('/withdraws/{withdraw}/approve', [AdminWithdrawController::class, 'approve'])->name('withdraws.approve');
        Route::post('/withdraws/{withdraw}/reject', [AdminWithdrawController::class, 'reject'])->name('withdraws.reject');
    });

// Admin side (ধরি role = admin)
Route::middleware(['auth'])->group(function () {
    Route::get('/admin/loans', [AdminLoanController::class, 'index'])->name('admin.loans.index');
    Route::get('/admin/loans/{loan}', [AdminLoanController::class, 'show'])->name('admin.loans.show');
    Route::post('/admin/loans/{loan}/approve', [AdminLoanController::class, 'approve'])->name('admin.loans.approve');
    Route::post('/admin/loans/{loan}/reject', [AdminLoanController::class, 'reject'])->name('admin.loans.reject');

    Route::post('/admin/deposits/{deposit}/approve', [AdminLoanController::class, 'approveDeposit'])->name('admin.deposits.approve');
    Route::post('/admin/deposits/{deposit}/reject', [AdminLoanController::class, 'rejectDeposit'])->name('admin.deposits.reject');
});

Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// ==========guest-route================
Route::middleware('guest.redirect')->group(function () {
    Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('/register', [RegisterController::class, 'register']);
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
    Route::get('/', function () {
        return view('frontend.dashboard');
    });
});
