<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Client Controllers
|--------------------------------------------------------------------------
*/

use App\Http\Controllers\ClientProfileController;
use App\Http\Controllers\ClientKycController;
use App\Http\Controllers\ClientEmploymentController;
use App\Http\Controllers\ClientApplicationController;
use App\Http\Controllers\CashfreePaymentController;
use App\Http\Controllers\ClientLoanController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\SupportController;
use App\Http\Controllers\ClientBankDetailController;


/*
|--------------------------------------------------------------------------
| Admin Controllers
|--------------------------------------------------------------------------
*/

use App\Http\Controllers\Admin\AdminAuthController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminApplicationController;
use App\Http\Controllers\Admin\ClientController;
use App\Http\Controllers\Admin\AdminKycController;
use App\Http\Controllers\Admin\PaymentController;
use App\Http\Controllers\Admin\EmiCardController;
use App\Http\Controllers\Admin\LoanController;
use App\Http\Controllers\Admin\AdminLoanCreateController;
use App\Http\Controllers\Admin\LoanSettingController;
use App\Http\Controllers\Admin\SystemSettingController;
use App\Http\Controllers\Admin\OverdueController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SupportController as AdminSupportController;
use App\Http\Controllers\Admin\AdminProfileSettingsController;


/*
|--------------------------------------------------------------------------
| Homepage
|--------------------------------------------------------------------------
*/

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


/*
|--------------------------------------------------------------------------
| Admin Authentication
|--------------------------------------------------------------------------
*/

Route::get(
    '/admin/login',
    [AdminAuthController::class, 'showLogin']
)->name('admin.login');

Route::post(
    '/admin/login',
    [AdminAuthController::class, 'login']
)->name('admin.login.submit');

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


/*
|--------------------------------------------------------------------------
| Authenticated Client Routes
|--------------------------------------------------------------------------
*/

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

    /*
    |--------------------------------------------------------------------------
    | Dashboard
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/dashboard',
        [ClientApplicationController::class, 'dashboard']
    )->name('client.dashboard');


    /*
    |--------------------------------------------------------------------------
    | EMI Card
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/emi-card',
        [ClientApplicationController::class, 'emiCard']
    )->name('client.emi-card');


    /*
    |--------------------------------------------------------------------------
    | Client Application
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/my-application',
        [ClientApplicationController::class, 'index']
    )->name('client.application');

    Route::get(
        '/application-review',
        [ClientApplicationController::class, 'review']
    )->name('client.review');


    /*
    |--------------------------------------------------------------------------
    | Personal Details
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/personal-details',
        [ClientProfileController::class, 'create']
    )->name('client.personal-details');

    Route::post(
        '/personal-details',
        [ClientProfileController::class, 'store']
    )->name('client.personal-details.store');


    /*
    |--------------------------------------------------------------------------
    | KYC
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/kyc',
        [ClientKycController::class, 'create']
    )->name('client.kyc');

    Route::post(
        '/kyc',
        [ClientKycController::class, 'store']
    )->name('client.kyc.store');



    Route::get(
    '/bank-details',
    [ClientBankDetailController::class, 'create']
)->name('client.bank-details');

Route::post(
    '/bank-details',
    [ClientBankDetailController::class, 'store']
)->name('client.bank-details.store');


    /*
    |--------------------------------------------------------------------------
    | Job Details
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/job-details',
        [ClientEmploymentController::class, 'create']
    )->name('client.job-details');

    Route::post(
        '/job-details',
        [ClientEmploymentController::class, 'store']
    )->name('client.job-details.store');


    /*
    |--------------------------------------------------------------------------
    | Client Loans
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/loans',
        [ClientLoanController::class, 'index']
    )->name('client.loans');

    Route::get(
        '/loans/apply',
        [ClientLoanController::class, 'create']
    )->name('client.loans.create');

    Route::post(
        '/loans/calculate',
        [ClientLoanController::class, 'calculate']
    )->name('client.loans.calculate');

    Route::post(
        '/loans',
        [ClientLoanController::class, 'store']
    )->name('client.loans.store');

    Route::get(
        '/loans/{loan}',
        [ClientLoanController::class, 'show']
    )->name('client.loans.show');

    Route::post(
        '/loans/{loan}/processing-fee/pay',
        [ClientLoanController::class, 'payProcessingFee']
    )->name('client.loans.pay-processing-fee');


    /*
    |--------------------------------------------------------------------------
    | Client Payments
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/payments',
        [ClientApplicationController::class, 'payments']
    )->name('client.payments');


    /*
    |--------------------------------------------------------------------------
    | Client Support
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/support',
        [SupportController::class, 'index']
    )->name('client.support.index');

    Route::get(
        '/support/create',
        [SupportController::class, 'create']
    )->name('client.support.create');

    Route::post(
        '/support',
        [SupportController::class, 'store']
    )->name('client.support.store');

    Route::get(
        '/support/{ticket}',
        [SupportController::class, 'show']
    )->name('client.support.show');

    Route::post(
        '/support/{ticket}/message',
        [SupportController::class, 'message']
    )->name('client.support.message');


    /*
    |--------------------------------------------------------------------------
    | Notifications
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/notifications',
        [NotificationController::class, 'index']
    )->name('notifications.index');

    Route::post(
        '/notifications/{id}/read',
        [NotificationController::class, 'read']
    )->name('notifications.read');

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


    /*
    |--------------------------------------------------------------------------
    | Cashfree Payment - EMI Card Unlock
    |--------------------------------------------------------------------------
    */

    Route::post(
        '/payment/cashfree/create',
        [CashfreePaymentController::class, 'createOrder']
    )->name('cashfree.create');


    /*
    |--------------------------------------------------------------------------
    | Client EMI Cashfree Payment
    |--------------------------------------------------------------------------
    */

    Route::post(
        '/loans/{loanId}/installments/{installmentId}/pay',
        [CashfreePaymentController::class, 'createEmiOrder']
    )->name('cashfree.emi.create');

});


