<?php

use App\Http\Controllers\Accounting\CustomerBalanceController;
use App\Http\Controllers\Accounting\ExternalPaymentVoucherController;
use App\Http\Controllers\Accounting\PaymentVoucherController;
use App\Http\Controllers\Accounting\ReceiptVoucherController;
use App\Http\Controllers\Customers\AttendanceReportController;
use App\Http\Controllers\Customers\CheckInController;
use App\Http\Controllers\Customers\CustomersController;
use App\Http\Controllers\DynamicData\{DynamicDataController, DynamicSelectController};
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LocaleController;
use App\Http\Controllers\Plans\PlansController;
use App\Http\Controllers\Reports\ExpensesReportController;
use App\Http\Controllers\Reports\PlansReportController;
use App\Http\Controllers\Reports\RefundsReportController;
use App\Http\Controllers\Reports\RevenueReportController;
use App\Http\Controllers\Settings\DataSettings\{PaymentMethodsController};
use App\Http\Controllers\Settings\General\SettingsController;
use App\Http\Controllers\Settings\UsersManagements\{UsersManagements\RoleController,
    UsersManagements\UserController,
    UsersManagements\UserProfileController};
use App\Http\Controllers\Subscriptions\SubscriptionsController;
use App\Http\Controllers\Subscriptions\SubscriptionTransferController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Auth::routes([
    'login'    => true,
    'logout'   => true,
    'register' => false,
    'reset'    => false,
    'confirm'  => false,
    'verify'   => false,
]);

/* || ================== MIDDLEWARE WEB ===================== || */
Route::middleware(['web'])->group(function () {

    define("LOOK_COUNT", 5);
    define("LIMIT_SELECT2", 20);
    define("LOCALIZE_NAME", app()->getLocale() === 'ar' ? 'name_ar' : 'name_en');

    Route::prefix('language')->name('language.')->controller(LocaleController::class)->group(function () {
        Route::get('switch/{locale}', 'switch')->name('switch');
    });

    Route::prefix('dashboard')->name('dashboard.')->controller(HomeController::class)->group(function () {
        Route::get('/', 'index')->name('index');
    });

});

//  php artisan translation:sync-missing-translation

