<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\InventoryController;
use App\Http\Controllers\InventoryItemController;
use App\Http\Controllers\StaffController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\AreaController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\DashboardController;
use Illuminate\Support\Facades\Route;


// =========================================================
// AUTHENTICATION
// =========================================================

Route::get('/', function () {
    return view('auth.login');
})->name('login');

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

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

// =========================================================
// PROTECTED APPLICATION
// =========================================================

Route::middleware('auth')->group(function () {


    // =====================================================
    // DASHBOARD
    // =====================================================

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


    // =====================================================
    // AREAS
    // =====================================================

    Route::get('/area/list', [AreaController::class, 'index'])
        ->name('area.list');

    Route::get('/area/add', [AreaController::class, 'create'])
        ->name('area.add');

    Route::post('/area/store', [AreaController::class, 'store'])
        ->name('area.store');

    Route::get('/area/edit/{id}', [AreaController::class, 'edit'])
        ->name('area.edit');

    Route::put('/area/update/{id}', [AreaController::class, 'update'])
        ->name('area.update');

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


// =====================================================
// CATEGORIES
// =====================================================

// Everyone logged in can VIEW categories
Route::get('/categories/list', [CategoryController::class, 'index'])
    ->name('categories.list');

Route::get('/categories/{id}', [CategoryController::class, 'show'])
    ->name('categories.show');


// =====================================================
// CATEGORIES
// =====================================================

// Everyone logged in can VIEW categories
Route::get('/categories/list', [CategoryController::class, 'index'])
    ->name('categories.list');


// =====================================================
// CATEGORIES — ADMIN ONLY ACTIONS
// =====================================================

Route::middleware('admin')->group(function () {

    // Add Category
    Route::get('/categories/add', [CategoryController::class, 'create'])
        ->name('categories.add');

    Route::post('/categories', [CategoryController::class, 'store'])
        ->name('categories.store');

    // Edit Category
    Route::get('/categories/{id}/edit', [CategoryController::class, 'edit'])
        ->whereNumber('id')
        ->name('categories.edit');

    Route::put('/categories/{id}', [CategoryController::class, 'update'])
        ->whereNumber('id')
        ->name('categories.update');

    // Delete Category
    Route::delete('/categories/{id}', [CategoryController::class, 'destroy'])
        ->whereNumber('id')
        ->name('categories.destroy');
});


// Everyone logged in can VIEW individual category
// Keep this AFTER /categories/add and /categories/{id}/edit
Route::get('/categories/{id}', [CategoryController::class, 'show'])
    ->whereNumber('id')
    ->name('categories.show');

// -----------------------------------------------------
// INVENTORY — VIEW ACCESS
// Admin + Staff
// -----------------------------------------------------

Route::get('/inventory/list', [InventoryItemController::class, 'index'])
    ->name('inventory.list');

Route::get('/inventory/{id}', [InventoryItemController::class, 'show'])
    ->whereNumber('id')
    ->name('inventory.show');


// -----------------------------------------------------
// INVENTORY — ADMIN ONLY ACTIONS
// -----------------------------------------------------

Route::middleware('admin')->group(function () {

    // Add Inventory
    Route::get('/inventory/add', [InventoryItemController::class, 'create'])
        ->name('inventory.add');

    Route::post('/inventory', [InventoryItemController::class, 'store'])
        ->name('inventory.store');

    // Edit Inventory
    Route::get('/inventory/{id}/edit', [InventoryItemController::class, 'edit'])
        ->whereNumber('id')
        ->name('inventory.edit');

    Route::put('/inventory/{id}', [InventoryItemController::class, 'update'])
        ->whereNumber('id')
        ->name('inventory.update');

    // Delete Inventory
    Route::delete('/inventory/{id}', [InventoryItemController::class, 'destroy'])
        ->whereNumber('id')
        ->name('inventory.destroy');
});// =====================================================
// STAFF
// =====================================================

// Staff can VIEW the list
Route::get('/staff/list', [StaffController::class, 'index'])
    ->name('staff.list');


// =====================================================
// STAFF — ADMIN ONLY ACTIONS
// =====================================================

Route::middleware('admin')->group(function () {

    // Add Staff
    Route::get('/staff/add', [StaffController::class, 'create'])
        ->name('staff.add');

    Route::post('/staff', [StaffController::class, 'store'])
        ->name('staff.store');

    // Edit Staff
    Route::get('/staff/{id}/edit', [StaffController::class, 'edit'])
        ->name('staff.edit');

    Route::put('/staff/{id}', [StaffController::class, 'update'])
        ->name('staff.update');

    // Delete Staff
    Route::delete('/staff/{id}', [StaffController::class, 'destroy'])
        ->name('staff.destroy');
});


// Staff can VIEW individual staff details
// Keep this AFTER /staff/add and /staff/{id}/edit
Route::get('/staff/{id}', [StaffController::class, 'show'])
    ->whereNumber('id')
    ->name('staff.show');
    // =====================================================
    // ORDERS
    // =====================================================

    Route::get('/order/list', [OrderController::class, 'index'])
        ->name('orders.list');

    Route::get('/order/add', [OrderController::class, 'create'])
        ->name('orders.add');

    Route::post('/order/store', [OrderController::class, 'store'])
        ->name('orders.store');

               /*
|--------------------------------------------------------------------------
| Order Edit
|--------------------------------------------------------------------------
*/

Route::get(
    '/order/{orderId}/edit',
    [OrderController::class, 'edit']
)->name('orders.edit');

Route::put('/order/{id}', [OrderController::class, 'update'])
    ->name('orders.update');
    // =====================================================
    // INVOICE
    // =====================================================

    Route::get('/invoice/{invoiceId}', [OrderController::class, 'showInvoice'])
        ->name('invoices.show');
 

/*
|--------------------------------------------------------------------------
| Delete Invoice
|--------------------------------------------------------------------------
*/ Route::delete('/invoice/{invoiceId}', [OrderController::class,'destroyInvoice'])
    ->name('invoices.destroy');

// -----------------------------------------------------
// CUSTOMER — VIEW ACCESS
// Admin + Staff
// -----------------------------------------------------

// Customer List
Route::get('/customer/list', [CustomerController::class, 'index'])
    ->name('customer.list');

// View Individual Customer
Route::get('/customer/{id}', [CustomerController::class, 'show'])
    ->whereNumber('id')
    ->name('customer.show');


// -----------------------------------------------------
// CUSTOMER — ADMIN ONLY ACTIONS
// -----------------------------------------------------

Route::middleware('admin')->group(function () {

    // Add Customer
    Route::get('/customer/add', [CustomerController::class, 'create'])
        ->name('customer.add');

    Route::post('/customer', [CustomerController::class, 'store'])
        ->name('customer.store');

    // Edit Customer
    Route::get('/customer/{id}/edit', [CustomerController::class, 'edit'])
        ->whereNumber('id')
        ->name('customer.edit');

    Route::put('/customer/{id}', [CustomerController::class, 'update'])
        ->whereNumber('id')
        ->name('customer.update');

    // Delete Customer
    Route::delete('/customer/{id}', [CustomerController::class, 'destroy'])
        ->whereNumber('id')
        ->name('customer.destroy');
});
// =====================================================
// PAYMENT
// =====================================================

Route::get('/payments', [PaymentController::class, 'index'])
    ->name('payments.index');

Route::get('/payments/create', [PaymentController::class, 'create'])
    ->name('payments.create');

Route::post('/payments', [PaymentController::class, 'store'])
    ->name('payments.store');

Route::get('/payments/order/{orderId}/summary', [PaymentController::class, 'summary'])
    ->name('payments.order.summary');

Route::get('/payments/{payment}/show', [PaymentController::class, 'show'])
    ->name('payments.show');

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

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

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


// =====================================================
// REPORTS — ADMIN ONLY
// =====================================================

Route::get('/report', [ReportController::class, 'index'])
    ->name('report.index')
    ->middleware('admin');

// =====================================================
// SETTINGS — ADMIN ONLY
// =====================================================

Route::get('/settings', [SettingsController::class, 'index'])
    ->name('settings')
    ->middleware('admin');

Route::post('/settings', [SettingsController::class, 'update'])
    ->name('settings.update')
    ->middleware('admin');
    

});