/*
|--------------------------------------------------------------------------
| Cashfree Payment Return - EMI Card Unlock
|--------------------------------------------------------------------------
*/

Route::get(
    '/payment/cashfree/return',
    [CashfreePaymentController::class, 'return']
)->name('cashfree.return');


/*
|--------------------------------------------------------------------------
| Cashfree EMI Payment Return
|--------------------------------------------------------------------------
*/

Route::get(
    '/payment/cashfree/emi/return',
    [CashfreePaymentController::class, 'emiReturn']
)->name('cashfree.emi.return');


/*
|--------------------------------------------------------------------------
| Admin Protected Routes
|--------------------------------------------------------------------------
*/

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

    /*
    |--------------------------------------------------------------------------
    | Admin Dashboard
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/dashboard',
        [AdminDashboardController::class, 'index']
    )->name('admin.dashboard');


    /*
    |--------------------------------------------------------------------------
    | Admin Applications
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/applications',
        [AdminApplicationController::class, 'index']
    )->name('admin.applications');


    /*
|--------------------------------------------------------------------------
| Admin Clients
|--------------------------------------------------------------------------
*/

Route::get(
    '/admin/clients',
    [ClientController::class, 'index']
)->name('admin.clients');


/*
|--------------------------------------------------------------------------
| Add Client
|--------------------------------------------------------------------------
*/

Route::get(
    '/admin/clients/create',
    [ClientController::class, 'create']
)->name('admin.clients.create');

Route::post(
    '/admin/clients',
    [ClientController::class, 'store']
)->name('admin.clients.store');


/*
|--------------------------------------------------------------------------
| Client Profile
|--------------------------------------------------------------------------
*/