/* || ================== MIDDLEWARE AUTH ===================== || */
Route::middleware(['auth', 'verified'])->group(function () {

    /* || ================== BEGIN - DYNAMIC SELECT2 ===================== || */
    Route::prefix('ajax')->name('ajax.')->controller(DynamicSelectController::class)->group(function () {
        Route::get('/get-users','getUsersAjax')->name('get-users');
        Route::get('/get-coaches','getCoachesAjax')->name('get-coaches');
        Route::get('/get-roles','getRolesAjax')->name('get-roles');
        Route::get('/get-nationalities','getNationalitiesAjax')->name('get-nationalities');
        Route::get('/get-countries-codes','getCountriesCodesAjax')->name('get-countries-codes');
        Route::get('/get-customers','getCustomersAjax')->name('get-customers');
        Route::get('/get-subscriptions','getSubscriptionsAjax')->name('get-subscriptions');
        Route::get('/get-plans','getPlansAjax')->name('get-plans');
        Route::get('/get-banks','getBanksAjax')->name('get-banks');
        Route::get('/get-payment-methods','getPaymentMethodsAjax')->name('get-payment-methods');
        Route::get('/get-regions','getRegionsAjax')->name('get-regions');
        Route::get('/get-cities','getCitiesAjax')->name('get-cities');
    });

    /* || ================== BEGIN - DYNAMIC SELECT2 ===================== || */
    Route::prefix('data')->name('data.')->controller(DynamicDataController::class)->group(function () {
        Route::get('/get-payment-methods','getPaymentMethodsData')->name('get-payment-methods');
        Route::get('/get-plans','getPlansData')->name('get-plans');
        Route::get('/get-customers','getCustomersData')->name('get-customers');
        Route::get('/get-subscriptions/{id}','getSubscriptionsData')->name('get-subscriptions');
        Route::get('/get-receipt-vouchers/{id}','getReceiptVouchersData')->name('get-receipt-vouchers');
        Route::get('/get-payment-vouchers/{id}','getPaymentVouchersData')->name('get-payment-vouchers');
    });

    /* || ================== BEGIN - PLANS ===================== || */
    Route::prefix('plans')->name('plans.')->controller(PlansController::class)->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('/yajra', 'yajra')->name('yajra');
        Route::get('/statics', 'statics')->name('statics');
        Route::get('/create', 'create')->name('create');
        Route::get('/edit/{id}', 'edit')->name('edit');

        Route::post('/store', 'store')->name('store');
        Route::post('/update/{id}', 'update')->name('update');

        Route::patch('/update-status/{id}', 'updateStatus')->name('update-status');
        Route::patch('/update-status-selector', 'updateStatusSelector')->name('update-status-selector');

        Route::delete('/destroy/{id}', 'destroy')->name('destroy');
        Route::delete('/destroy-selector', 'destroySelector')->name('destroy-selector');
    });

    /* || ================== BEGIN - CUSTOMERS ===================== || */
    Route::prefix('customers')->name('customers.')->controller(CustomersController::class)->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('/pending', 'pending')->name('pending');
        Route::get('/yajra', 'yajra')->name('yajra');
        Route::get('/statics', 'statics')->name('statics');
        Route::get('/create', 'create')->name('create');
        Route::get('/edit/{id}', 'edit')->name('edit');
        Route::get('/show/{id}', 'show')->name('show');

        Route::post('/store', 'store')->name('store');
        Route::post('/update/{id}', 'update')->name('update');
        Route::post('/toggle-status/{id}', 'toggleStatus')->name('toggle-status');
        Route::post('/add-balance/{id}', 'addBalance')->name('add-balance');

        Route::patch('/update-status/{id}', 'updateStatus')->name('update-status');
        Route::patch('/update-status-selector', 'updateStatusSelector')->name('update-status-selector');

        Route::delete('/destroy/{id}', 'destroy')->name('destroy');
        Route::delete('/destroy-selector', 'destroySelector')->name('destroy-selector');

    });

    /* || ================== BEGIN - ATTENDANCE ===================== || */
    Route::prefix('attendance')->name('attendance.')->group(function () {
        Route::prefix('check-in')->name('check-in.')->controller(CheckInController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/search', 'search')->name('search');
            Route::post('/check-in', 'checkIn')->name('check-in');
            Route::post('/check-out', 'checkOut')->name('check-out');
            Route::post('/bulk-checkout', 'bulkCheckOut')->name('bulk-checkout');
            Route::get('/current', 'getCurrent')->name('current');
            Route::get('/customer-card/{customer}', 'customerCard')->name('customer-card');
            Route::get('/customer/{customer}', 'customer')->name('customer');
        });

        // Attendance Reports
        Route::prefix('reports')->name('reports.')->controller(AttendanceReportController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/filter', 'filter')->name('filter');
            Route::get('/daily', 'daily')->name('daily');
            Route::get('/monthly', 'monthly')->name('monthly');
            Route::get('/export', 'export')->name('export');
        });
    });

    /* || ================== BEGIN - PLANS ===================== || */
    Route::prefix('subscriptions')->name('subscriptions.')->controller(SubscriptionsController::class)->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('/yajra', 'yajra')->name('yajra');
        Route::get('/statics', 'statics')->name('statics');
        Route::get('/create', 'create')->name('create');
        Route::get('/edit/{id}', 'edit')->name('edit');
        Route::get('/print/{id}', 'print')->name('print');

        Route::post('/store', 'store')->name('store');
        Route::post('/update/{id}', 'update')->name('update');
        Route::post('/cancel', 'cancel')->name('cancel');
        Route::post('/renew', 'renew')->name('renew');

        Route::patch('/update-status/{id}', 'updateStatus')->name('update-status');
        Route::patch('/update-status-selector', 'updateStatusSelector')->name('update-status-selector');

        Route::delete('/destroy/{id}', 'destroy')->name('destroy');
        Route::delete('/destroy-selector', 'destroySelector')->name('destroy-selector');

        // Subscription Transfer Routes
        Route::prefix('transfer')->name('transfer.')->controller(SubscriptionTransferController::class)->group(function () {
            Route::get('/transfer-history/{id}', 'history')->name('subscriptions.transfer.history');

            Route::post('/store', 'store')->name('store');
            Route::post('/rollback/{id}', 'rollback')->name('subscription-transfers.rollback');
        });

    });

    /* || ================== BEGIN - ACCOUNTING ===================== || */
    Route::prefix('accounting')->name('accounting.')->group(function () {
        // Payment Vouchers (سندات الصرف)
        Route::prefix('external-payment-vouchers')->name('external-payment-vouchers.')->controller(ExternalPaymentVoucherController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/yajra', 'yajra')->name('yajra');
        });

        Route::prefix('payment-vouchers')->name('payment-vouchers.')->controller(PaymentVoucherController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/yajra', 'yajra')->name('yajra');
            Route::get('/create', 'create')->name('create');
            Route::post('/', 'store')->name('store');
            Route::get('/external', 'externalExpenses')->name('external');
            Route::get('/{id}', 'show')->name('show');
            Route::get('/{id}/edit', 'edit')->name('edit');
            Route::put('/{id}', 'update')->name('update');
            Route::post('/cancel', 'cancel')->name('cancel');
            Route::post('/{id}/approve', 'approve')->name('approve');
            Route::get('/{id}/print', 'print')->name('print');
            Route::delete('/attachments/{attachmentId}', 'deleteAttachment')->name('delete-attachment');
        });

        // Receipt Vouchers (سندات القبض)
        Route::prefix('receipt-vouchers')->name('receipt-vouchers.')->controller(ReceiptVoucherController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/yajra', 'yajra')->name('yajra');
            Route::get('/create', 'create')->name('create');
            Route::post('/store', 'store')->name('store');
            Route::get('/show/{id}', 'show')->name('show');
            Route::get('/{id}/edit', 'edit')->name('edit');
            Route::put('/update/{id}', 'update')->name('update');
            Route::post('/refund', 'refund')->name('refund');
            Route::post('/cancel', 'cancel')->name('cancel');
            Route::post('/{id}/approve', 'approve')->name('approve');
            Route::get('/{id}/print', 'print')->name('print');
            Route::delete('/attachments/{attachmentId}', 'deleteAttachment')->name('delete-attachment');
        });

        // Customer Balances (أرصدة العملاء)
        Route::prefix('customers-balance')->name('customers-balance.')->controller(CustomerBalanceController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/yajra', 'yajra')->name('yajra');
            Route::get('/summary', 'summary')->name('summary');
            Route::get('/export', 'export')->name('export');
            Route::get('/{customerId}', 'show')->name('show');
            Route::get('/{customerId}/transactions', 'transactions')->name('transactions');
            Route::post('/{customerId}/adjust', 'adjust')->name('adjust');
        });

    });

    /* || ================== BEGIN - REPORTS ===================== || */
    Route::prefix('reports')->name('reports.')->group(function () {
        Route::prefix('revenues')->name('revenues.')->controller(RevenueReportController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::post('/filter', 'filter')->name('filter');

        });
        Route::prefix('expenses')->name('expenses.')->controller(ExpensesReportController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::post('/filter', 'filter')->name('filter');

        });
        Route::prefix('refunds')->name('refunds.')->controller(RefundsReportController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::post('/filter', 'filter')->name('filter');

        });
        Route::prefix('plans')->name('plans.')->controller(PlansReportController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::get('/details', 'details')->name('details');
            Route::post('/filter', 'filter')->name('filter');
        });

    });

    /* || ================== BEGIN - SETTINGS ===================== || */
    Route::prefix('settings')->name('settings.')->group(function () {

        Route::controller(SettingsController::class)->group(function () {
            Route::get('/', 'index')->name('index');
            Route::post('/update/{group}', 'update')->name('update');
            Route::get('/import', 'import')->name('import');
            Route::post('/import', 'import')->name('import.post');
            Route::get('/export', 'export')->name('export');
            Route::get('/reset', 'reset')->name('reset');
        });

        Route::prefix('users-management')->name('users-management.')->group(function () {

            /* || ================== BEGIN - USERS ===================== || */
            Route::prefix('users')->name('users.')->controller(UserController::class)->group(function () {
                Route::get('/', 'index')->name('index');
                Route::get('/yajra', 'yajra')->name('yajra');
                Route::get('/statics', 'statics')->name('statics');
                Route::get('/edit/{id}', 'edit')->name('edit');

                Route::post('/store', 'store')->name('store');
                Route::post('/update/{id}', 'update')->name('update');

                Route::patch('/update-status/{id}', 'updateStatus')->name('update-status');
                Route::patch('/update-status-selector', 'updateStatusSelector')->name('update-status-selector');

                Route::delete('/destroy/{id}', 'destroy')->name('destroy');
                Route::delete('/destroy-selector', 'destroySelector')->name('destroy-selector');

                Route::prefix('profile')->name('profile.')->controller(UserProfileController::class)->group(function () {
                    Route::get('/', 'index')->name('index');
                });

            });

            /* || ================== BEGIN - ROLES ===================== || */
            Route::prefix('roles')->name('roles.')->controller(RoleController::class)->group(function () {
                Route::get('/', 'index')->name('index');

                Route::get('/create', 'create')->name('create');
                Route::get('/edit/{id}', 'edit')->name('edit');

                Route::post('/store', 'store')->name('store');
                Route::post('/update/{id}', 'update')->name('update');
                Route::post('/duplicate/{id}', 'duplicate')->name('duplicate');

                Route::post('/destroy/{id}', 'destroy')->name('destroy');
            });

        });

        Route::prefix('data-settings')->name('data-settings.')->group(function () {

            /* || ================== BEGIN - ROLES ===================== || */
            Route::prefix('payment-methods')->name('payment-methods.')->controller(PaymentMethodsController::class)->group(function () {
                Route::get('/', 'index')->name('index');
                Route::get('/yajra', 'yajra')->name('yajra');
                Route::get('/edit/{id}', 'edit')->name('edit');

                Route::post('/store/{id}', 'store')->name('store');
                Route::post('/update/{id}', 'update')->name('update');

                Route::patch('/update-status/{id}', 'updateStatus')->name('update-status');
                Route::patch('/update-status-selector', 'updateStatusSelector')->name('update-status-selector');

                Route::delete('/destroy/{id}', 'destroy')->name('destroy');
                Route::delete('/destroy-selector', 'destroySelector')->name('destroy-selector');

            });




        });


    });

});


