<?php

use App\Http\Controllers\Admin\Auth\LoginController;
use App\Http\Controllers\Admin\Dashboard\AnalyticController;
use App\Http\Controllers\Admin\Dashboard\OverviewController;
use App\Http\Controllers\Admin\Finance\CashController;
use App\Http\Controllers\Admin\Finance\DebtController;
use App\Http\Controllers\Admin\Finance\ExpenseController;
use App\Http\Controllers\Admin\Finance\PayrollController;
use App\Http\Controllers\Admin\Manage\AttendanceController;
use App\Http\Controllers\Admin\Manage\InventoryController;
use App\Http\Controllers\Admin\Manage\TransactionController;
use App\Http\Controllers\Admin\Master\BarbershopController;
use App\Http\Controllers\Admin\Master\CustomerController;
use App\Http\Controllers\Admin\Master\ServiceController;
use App\Http\Controllers\Admin\Master\UserController;
use App\Http\Controllers\Admin\Setting\AccountController;
use App\Http\Controllers\Admin\Setting\ApplicationController;
use App\Http\Controllers\Client\HomepageController;
use Illuminate\Support\Facades\Route;

Route::prefix('auth')->group(function () {
    Route::get('login', [LoginController::class, 'index'])->name('login');
    Route::post('login', [LoginController::class, 'auth'])->name('auth.login');
    Route::post('logout', [LoginController::class, 'logout'])->name('auth.logout');
});