Route::get(
    '/admin/clients/{client}',
    [ClientController::class, 'show']
)->name('admin.clients.show');

    /*
    |--------------------------------------------------------------------------
    | KYC Verification
    |--------------------------------------------------------------------------
    */
    
    Route::get(
    '/admin/kyc',
    [AdminKycController::class, 'index']
)->name('admin.kyc.index');

    Route::get(
    '/admin/kyc/{client}',
    [AdminKycController::class, 'show']
)->name('admin.kyc.show');

    Route::post(
        '/admin/clients/{client}/kyc/verify',
        [AdminKycController::class, 'verify']
    )->name('admin.kyc.verify');

    Route::post(
        '/admin/clients/{client}/kyc/reject',
        [AdminKycController::class, 'reject']
    )->name('admin.kyc.reject');
    Route::post(
    '/admin/clients/{client}/kyc/card-unlock-payment',
    [AdminKycController::class, 'manualCardUnlockPayment']
)->name('admin.kyc.manual-card-unlock-payment');


    /*
    |--------------------------------------------------------------------------
    | KYC Documents
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/clients/{client}/kyc/aadhaar',
        [AdminKycController::class, 'viewAadhaar']
    )->name('admin.kyc.aadhaar');

    Route::get(
        '/admin/clients/{client}/kyc/pan',
        [AdminKycController::class, 'viewPan']
    )->name('admin.kyc.pan');


    /*
    |--------------------------------------------------------------------------
    | Admin Payments
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/payments',
        [PaymentController::class, 'index']
    )->name('admin.payments');

    Route::post(
        '/admin/payments/manual',
        [PaymentController::class, 'storeManual']
    )->name('admin.payments.manual');

    Route::post(
        '/admin/clients/{client}/card/unlock',
        [PaymentController::class, 'unlockCard']
    )->name('admin.card.unlock');

    Route::get(
        '/admin/payments/{payment}',
        [PaymentController::class, 'show']
    )->name('admin.payments.show');


    /*
    |--------------------------------------------------------------------------
    | Admin EMI Cards
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/emi-cards',
        [EmiCardController::class, 'index']
    )->name('admin.emi-cards');

    Route::post(
        '/admin/emi-cards/generate',
        [EmiCardController::class, 'generate']
    )->name('admin.emi-cards.generate');

    Route::get(
        '/admin/emi-cards/{card}',
        [EmiCardController::class, 'show']
    )->name('admin.emi-cards.show');

    Route::put(
        '/admin/emi-cards/{card}/limit',
        [EmiCardController::class, 'updateLimit']
    )->name('admin.emi-cards.limit');

    Route::post(
        '/admin/emi-cards/{card}/approve',
        [EmiCardController::class, 'approve']
    )->name('admin.emi-cards.approve');

    Route::post(
        '/admin/emi-cards/{card}/activate',
        [EmiCardController::class, 'activate']
    )->name('admin.emi-cards.activate');

    Route::post(
        '/admin/emi-cards/{card}/block',
        [EmiCardController::class, 'block']
    )->name('admin.emi-cards.block');


    /*
    |--------------------------------------------------------------------------
    | Admin Loan Create
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/loans/create',
        [AdminLoanCreateController::class, 'create']
    )->name('admin.loans.create');

    Route::post(
        '/admin/loans/create',
        [AdminLoanCreateController::class, 'store']
    )->name('admin.loans.create.store');


    /*
    |--------------------------------------------------------------------------
    | Admin Loans
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/loans',
        [LoanController::class, 'index']
    )->name('admin.loans');

    Route::get(
        '/admin/loans/{loan}',
        [LoanController::class, 'show']
    )->name('admin.loans.show');

    Route::post(
        '/admin/loans/{loan}/approve',
        [LoanController::class, 'approve']
    )->name('admin.loans.approve');

    Route::post(
        '/admin/loans/{loan}/reject',
        [LoanController::class, 'reject']
    )->name('admin.loans.reject');

    Route::post(
        '/admin/loans/{loan}/disburse',
        [LoanController::class, 'disburse']
    )->name('admin.loans.disburse');

    Route::post(
        '/admin/loans/{loan}/cancel',
        [LoanController::class, 'cancel']
    )->name('admin.loans.cancel');

    Route::post(
        '/admin/loans/{loan}/close',
        [LoanController::class, 'close']
    )->name('admin.loans.close');

    Route::post(
        '/admin/loans/update-overdue',
        [LoanController::class, 'updateOverdueStatus']
    )->name('admin.loans.update-overdue');


    /*
    |--------------------------------------------------------------------------
    | Admin Loan EMI Payments
    |--------------------------------------------------------------------------
    */

    Route::post(
        '/admin/loans/{loan}/installments/{installment}/pay',
        [LoanController::class, 'payInstallment']
    )->name('admin.loans.pay-installment');

    Route::post(
        '/admin/loans/{loan}/installments/{installment}/pay-overdue',
        [LoanController::class, 'payOverdueInstallment']
    )->name('admin.loans.pay-overdue');

    Route::post(
        '/admin/loans/{loan}/pay-outstanding',
        [LoanController::class, 'payOutstanding']
    )->name('admin.loans.pay-outstanding');


    /*
    |--------------------------------------------------------------------------
    | Admin Reports
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/reports',
        [ReportController::class, 'index']
    )->name('admin.reports');

    Route::get(
        '/admin/reports/pdf',
        [ReportController::class, 'pdf']
    )->name('admin.reports.pdf');

    Route::get(
        '/admin/reports/collection-pdf',
        [ReportController::class, 'collectionPdf']
    )->name('admin.reports.collection-pdf');

    Route::get(
        '/admin/reports/loan-pdf',
        [ReportController::class, 'loanPdf']
    )->name('admin.reports.loan-pdf');


    /*
    |--------------------------------------------------------------------------
    | Admin Processing Fee Payment
    |--------------------------------------------------------------------------
    */

    Route::post(
        '/admin/loans/{loan}/processing-fee/pay',
        [LoanController::class, 'payProcessingFee']
    )->name('admin.loans.pay-processing-fee');


    /*
    |--------------------------------------------------------------------------
    | Loan Settings
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/loan-settings',
        [LoanSettingController::class, 'edit']
    )->name('admin.loan-settings.edit');

    Route::put(
        '/admin/loan-settings',
        [LoanSettingController::class, 'update']
    )->name('admin.loan-settings.update');


    /*
    |--------------------------------------------------------------------------
    | System Settings
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/settings',
        [SystemSettingController::class, 'edit']
    )->name('admin.system-settings.edit');

    Route::put(
        '/admin/settings',
        [SystemSettingController::class, 'update']
    )->name('admin.system-settings.update');


    /*
|--------------------------------------------------------------------------
| Admin Profile Settings
|--------------------------------------------------------------------------
*/

