<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\User\AuthController;
use App\Http\Controllers\User\ProfileController;
use App\Http\Controllers\User\DashboardController;
use App\Http\Controllers\User\SubscriptionController;

use App\Http\Controllers\User\UserAccountController;
use App\Http\Controllers\User\AccountTransferController;
use App\Http\Controllers\User\AccountLedgerController;

use App\Http\Controllers\User\CustomerController;
use App\Http\Controllers\User\CustomerLedgerController;

use App\Http\Controllers\User\SupplierController;
use App\Http\Controllers\User\SupplierLedgerController;

use App\Http\Controllers\User\ExpenseCategoryController;
use App\Http\Controllers\User\ExpenseController;

use App\Http\Controllers\User\GeneralLedgerController;
use App\Http\Controllers\User\BalanceSheetController;
use App\Http\Controllers\User\ProfitLossController;

use App\Http\Controllers\User\OwnerTransactionController;

use App\Http\Controllers\User\LoanController;
use App\Http\Controllers\User\LoanPaymentController;
use App\Http\Controllers\User\LoanGeneralLedgerController;

/*
|--------------------------------------------------------------------------
| HR Controllers
|--------------------------------------------------------------------------
*/
use App\Http\Controllers\User\HrDashboardController;
use App\Http\Controllers\User\DepartmentController;
use App\Http\Controllers\User\DesignationController;
use App\Http\Controllers\User\ShiftController;
use App\Http\Controllers\User\EmployeeController;
use App\Http\Controllers\User\EmployeeSalaryController;
use App\Http\Controllers\User\EmployeeAdvanceController;
use App\Http\Controllers\User\PayrollController;
use App\Http\Controllers\User\PayrollPaymentController;
use App\Http\Controllers\User\EmployeeAttendanceController;
use App\Http\Controllers\User\EmployeeLedgerController;
use App\Http\Controllers\User\HrPdfController;
use App\Http\Controllers\User\EmployeeBonusController;

/*
|--------------------------------------------------------------------------
| Guest Routes
|--------------------------------------------------------------------------
*/
Route::prefix('user')
    ->name('user.')
    ->middleware('guest:web')
    ->group(function () {

        Route::get('/register', [AuthController::class, 'showRegistrationForm'])->name('register');
        Route::post('/register', [AuthController::class, 'register'])->name('register.submit');

        Route::get('/login', [AuthController::class, 'showLoginForm'])->name('login');
        Route::post('/login', [AuthController::class, 'login'])->name('login.submit');

        Route::get('/check-username', [AuthController::class, 'checkUsername'])->name('username.check');
    });