Route::group([
    'middleware' => 'auth',
    'prefix' => 'dashboard',
], function () {
    Route::get('overview', [OverviewController::class, 'index'])->name('dashboard.overview');
    Route::get('analytic', [AnalyticController::class, 'index'])->name('dashboard.analytic')->middleware('check_role:1,2,3');
    Route::get('analytic/filter', [AnalyticController::class, 'filter'])->name('dashboard.analytic.filter')->middleware('check_role:1,2,3');

    Route::get('change-barbershop/{barbershop}', [OverviewController::class, 'changeBarbershop'])->name('dashboard.changeBarbershop');

    Route::middleware(['check_role:1,2,3'])->group(function () {
        Route::prefix('master')->group(function () {
            Route::prefix('barbershop')->group(function () {
                Route::get('/', [BarbershopController::class, 'index'])->name('master.barbershop');
                Route::middleware(['check_role:1,3'])->group(function () {
                    Route::get('create', [BarbershopController::class, 'create'])->name('master.barbershop.create');
                    Route::post('store', [BarbershopController::class, 'store'])->name('master.barbershop.store');
                    Route::get('edit/{barbershop}', [BarbershopController::class, 'edit'])->name('master.barbershop.edit');
                    Route::put('update/{barbershop}', [BarbershopController::class, 'update'])->name('master.barbershop.update');
                });
                Route::delete('delete/{barbershop}', [BarbershopController::class, 'delete'])->name('master.barbershop.delete')->middleware('check_role:1');
            });

            Route::get('/users', [UserController::class, 'index'])->name('master.users');
            Route::prefix('user')->group(function () {
                Route::middleware(['check_role:1,2'])->group(function () {
                    Route::get('create', [UserController::class, 'create'])->name('master.user.create');
                    Route::post('store', [UserController::class, 'store'])->name('master.user.store');
                    Route::get('edit/{user}', [UserController::class, 'edit'])->name('master.user.edit');
                    Route::put('update/{user}', [UserController::class, 'update'])->name('master.user.update');
                    Route::put('reset-password/{user}', [UserController::class, 'resetPassword'])->name('master.user.resetPassword');
                    Route::put('activation/{user}', [UserController::class, 'activation'])->name('master.user.activation');
                    Route::delete('delete/{user}', [UserController::class, 'delete'])->name('master.user.delete');
                });
            });

            Route::get('/customers', [CustomerController::class, 'index'])->name('master.customers');
            Route::prefix('customer')->group(function () {
                Route::middleware(['check_role:1,3'])->group(function () {
                    Route::post('store', [CustomerController::class, 'store'])->name('master.customer.store');
                    Route::put('update/{customer}', [CustomerController::class, 'update'])->name('master.customer.update');
                });
                Route::delete('delete/{customer}', [CustomerController::class, 'delete'])->name('master.customer.delete')->middleware('check_role:1');
            });

            Route::get('/services', [ServiceController::class, 'index'])->name('master.services');
            Route::prefix('service')->group(function () {
                Route::middleware(['check_role:1,3'])->group(function () {
                    Route::get('create', [ServiceController::class, 'create'])->name('master.service.create');
                    Route::post('store', [ServiceController::class, 'store'])->name('master.service.store');
                    Route::get('edit/{service}', [ServiceController::class, 'edit'])->name('master.service.edit');
                    Route::put('update/{service}', [ServiceController::class, 'update'])->name('master.service.update');
                });
                Route::delete('delete/{service}', [ServiceController::class, 'delete'])->name('master.service.delete')->middleware('check_role:1');
            });
        });
    });

    Route::prefix('finance')->group(function () {
        Route::middleware(['check_role:1,2,3,4'])->group(function () {
            Route::get('/expenses', [ExpenseController::class, 'index'])->name('finance.expenses');
            Route::prefix('expense')->group(function () {
                Route::middleware(['check_role:1,3,4'])->group(function () {
                    Route::post('store', [ExpenseController::class, 'store'])->name('finance.expense.store');
                    Route::put('update/{expense}', [ExpenseController::class, 'update'])->name('finance.expense.update');
                });
                Route::delete('delete/{expense}', [ExpenseController::class, 'delete'])->name('finance.expense.delete')->middleware('check_role:1');
            });
        });

        Route::get('/payrolls', [PayrollController::class, 'index'])->name('finance.payrolls');
        Route::prefix('payroll')->group(function () {
            Route::middleware(['check_role:1,3,4'])->group(function () {
                Route::get('create', [PayrollController::class, 'create'])->name('finance.payroll.create');
                Route::post('store', [PayrollController::class, 'store'])->name('finance.payroll.store');
                Route::get('edit/{payroll}', [PayrollController::class, 'edit'])->name('finance.payroll.edit');
                Route::put('update/{payroll}', [PayrollController::class, 'update'])->name('finance.payroll.update');
                Route::get('get-salary/{employeeId}', [PayrollController::class, 'getSalaryEmployee'])->name('finance.payroll.getSalaryEmployee');
            });
            Route::delete('delete/{payroll}', [PayrollController::class, 'delete'])->name('finance.payroll.delete')->middleware('check_role:1');
        });

        Route::middleware(['check_role:1,2,3,4'])->group(function () {
            Route::get('/cashes', [CashController::class, 'index'])->name('finance.cashes');
            Route::prefix('cash')->group(function () {
                Route::middleware(['check_role:1,3,4'])->group(function () {
                    Route::post('store', [CashController::class, 'store'])->name('finance.cash.store');
                    Route::put('update/{cash}', [CashController::class, 'update'])->name('finance.cash.update');
                });
                Route::delete('delete/{cash}', [CashController::class, 'delete'])->name('finance.cash.delete')->middleware('check_role:1');
            });
        });

        Route::middleware(['check_role:1,2,3,4,5'])->group(function () {
            Route::get('/debts', [DebtController::class, 'index'])->name('finance.debts');
            Route::prefix('debt')->group(function () {
                Route::middleware(['check_role:1,3,4'])->group(function () {
                    Route::post('store', [DebtController::class, 'store'])->name('finance.debt.store');
                    Route::put('update/{debt}', [DebtController::class, 'update'])->name('finance.debt.update');
                });
                Route::delete('delete/{debt}', [DebtController::class, 'delete'])->name('finance.debt.delete')->middleware('check_role:1');
            });
        });
    });

    Route::prefix('manage')->group(function () {
        Route::get('/attendances', [AttendanceController::class, 'index'])->name('manage.attendances');
        Route::prefix('attendance')->group(function () {
            Route::post('store', [AttendanceController::class, 'store'])->name('manage.attendance.store');
            Route::delete('delete/{attendance}', [AttendanceController::class, 'delete'])->name('manage.attendance.delete');
        });

        Route::get('/transactions', [TransactionController::class, 'index'])->name('manage.transactions');
        Route::prefix('transaction')->group(function () {
            Route::middleware(['check_role:1,3,4'])->group(function () {
                Route::get('create', [TransactionController::class, 'create'])->name('manage.transaction.create');
                Route::post('store', [TransactionController::class, 'store'])->name('manage.transaction.store');
                Route::get('edit/{transaction}', [TransactionController::class, 'edit'])->name('manage.transaction.edit');
                Route::put('update/{transaction}', [TransactionController::class, 'update'])->name('manage.transaction.update');
                Route::get('get-price/{serviceId}', [TransactionController::class, 'getPrice'])->name('manage.transaction.getPrice');
            });
            Route::delete('delete/{transaction}', [TransactionController::class, 'delete'])->name('manage.transaction.delete')->middleware('check_role:1');
        });

        Route::middleware(['check_role:1,2,3'])->group(function () {
            Route::get('/inventory', [InventoryController::class, 'index'])->name('manage.inventory');
            Route::prefix('inventory')->group(function () {
                Route::middleware(['check_role:1,3'])->group(function () {
                    Route::post('store', [InventoryController::class, 'store'])->name('manage.inventory.store');
                    Route::put('update/{inventory}', [InventoryController::class, 'update'])->name('manage.inventory.update');
                });
                Route::delete('delete/{inventory}', [InventoryController::class, 'delete'])->name('manage.inventory.delete')->middleware('check_role:1');
            });
        });
    });

    Route::prefix('setting')->group(function () {
        Route::prefix('application')->group(function () {
            Route::get('/', [ApplicationController::class, 'index'])->name('setting.application')->middleware('check_role:1,3');
            Route::put('update/{application}', [ApplicationController::class, 'update'])->name('setting.application.update')->middleware('check_role:1,3');
        });

        Route::prefix('account')->group(function () {
            Route::get('/', [AccountController::class, 'index'])->name('setting.account');
            Route::put('update-profile/{user}', [AccountController::class, 'updateProfile'])->name('setting.account.updateProfile');
            Route::put('update-password/{user}', [AccountController::class, 'updatePassword'])->name('setting.account.updatePassword');
        });
    });
});

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