<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ShopController;
use App\Http\Controllers\Admin\CustomerController;
use Illuminate\Support\Facades\Route;


Route::get('/', function () {
    return redirect('/login');
});
// ── Auth ──────────────────────────────────────────
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
Route::get('/password-help', function () {
    return view('auth.password-help');
})->name('password.help');
// ── Super Admin (type_id = 1) ─────────────────────
Route::middleware(['auth:admin', 'role:1'])->prefix('superadmin')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'superAdmin'])->name('superadmin.dashboard');
    Route::resource('shops', ShopController::class)->names('superadmin.shops');
    Route::put('/shops/{shop}/reset-owner', [\App\Http\Controllers\Admin\ShopController::class, 'resetOwnerCredentials'])->name('superadmin.shops.resetOwner');
    // Route::resource('garment-types', GarmentTypeController::class);
});

// ── Shop Owner (type_id = 2) ───────────────────────
Route::middleware(['auth:admin', 'role:2'])->prefix('shop')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'shopOwner'])->name('shop.dashboard');
    Route::resource('roles', \App\Http\Controllers\Admin\RoleController::class)->names('shop.roles');
    Route::resource('addons', \App\Http\Controllers\Admin\AddonController::class)->names('shop.addons');
    Route::resource('staff', \App\Http\Controllers\Admin\StaffController::class)
        ->names('shop.staff')->parameters(['staff' => 'staffMember']);

    Route::get('/garments', [\App\Http\Controllers\Admin\ShopGarmentController::class, 'index'])->name('shop.garments.index');
    Route::post('/garments/{garmentType}/toggle', [\App\Http\Controllers\Admin\ShopGarmentController::class, 'toggle'])->name('shop.garments.toggle');
    Route::get('/garments/{garmentType}/builder', [\App\Http\Controllers\Admin\ShopGarmentFieldController::class, 'index'])->name('shop.garments.builder');
    Route::post('/garments/{garmentType}/fields', [\App\Http\Controllers\Admin\ShopGarmentFieldController::class, 'store'])->name('shop.garments.fields.store');
    Route::put('/garments/{garmentType}/fields/{field}', [\App\Http\Controllers\Admin\ShopGarmentFieldController::class, 'update'])->name('shop.garments.fields.update');
    Route::delete('/garments/{garmentType}/fields/{field}', [\App\Http\Controllers\Admin\ShopGarmentFieldController::class, 'destroy'])->name('shop.garments.fields.destroy');
    Route::post('/garments/{garmentType}/fields/reorder', [\App\Http\Controllers\Admin\ShopGarmentFieldController::class, 'reorder'])->name('shop.garments.fields.reorder');
    // Route::resource('shop-garments', ShopGarmentController::class);
    // Route::resource('measurement-fields', ShopGarmentFieldController::class);
});

// ── Staff (type_id = 3) ────────────────────────────
Route::middleware(['auth:admin', 'role:3'])->prefix('staff')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'staff'])->name('staff.dashboard');
});
Route::middleware(['auth:admin', 'role:2,3'])->prefix('reports')->group(function () {
    Route::get('/', [\App\Http\Controllers\Admin\ReportController::class, 'index'])->name('reports.index');
    Route::get('/export/excel', [\App\Http\Controllers\Admin\ReportController::class, 'exportExcel'])->name('reports.export.excel');
    Route::get('/export/pdf', [\App\Http\Controllers\Admin\ReportController::class, 'exportPdf'])->name('reports.export.pdf');
});