/*
|--------------------------------------------------------------------------
| Authenticated Routes
|--------------------------------------------------------------------------
*/
Route::prefix('user')
    ->name('user.')
    ->middleware('auth:web')
    ->group(function () {

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

        /*
        |--------------------------------------------------------------------------
        | Subscription
        |--------------------------------------------------------------------------
        */
        Route::prefix('subscriptions')->name('subscriptions.')->group(function () {
            Route::get('/', [SubscriptionController::class, 'index'])->name('index');
            Route::get('/history', [SubscriptionController::class, 'history'])->name('history');
            Route::post('/select', [SubscriptionController::class, 'selectPlan'])->name('select');
            Route::get('/methods', [SubscriptionController::class, 'methods'])->name('methods');
            Route::get('/payment/{method}', [SubscriptionController::class, 'paymentPage'])->name('payment');
            Route::post('/submit', [SubscriptionController::class, 'submit'])->name('submit');
            Route::get('/processing/{subscriptionRequest}', [SubscriptionController::class, 'processing'])->name('processing');
            Route::get('/check-status/{subscriptionRequest}', [SubscriptionController::class, 'checkStatus'])->name('check-status');
        });

        Route::get('/subscription-status', [SubscriptionController::class, 'subscriptionStatus'])
            ->name('subscription-status');

        /*
        |--------------------------------------------------------------------------
        | Routes Requiring Active Subscription
        |--------------------------------------------------------------------------
        */
        Route::middleware('subscriptions')->group(function () {

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

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

            /*
            |--------------------------------------------------------------------------
            | Accounts
            |--------------------------------------------------------------------------
            */
            Route::resource('accounts', UserAccountController::class);

            Route::get('account-transfers', [AccountTransferController::class, 'index'])->name('account_transfers.index');
            Route::get('account-transfers/create', [AccountTransferController::class, 'create'])->name('account_transfers.create');
            Route::post('account-transfers', [AccountTransferController::class, 'store'])->name('account_transfers.store');
            Route::delete('account-transfers/{transfer}', [AccountTransferController::class, 'destroy'])->name('account_transfers.destroy');

            Route::get('accounts/{account}/ledger', [AccountLedgerController::class, 'index'])->name('accounts.ledger.index');
            Route::get('accounts/{account}/ledger/create', [AccountLedgerController::class, 'create'])->name('accounts.ledger.create');
            Route::post('accounts/{account}/ledger', [AccountLedgerController::class, 'store'])->name('accounts.ledger.store');
            Route::delete('accounts/{account}/ledger/{ledger}', [AccountLedgerController::class, 'destroy'])->name('accounts.ledger.destroy');

            /*
            |--------------------------------------------------------------------------
            | Customers
            |--------------------------------------------------------------------------
            */
            Route::resource('customers', CustomerController::class);

            Route::prefix('customers/{customer}/ledger')
                ->name('customers.ledger.')
                ->group(function () {
                    Route::get('/', [CustomerLedgerController::class, 'index'])->name('index');

                    Route::get('/bill/create', [CustomerLedgerController::class, 'createBill'])->name('bill.create');
                    Route::post('/bill/store', [CustomerLedgerController::class, 'storeBill'])->name('bill.store');

                    Route::get('/payment/create', [CustomerLedgerController::class, 'createPayment'])->name('payment.create');
                    Route::post('/payment/store', [CustomerLedgerController::class, 'storePayment'])->name('payment.store');

                    Route::get('/adjust/create', [CustomerLedgerController::class, 'createAdjust'])->name('adjust.create');
                    Route::post('/adjust/store', [CustomerLedgerController::class, 'storeAdjust'])->name('adjust.store');

                    Route::get('/{trx}/edit', [CustomerLedgerController::class, 'edit'])->name('edit');
                    Route::put('/{trx}', [CustomerLedgerController::class, 'update'])->name('update');
                    Route::delete('/{trx}', [CustomerLedgerController::class, 'destroy'])->name('destroy');
                });

            /*
            |--------------------------------------------------------------------------
            | Suppliers
            |--------------------------------------------------------------------------
            */
            Route::resource('suppliers', SupplierController::class);

            Route::prefix('suppliers/{supplier}/ledger')
                ->name('suppliers.ledger.')
                ->group(function () {
                    Route::get('/', [SupplierLedgerController::class, 'index'])->name('index');

                    Route::get('/purchase/create', [SupplierLedgerController::class, 'createPurchase'])->name('purchase.create');
                    Route::post('/purchase/store', [SupplierLedgerController::class, 'storePurchase'])->name('purchase.store');

                    Route::get('/payment/create', [SupplierLedgerController::class, 'createPayment'])->name('payment.create');
                    Route::post('/payment/store', [SupplierLedgerController::class, 'storePayment'])->name('payment.store');

                    Route::get('/adjust/create', [SupplierLedgerController::class, 'createAdjust'])->name('adjust.create');
                    Route::post('/adjust/store', [SupplierLedgerController::class, 'storeAdjust'])->name('adjust.store');

                    Route::get('/{trx}/edit', [SupplierLedgerController::class, 'edit'])->name('edit');
                    Route::put('/{trx}', [SupplierLedgerController::class, 'update'])->name('update');
                    Route::delete('/{trx}', [SupplierLedgerController::class, 'destroy'])->name('destroy');
                });

            /*
            |--------------------------------------------------------------------------
            | Owner Transactions
            |--------------------------------------------------------------------------
            */
            Route::resource('owner-transactions', OwnerTransactionController::class)
                ->names('owner_transactions')
                ->except(['show']);

            /*
            |--------------------------------------------------------------------------
            | Expenses
            |--------------------------------------------------------------------------
            */
            Route::resource('expense_categories', ExpenseCategoryController::class);
            Route::resource('expenses', ExpenseController::class)->except(['show']);

            /*
            |--------------------------------------------------------------------------
            | Reports
            |--------------------------------------------------------------------------
            */
            Route::get('/reports/general-ledger', [GeneralLedgerController::class, 'index'])->name('reports.general-ledger');
            Route::get('/reports/profit-loss', [ProfitLossController::class, 'index'])->name('reports.profit-loss');
            Route::get('/reports/profit-loss/pdf', [ProfitLossController::class, 'downloadPdf'])->name('reports.profit-loss.pdf');
            Route::get('/reports/balance-sheet', [BalanceSheetController::class, 'index'])->name('reports.balance-sheet');
            Route::get('/reports/balance-sheet/pdf', [BalanceSheetController::class, 'downloadPdf'])->name('reports.balance-sheet.pdf');

            /*
            |--------------------------------------------------------------------------
            | Loans
            |--------------------------------------------------------------------------
            */
            Route::get('/loans', [LoanController::class, 'index'])->name('loans.index');
            Route::get('/loans/create', [LoanController::class, 'create'])->name('loans.create');
            Route::post('/loans', [LoanController::class, 'store'])->name('loans.store');
            Route::get('/loans/{loan}', [LoanController::class, 'show'])->name('loans.show');
            Route::get('/loans/{loan}/edit', [LoanController::class, 'edit'])->name('loans.edit');
            Route::put('/loans/{loan}', [LoanController::class, 'update'])->name('loans.update');
            Route::get('/loans/{loan}/statement', [LoanController::class, 'statement'])->name('loans.statement');
            Route::post('/loans/{loan}/close', [LoanController::class, 'close'])->name('loans.close');
            Route::delete('/loans/{loan}', [LoanController::class, 'destroy'])->name('loans.destroy');

            Route::get('/loans/{loan}/payments/create', [LoanPaymentController::class, 'create'])->name('loans.payments.create');
            Route::post('/loans/{loan}/payments', [LoanPaymentController::class, 'store'])->name('loans.payments.store');
            Route::delete('/loans/{loan}/payments/{payment}', [LoanPaymentController::class, 'destroy'])->name('loans.payments.destroy');

            Route::get('/reports/loan-general-ledger', [LoanGeneralLedgerController::class, 'index'])->name('reports.loan-general-ledger');

            /*
            |--------------------------------------------------------------------------
            | HR Module
            |--------------------------------------------------------------------------
            */
            Route::prefix('hr')->name('hr.')->group(function () {

                /*
                |--------------------------------------------------------------------------
                | HR Dashboard
                |--------------------------------------------------------------------------
                */
                Route::get('/dashboard', [HrDashboardController::class, 'index'])->name('dashboard');

                /*
                |--------------------------------------------------------------------------
                | Master Setup
                |--------------------------------------------------------------------------
                */
                Route::resource('departments', DepartmentController::class)
                    ->names('departments')
                    ->except(['show']);

                Route::resource('designations', DesignationController::class)
                    ->names('designations')
                    ->except(['show']);

                Route::resource('shifts', ShiftController::class)
                    ->names('shifts')
                    ->except(['show']);

                /*
                |--------------------------------------------------------------------------
                | Employees
                |--------------------------------------------------------------------------
                */
                Route::resource('employees', EmployeeController::class)
                    ->names('employees');

                /*
                |--------------------------------------------------------------------------
                | Employee Salary Setup
                |--------------------------------------------------------------------------
                */
                Route::prefix('salary')->name('salary.')->group(function () {
                    Route::get('/', [EmployeeSalaryController::class, 'index'])->name('index');
                    Route::get('/{employee}/create', [EmployeeSalaryController::class, 'create'])->name('create');
                    Route::post('/{employee}', [EmployeeSalaryController::class, 'store'])->name('store');
                    Route::get('/{employee}/edit', [EmployeeSalaryController::class, 'edit'])->name('edit');
                    Route::put('/{employee}', [EmployeeSalaryController::class, 'update'])->name('update');
                    Route::get('/{employee}/history', [EmployeeSalaryController::class, 'history'])->name('history');
                });

                /*
                |--------------------------------------------------------------------------
                | Employee Advances
                |--------------------------------------------------------------------------
                */
                Route::resource('advances', EmployeeAdvanceController::class)
                    ->names('advances')
                    ->except(['show']);

                Route::prefix('advances')->name('advances.')->group(function () {
                    Route::get('{advance}/repayments', [EmployeeAdvanceController::class, 'repaymentIndex'])->name('repayments.index');
                    Route::get('{advance}/repayment/create', [EmployeeAdvanceController::class, 'repaymentCreate'])->name('repayments.create');
                    Route::post('{advance}/repayment/store', [EmployeeAdvanceController::class, 'repaymentStore'])->name('repayments.store');

                    Route::get('{advance}/repayment/{repayment}/edit', [EmployeeAdvanceController::class, 'repaymentEdit'])->name('repayments.edit');
                    Route::put('{advance}/repayment/{repayment}', [EmployeeAdvanceController::class, 'repaymentUpdate'])->name('repayments.update');
                    Route::delete('{advance}/repayment/{repayment}', [EmployeeAdvanceController::class, 'repaymentDestroy'])->name('repayments.destroy');
                });

                /*
                |--------------------------------------------------------------------------
                | Employee Bonuses
                |--------------------------------------------------------------------------
                */
                Route::prefix('bonuses')->name('bonuses.')->group(function () {
                    Route::get('/', [EmployeeBonusController::class, 'index'])->name('index');
                    Route::get('/create', [EmployeeBonusController::class, 'create'])->name('create');
                    Route::post('/', [EmployeeBonusController::class, 'store'])->name('store');

                    Route::get('/report', [EmployeeBonusController::class, 'report'])->name('report');

                    Route::get('/{id}', [EmployeeBonusController::class, 'show'])->name('show');
                    Route::get('/{id}/edit', [EmployeeBonusController::class, 'edit'])->name('edit');
                    Route::put('/{id}', [EmployeeBonusController::class, 'update'])->name('update');

                    Route::get('/{id}/pay', [EmployeeBonusController::class, 'payForm'])->name('pay-form');
                    Route::post('/{id}/pay', [EmployeeBonusController::class, 'pay'])->name('pay');

                    Route::delete('/{id}', [EmployeeBonusController::class, 'destroy'])->name('destroy');
                });

                /*
                |--------------------------------------------------------------------------
                | Payroll
                |--------------------------------------------------------------------------
                */
                Route::get('payrolls', [PayrollController::class, 'index'])->name('payrolls.index');
                Route::get('payrolls/create', [PayrollController::class, 'create'])->name('payrolls.create');
                Route::post('payrolls/preview', [PayrollController::class, 'preview'])->name('payrolls.preview');
                Route::post('payrolls', [PayrollController::class, 'store'])->name('payrolls.store');
                Route::get('payrolls/{payroll}', [PayrollController::class, 'show'])->name('payrolls.show');
                Route::delete('payrolls/{payroll}', [PayrollController::class, 'destroy'])->name('payrolls.destroy');

                Route::post('payrolls/{payroll}/finalize', [PayrollController::class, 'finalize'])
                    ->name('payrolls.finalize');

                /*
                |--------------------------------------------------------------------------
                | Payroll Payments
                |--------------------------------------------------------------------------
                */
                Route::get('payrolls/{payroll}/payments/{item}/create', [PayrollPaymentController::class, 'create'])
                    ->name('payroll-payments.create');

                Route::post('payrolls/{payroll}/payments/{item}', [PayrollPaymentController::class, 'store'])
                    ->name('payroll-payments.store');

                Route::get('payrolls/{payroll}/payments/{payment}/edit', [PayrollPaymentController::class, 'edit'])
                    ->name('payroll-payments.edit');

                Route::put('payrolls/{payroll}/payments/{payment}', [PayrollPaymentController::class, 'update'])
                    ->name('payroll-payments.update');

                Route::delete('payroll-payments/{payment}', [PayrollPaymentController::class, 'destroy'])
                    ->name('payroll-payments.destroy');

                Route::post('payrolls/check-finalized', [PayrollController::class, 'checkFinalized'])
                    ->name('payrolls.check-finalized');

                /*
                |--------------------------------------------------------------------------
                | Attendance
                |--------------------------------------------------------------------------
                */
                Route::prefix('attendance')->name('attendance.')->group(function () {
                    Route::get('/', [EmployeeAttendanceController::class, 'index'])->name('index');
                    Route::get('/daily', [EmployeeAttendanceController::class, 'daily'])->name('daily');
                    Route::post('/daily', [EmployeeAttendanceController::class, 'saveDaily'])->name('save-daily');

                    Route::get('/bulk-monthly', [EmployeeAttendanceController::class, 'bulkMonthly'])->name('bulk-monthly');
                    Route::post('/bulk-monthly', [EmployeeAttendanceController::class, 'saveBulkMonthly'])->name('save-bulk-monthly');

                    Route::get('/bulk-daily-roz', [EmployeeAttendanceController::class, 'bulkDailyRoz'])->name('bulk-daily-roz');
                    Route::post('/bulk-daily-roz', [EmployeeAttendanceController::class, 'saveBulkDailyRoz'])->name('save-bulk-daily-roz');

                    Route::get('/sheet', [EmployeeAttendanceController::class, 'sheetIndex'])->name('sheet.index');
                    Route::get('/sheet/{id}', [EmployeeAttendanceController::class, 'sheetShow'])->name('sheet.show');
                    Route::get('/sheet/{id}/print', [EmployeeAttendanceController::class, 'printSheet'])->name('sheet.print');

                    Route::delete('/{attendance}', [EmployeeAttendanceController::class, 'destroy'])->name('destroy');
                });

                /*
                |--------------------------------------------------------------------------
                | Employee Ledger
                |--------------------------------------------------------------------------
                */
                Route::prefix('ledger')->name('ledger.')->group(function () {
                    Route::get('/', [EmployeeLedgerController::class, 'index'])->name('index');
                    Route::get('/employee/{employee}', [EmployeeLedgerController::class, 'show'])->name('show');
                });

                /*
                |--------------------------------------------------------------------------
                | HR Reports
                |--------------------------------------------------------------------------
                */
                Route::prefix('reports')->name('reports.')->group(function () {
                    Route::get('/advances', [EmployeeLedgerController::class, 'advances'])->name('advances');
                    Route::get('/payroll-summary', [EmployeeLedgerController::class, 'payrollSummary'])->name('payroll-summary');
                });

                /*
                |--------------------------------------------------------------------------
                | HR PDF
                |--------------------------------------------------------------------------
                */
                Route::prefix('pdf')->name('pdf.')->group(function () {
                    Route::get('/payslip/{payroll}/{item}', [HrPdfController::class, 'payslip'])->name('payslip');
                    Route::get('/employee-ledger/{employee}', [HrPdfController::class, 'employeeLedger'])->name('employee-ledger');
                    Route::get('/payroll-summary', [HrPdfController::class, 'payrollSummary'])->name('payroll-summary');
                    Route::get('/advance-report', [HrPdfController::class, 'advanceReport'])->name('advance-report');
                    Route::get('/payroll-sheet/{payroll}', [HrPdfController::class, 'payrollSheet'])->name('payroll-sheet');
                });
            });
        });
    });