<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\admin\{UserController, EmployeeController, PeopleController,ChartController,assistanceController,paysController,salesController,membershipController,productController,posController,SupplierController,BillController,PayBillController,CompanyController,EntryTimeController};
use App\Http\Controllers\admin\{ExpenseController};
use App\Http\Controllers\public\userCompanyController;
use App\Http\Controllers\rateController;


Route::middleware(['web', 'auth', 'role:Administrador|Empleado', 'company.info', 'rate.update'])
	->prefix('admin-fit')
	->name('admin_fit.')
	->group(function (): void {
		Route::get('/', function () {
			return 'Admin Fit Dashboard';
		})->name('dashboard');

		Route::prefix('users')->name('users.')->group(function (): void {
            Route::get('/', [UserController::class, 'index'])->name('index');
            Route::get('/json', [UserController::class, 'userPaginatedJson'])->name('json');
			Route::post('/export', [UserController::class, 'export'])->name('export');
            Route::get('/image/{filename}', [UserController::class, 'image'])->where('filename', '[A-Za-z0-9._-]+')->name('image');
			Route::get('/{user}/membership-sale/preview', [UserController::class, 'membershipSalePreview'])->name('membership_sale_preview');
			Route::post('/{user}/membership-sale', [UserController::class, 'createMembershipSale'])->name('membership_sale_create');

            Route::get('/{user}/sales', [salesController::class, 'getUserSales'])->name('sales');
			Route::get('/create', [UserController::class, 'create'])->name('create');
			Route::post('/', [UserController::class, 'store'])->name('store');
			Route::get('/{user}/edit', [UserController::class, 'edit'])->name('edit');
			
            Route::put('/{user}', [UserController::class, 'update'])->name('update');
			Route::patch('/{user}/set-active', [UserController::class, 'setIs_active'])->name('set_active');
		});

		Route::prefix('assistance')->name('assistance.')->group(function (): void {
            Route::get('/', [assistanceController::class, 'index'])->name('index')->middleware('role:Administrador|Empleado');
            Route::get('/history', [assistanceController::class, 'history'])->name('history');
            Route::get('/takeAssistance/{entry_time_id?}', [assistanceController::class, 'takeAssistance'])->name('takeAssistance')->middleware('role:Administrador|Empleado');
            Route::post('/store', [assistanceController::class, 'storeAssistance'])->name('storeAssistance')->middleware('role:Administrador|Empleado');
            Route::post('/store-multiple', [assistanceController::class, 'createMultipleAssistance'])->name('createMultipleAssistance')->middleware('role:Administrador|Empleado');
            Route::post('/remove', [assistanceController::class, 'removeAssistance'])->name('removeAssistance');
            Route::get('/json', [assistanceController::class, 'payPaginatedJson'])->name('json');
            Route::get('/jsondaily', [assistanceController::class, 'payPaginatedJsonDaily'])->name('jsonDaily');
		});

		Route::prefix('pay')->name('pay.')->group(function (): void {
			Route::get('/', [paysController::class, 'index'])->name('index');
			Route::get('/json', [paysController::class, 'payPaginatedJson'])->name('json');
			Route::post('/export', [paysController::class, 'export'])->name('export');
			Route::get('/image/{filename}', [paysController::class, 'image'])->where('filename', '[A-Za-z0-9._-]+')->name('image');
			Route::get('/create', [paysController::class, 'create'])->name('create');
			Route::post('/', [paysController::class, 'store'])->name('store');
			Route::get('/{pay}/edit', [paysController::class, 'edit'])->name('edit');
			Route::put('/{pay}', [paysController::class, 'update'])->name('update');
		});
        

		Route::prefix('employees')->name('employees.')->group(function (): void {
			Route::get('/', function () {
				return 'Employee Administration - List';
			})->name('index');
            
            Route::get('/json', [EmployeeController::class, 'employeePaginatedJson'])->name('json');
			Route::get('/create', function () {
				return 'Employee Administration - Create';
			})->name('create');

			Route::get('/{employee}/edit', function (string $employee) {
				return "Employee Administration - Edit {$employee}";
			})->name('edit');

			Route::delete('/{employee}', function (string $employee) {
				return "Employee Administration - Delete {$employee}";
			})->name('destroy');
		});
		Route::prefix('people')->name('people.')->group(function (): void { 
			Route::get('/', [PeopleController::class, 'index'])->name('index');
            Route::get('/json', [PeopleController::class, 'peoplePaginatedJson'])->name('json');

			Route::get('/create', [PeopleController::class, 'create']   )->name('create');
			Route::post('/', [PeopleController::class, 'store'])->name('store');
			Route::get('/{person}/edit', function (string $person) {
				return "People Administration - Edit {$person}";
			})->name('edit');

			Route::delete('/{person}', function (string $person) {
				return "People Administration - Delete {$person}";
			})->name('destroy');
		});
		Route::prefix('chart')->name('chart.')->group(function (): void { 
			Route::get('/memberschipsformoutn', [ChartController::class, 'getChartMemberschipsForMoutn'])->name('memberschipsformoutn');
			Route::get('/daysformembership', [ChartController::class, 'getDaysForMembership'])->name('daysformembership');
			Route::get('/todayattendancerate/{mesAnterior?}', [ChartController::class, 'getTodayAttendanceRate'])->name('todayattendancerate');
			Route::get('/topattendees/{mesAnterior?}', [ChartController::class, 'getTopAttendees'])->name('topattendees');
		});

		Route::prefix('rate')->name('rate.')->group(function (): void {
			Route::get('/', [rateController::class, 'index'])->name('index');
			Route::get('/json', [rateController::class, 'ratePaginatedJson'])->name('json');
			Route::get('/create', [rateController::class, 'create'])->name('create');
			Route::post('/store', [rateController::class, 'store'])->name('store');
			Route::post('/quick-store', [rateController::class, 'quickStore'])->name('quick_store');
		});
		Route::prefix('sales')->name('sales.')->group(function (): void {
			Route::get('/', [salesController::class, 'index'])->name('index');
			Route::get('/json', [salesController::class, 'salesPaginatedJson'])->name('json');
			Route::get('/{sale}/edit', [salesController::class, 'edit'])->name('edit')->middleware('role:Administrador');
			Route::get('/{sale}/edit-data', [salesController::class, 'editData'])->name('edit_data')->middleware('role:Administrador');
			Route::put('/{sale}', [salesController::class, 'update'])->name('update')->middleware('role:Administrador');
		});
		Route::prefix('membership')->name('membership.')->group(function (): void {
            Route::get('/', [membershipController::class, 'index'])->name('index');
            Route::get('/json', [membershipController::class, 'membershipPaginatedJson'])->name('json');
			Route::get('/create', [membershipController::class, 'create'])->name('create');
			Route::post('/', [membershipController::class, 'store'])->name('store');
			Route::get('/{membership}/edit', [membershipController::class, 'edit'])->name('edit');
			Route::put('/{membership}', [membershipController::class, 'update'])->name('update');
		});

		Route::prefix('entry-time')->name('entry_time.')->group(function (): void {
            Route::get('/', [EntryTimeController::class, 'index'])->name('index');
            Route::get('/json', [EntryTimeController::class, 'entryTimePaginatedJson'])->name('json');
			Route::get('/create', [EntryTimeController::class, 'create'])->name('create');
			Route::post('/', [EntryTimeController::class, 'store'])->name('store');
			Route::get('/{entryTime}/edit', [EntryTimeController::class, 'edit'])->name('edit');
			Route::put('/{entryTime}', [EntryTimeController::class, 'update'])->name('update');
			Route::delete('/{entryTime}', [EntryTimeController::class, 'destroy'])->name('destroy');
		});

		Route::prefix('product')->name('product.')->group(function (): void {
			Route::get('/', [productController::class, 'index'])->name('index');
			Route::get('/json', [productController::class, 'productPaginatedJson'])->name('json');
			Route::get('/create', [productController::class, 'create'])->name('create');
			Route::post('/', [productController::class, 'store'])->name('store');
			Route::get('/{product}/edit', [productController::class, 'edit'])->name('edit');
			Route::put('/{product}', [productController::class, 'update'])->name('update');
			Route::patch('/{product}/set-active', [productController::class, 'setIs_active'])->name('set_active');
			Route::delete('/{product}', [productController::class, 'destroy'])->name('destroy');
		});

		Route::prefix('supplier')->name('supplier.')->group(function (): void {
			Route::get('/', [SupplierController::class, 'index'])->name('index');
			Route::get('/json', [SupplierController::class, 'supplierPaginatedJson'])->name('json');
			Route::get('/create', [SupplierController::class, 'create'])->name('create');
			Route::post('/', [SupplierController::class, 'store'])->name('store');
			Route::get('/{supplier}/edit', [SupplierController::class, 'edit'])->name('edit');
			Route::put('/{supplier}', [SupplierController::class, 'update'])->name('update');
			Route::patch('/{supplier}/set-active', [SupplierController::class, 'setIs_active'])->name('set_active');
			Route::delete('/{supplier}', [SupplierController::class, 'destroy'])->name('destroy');
			// Creación rápida de proveedor para AJAX
			Route::post('/quick-store', [SupplierController::class, 'quickStore'])->name('quick_store');
		});
		Route::prefix('expense')->name('expense.')->group(function (): void {
			// Gastos
			Route::get('/', [ExpenseController::class, 'index'])->name('index');
			Route::get('/json', [ExpenseController::class, 'expensePaginatedJson'])->name('json');
			Route::get('/create', [ExpenseController::class, 'create'])->name('create');
			Route::post('/', [ExpenseController::class, 'store'])->name('store');
			Route::get('/summary', [ExpenseController::class, 'summary'])->name('summary');
			Route::post('/export', [ExpenseController::class, 'export'])->name('export');
			Route::get('/image/{filename}', [ExpenseController::class, 'image'])->name('image');
			Route::get('/{expense}/edit', [ExpenseController::class, 'edit'])->name('edit');
			Route::put('/{expense}', [ExpenseController::class, 'update'])->name('update');
			Route::delete('/{expense}', [ExpenseController::class, 'destroy'])->name('destroy');
			
			// Categorías
			Route::get('/categories', [ExpenseController::class, 'categoriesIndex'])->name('categories');
			Route::post('/categories', [ExpenseController::class, 'storeCategory'])->name('categories.store');
			Route::put('/categories/{category}', [ExpenseController::class, 'updateCategory'])->name('categories.update');
			Route::delete('/categories/{category}', [ExpenseController::class, 'destroyCategory'])->name('categories.destroy');
		});

		Route::prefix('bill')->name('bill.')->group(function (): void {
			Route::get('/', [BillController::class, 'index'])->name('index');
			Route::get('/json', [BillController::class, 'billPaginatedJson'])->name('json');
			Route::get('/create', [BillController::class, 'create'])->name('create');
			Route::post('/', [BillController::class, 'store'])->name('store');
			Route::get('/supplier-search', [BillController::class, 'searchSuppliers'])->name('supplier_search');
			Route::get('/search-suppliers-dt', [BillController::class, 'searchSuppliersDt'])->name('search_suppliers_dt');
			Route::post('/supplier-quick-create', [BillController::class, 'quickCreateSupplier'])->name('supplier_quick_create');
			Route::get('/product-search', [BillController::class, 'searchProducts'])->name('product_search');
			Route::get('/search-products-dt', [BillController::class, 'searchProductsDt'])->name('search_products_dt');
			Route::post('/product-quick-create', [BillController::class, 'quickCreateProduct'])->name('product_quick_create');
			Route::get('/{bill}/edit', [BillController::class, 'edit'])->name('edit');
			Route::put('/{bill}', [BillController::class, 'update'])->name('update');
			Route::delete('/{bill}', [BillController::class, 'destroy'])->name('destroy');
		});

		Route::prefix('pay-bill')->name('pay_bill.')->group(function (): void {
			Route::get('/', [PayBillController::class, 'index'])->name('index');
			Route::get('/json', [PayBillController::class, 'payBillPaginatedJson'])->name('json');
			Route::get('/image/{filename}', [PayBillController::class, 'image'])->where('filename', '[A-Za-z0-9._-]+')->name('image');
			Route::get('/create', [PayBillController::class, 'create'])->name('create');
			Route::post('/', [PayBillController::class, 'store'])->name('store');
			Route::get('/{payBill}/edit', [PayBillController::class, 'edit'])->name('edit');
			Route::put('/{payBill}', [PayBillController::class, 'update'])->name('update');
			Route::delete('/{payBill}', [PayBillController::class, 'destroy'])->name('destroy');
		});

		Route::prefix('company')->name('company.')->group(function (): void {
			Route::get('/edit', [userCompanyController::class, 'edit'])->name('edit');
			Route::put('/update', [userCompanyController::class, 'update'])->name('update');
			Route::get('/image/{filename}', [userCompanyController::class, 'image'])
				->where('filename', '[A-Za-z0-9._-]+')
				->name('image');
		});
		Route::prefix('pos')->name('pos.')->group(function (): void {
			Route::get('/', [posController::class, 'index'])->name('index');
			Route::get('/products', [posController::class, 'products'])->name('products');
			Route::get('/users', [posController::class, 'users'])->name('users');
			Route::post('/sale', [posController::class, 'store'])->name('sale.store');
		});
	});