// ── Orders — shared between Shop Owner (2) and Staff (3) ──────
Route::middleware(['auth:admin', 'role:2,3'])->prefix('orders')->group(function () {
    Route::get('/', [\App\Http\Controllers\Admin\OrderController::class, 'index'])->name('orders.index');
    Route::get('/create', [\App\Http\Controllers\Admin\OrderController::class, 'create'])->name('orders.create');
    Route::post('/', [\App\Http\Controllers\Admin\OrderController::class, 'store'])->name('orders.store');
    Route::get('/customers/search', [\App\Http\Controllers\Admin\OrderController::class, 'searchCustomers'])->name('orders.customers.search');
    Route::post('/customers/quick-add', [\App\Http\Controllers\Admin\OrderController::class, 'quickAddCustomer'])->name('orders.customers.quickAdd');
    Route::get('/garments/{garmentType}/fields', [\App\Http\Controllers\Admin\OrderController::class, 'garmentFields'])->name('orders.garments.fields');
    Route::post('/garments/{garmentType}/price', [\App\Http\Controllers\Admin\ShopGarmentController::class, 'updatePrice'])->name('shop.garments.price');
    Route::post('/garments', [\App\Http\Controllers\Admin\ShopGarmentController::class, 'store'])->name('shop.garments.store');
    Route::get('/garments/{garmentType}/addons', [\App\Http\Controllers\Admin\OrderController::class, 'garmentAddons'])->name('orders.garments.addons');
    Route::get('/customers/{customer}/orders', [\App\Http\Controllers\Admin\OrderController::class, 'customerOrders'])->name('orders.customers.orders');
    Route::get('/items/{orderItem}/measurements', [\App\Http\Controllers\Admin\OrderController::class, 'itemMeasurements'])->name('orders.items.measurements');
    Route::put('/{order}/stage', [\App\Http\Controllers\Admin\OrderController::class, 'updateStage'])->name('orders.updateStage');
    Route::get('/{order}', [\App\Http\Controllers\Admin\OrderController::class, 'show'])->name('orders.show');
    Route::post('/{order}/payments', [\App\Http\Controllers\Admin\OrderController::class, 'storePayment'])->name('orders.payments.store');
    Route::get('/garments/{garmentType}/price', [\App\Http\Controllers\Admin\OrderController::class, 'garmentPrice'])->name('orders.garments.price');

});
Route::middleware(['auth:admin', 'role:2,3'])->prefix('settings')->group(function () {
    Route::get('/prices', [\App\Http\Controllers\Admin\PriceSettingController::class, 'index'])->name('settings.prices.index');
    Route::post('/prices/{shopGarment}', [\App\Http\Controllers\Admin\PriceSettingController::class, 'update'])->name('settings.prices.update');
});
Route::middleware(['auth:admin', 'role:2,3'])->prefix('shop')->group(function () {
    Route::get('/age-types', [\App\Http\Controllers\Admin\AgeTypeController::class, 'index'])->name('shop.ageTypes.index');
    Route::post('/age-types', [\App\Http\Controllers\Admin\AgeTypeController::class, 'store'])->name('shop.ageTypes.store');
    Route::put('/age-types/{ageType}', [\App\Http\Controllers\Admin\AgeTypeController::class, 'update'])->name('shop.ageTypes.update');
    Route::delete('/age-types/{ageType}', [\App\Http\Controllers\Admin\AgeTypeController::class, 'destroy'])->name('shop.ageTypes.destroy');
    Route::post('/age-types/reorder', [\App\Http\Controllers\Admin\AgeTypeController::class, 'reorder'])->name('shop.ageTypes.reorder');
});
Route::middleware(['auth:admin', 'role:1'])->group(function () {
    Route::get('/settings/branding', [\App\Http\Controllers\Admin\AppSettingController::class, 'edit'])->name('superadmin.branding.edit');
    Route::post('/settings/branding', [\App\Http\Controllers\Admin\AppSettingController::class, 'update'])->name('superadmin.branding.update');
});

Route::middleware(['auth:admin', 'role:2'])->group(function () {
    Route::get('/settings/shop-profile', [\App\Http\Controllers\Admin\ShopProfileController::class, 'edit'])->name('shop.profile.edit');
    Route::post('/settings/shop-profile', [\App\Http\Controllers\Admin\ShopProfileController::class, 'update'])->name('shop.profile.update');
});
Route::middleware(['auth:admin', 'role:2'])->prefix('salary')->group(function () {
    Route::get('/', [\App\Http\Controllers\Admin\SalaryController::class, 'index'])->name('salary.index');
    Route::put('/{staff}/settings', [\App\Http\Controllers\Admin\SalaryController::class, 'updateSettings'])->name('salary.updateSettings');
    Route::get('/{staff}', [\App\Http\Controllers\Admin\SalaryController::class, 'show'])->name('salary.show');
    Route::post('/{staff}/payments', [\App\Http\Controllers\Admin\SalaryController::class, 'storePayment'])->name('salary.payments.store');
    Route::delete('/payments/{payment}', [\App\Http\Controllers\Admin\SalaryController::class, 'destroyPayment'])->name('salary.payments.destroy');
});
// ── Payments ledger — shared between Shop Owner (2) and Staff (3) ──
Route::middleware(['auth:admin', 'role:2,3'])->get('/payments', [\App\Http\Controllers\Admin\PaymentController::class, 'index'])->name('payments.index');
Route::middleware(['auth:admin', 'role:2,3'])->group(function () {
    Route::resource('customers', \App\Http\Controllers\Admin\CustomerController::class)->names('customers');
});