Route::get(
    '/admin/profile-settings',
    [AdminProfileSettingsController::class, 'edit']
)->name('admin.profile-settings.edit');

Route::put(
    '/admin/profile-settings',
    [AdminProfileSettingsController::class, 'update']
)->name('admin.profile-settings.update');

Route::put(
    '/admin/profile-settings/password',
    [AdminProfileSettingsController::class, 'updatePassword']
)->name('admin.profile-settings.password');

Route::post(
    '/admin/profile-settings/admin',
    [AdminProfileSettingsController::class, 'storeAdmin']
)->name('admin.profile-settings.admin.store');

Route::get(
    '/admin/profile-settings/admin/{admin}/edit',
    [AdminProfileSettingsController::class, 'editAdmin']
)->name('admin.profile-settings.admin.edit');

Route::put(
    '/admin/profile-settings/admin/{admin}',
    [AdminProfileSettingsController::class, 'updateAdmin']
)->name('admin.profile-settings.admin.update');

Route::delete(
    '/admin/profile-settings/admin/{admin}',
    [AdminProfileSettingsController::class, 'deleteAdmin']
)->name('admin.profile-settings.admin.delete');


    /*
    |--------------------------------------------------------------------------
    | Admin Overdue
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/overdue',
        [OverdueController::class, 'index']
    )->name('admin.overdue');


    /*
    |--------------------------------------------------------------------------
    | Admin Support
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/support',
        [AdminSupportController::class, 'index']
    )->name('admin.support.index');

    Route::get(
        '/admin/support/{ticket}',
        [AdminSupportController::class, 'show']
    )->name('admin.support.show');

    Route::post(
        '/admin/support/{ticket}/message',
        [AdminSupportController::class, 'message']
    )->name('admin.support.message');

    Route::patch(
        '/admin/support/{ticket}/status',
        [AdminSupportController::class, 'status']
    )->name('admin.support.status');


    /*
    |--------------------------------------------------------------------------
    | Admin Test
    |--------------------------------------------------------------------------
    */

    Route::get(
        '/admin/test',
        function () {
            return 'Admin access working!';
        }
    )->name('admin.test');

});

use Illuminate\Support\Facades\DB;

Route::get('/time-check', function () {
    return response()->json([
        'php_time' => date('Y-m-d H:i:s'),
        'php_timezone' => date_default_timezone_get(),

        'laravel_time' => now()->format('Y-m-d H:i:s'),
        'laravel_timezone' => config('app.timezone'),

        'db_time' => DB::selectOne('SELECT NOW() as now')->now,
    ]);
});


/*
|--------------------------------------------------------------------------
| Authentication Routes
|--------------------------------------------------------------------------
*/

require __DIR__ . '/auth.php';