Route::middleware(['web', 'auth', 'role:SuperAdmin', 'rate.update'])
	->prefix('admin-fit/super-admin')
	->name('admin_fit.super_admin.')
	->group(function (): void {
		Route::prefix('companies')->name('companies.')->group(function (): void {
			Route::get('/', [CompanyController::class, 'index'])->name('index');
			Route::get('/json', [CompanyController::class, 'companiesPaginatedJson'])->name('json');
			Route::get('/create', [CompanyController::class, 'create'])->name('create');
			Route::post('/', [CompanyController::class, 'store'])->name('store');
			Route::get('/{id}/edit', [CompanyController::class, 'edit'])->name('edit');
			Route::put('/{id}', [CompanyController::class, 'update'])->name('update');
			Route::delete('/{id}', [CompanyController::class, 'destroy'])->name('destroy');
			Route::post('/{id}/assign-user', [CompanyController::class, 'assignUser'])->name('assign_user');
			Route::delete('/{id}/remove-user/{user}', [CompanyController::class, 'removeUser'])->name('remove_user');
		});

		Route::prefix('users')->name('users.')->group(function (): void {
			Route::get('/create', [CompanyController::class, 'createUser'])->name('create');
			Route::post('/', [CompanyController::class, 'storeUser'])->name('store');
		});
	});
