<?php

use App\Http\Controllers\Gerai\GeraiController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\AuditController;
use App\Http\Controllers\Daily\DailyController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    if (Auth::check()) {
        return redirect()->route('admin.dashboard');
    } else {
        return view('auth/login');
    }
});

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    // Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
//Users
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.users.'], function () {
    Route::resource('/users', UserController::class);
    Route::middleware(['can:user-view'])->group(function () {
        Route::get('users', [UserController::class, 'index'])->name('index');
        Route::get('users/{user}', [UserController::class, 'show'])->name('show');
    });
    Route::middleware(['can:user-add'])->group(function () {
        Route::get('/users-management/create', [UserController::class, 'create'])->name('create');
        Route::post('users', [UserController::class, 'store'])->name('store');
    });
    Route::middleware(['can:user-edit'])->group(function () {
        Route::get('users/{user}/edit', [UserController::class, 'edit'])->name('edit');
        Route::post('/users/{user}', [UserController::class, 'update'])->name('update');
        Route::put('/users/{user}/edit', [UserController::class, 'updatePassword'])->name('updatepassword');
    });
    Route::middleware(['can:user-delete'])->group(function () {
        Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('destroy');
    });
});

//Roles
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.roles.'], function () {
    Route::resource('/roles', RoleController::class);
    Route::middleware(['can:role-view'])->group(function () {
        Route::get('roles', [RoleController::class, 'index'])->name('index');
        Route::get('/roles/{role}/users', [RoleController::class, 'retriveRoleUser'])->name('users');
    });
    Route::middleware(['can:role-add'])->group(function () {
        Route::get('/roles-management/create', [RoleController::class, 'create'])->name('create');
        Route::post('roles', [RoleController::class, 'store'])->name('store');
    });
    Route::middleware(['can:role-edit'])->group(function () {
        Route::get('roles/{role}/edit', [RoleController::class, 'edit'])->name('edit');
        Route::post('roles/{role}', [RoleController::class, 'update'])->name('update');
    });
    Route::middleware(['can:role-delete'])->group(function () {
        Route::delete('/roles/{role}', [RoleController::class, 'destroy'])->name('destroy');
    });
});

//Daily
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.daily.'], function () {
    Route::resource('/daily', DailyController::class);
    Route::middleware(['can:daily-view'])->group(function () {
        Route::get('daily', [DailyController::class, 'index'])->name('index');
        Route::get('/daily/{daily}', [DailyController::class, 'show'])->name('show');
    });

    Route::middleware(['can:daily-add'])->group(function () {
        Route::get('daily/create', [DailyController::class, 'create'])->name('create');
        Route::post('daily', [DailyController::class, 'store'])->name('store');
    });
    Route::middleware(['can:daily-edit'])->group(function () {
        Route::get('daily/{daily}/edit', [DailyController::class, 'edit'])->name('edit');
        Route::post('daily/{daily}', [DailyController::class, 'update'])->name('update');
    });

    Route::middleware(['can:daily-delete'])->group(function () {
        Route::delete('/daily/{daily}', [DailyController::class, 'destroy'])->name('destroy');
    });
});

//Invoice
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.invoice.'], function () {
    Route::middleware(['can:invoice-view'])->group(function () {
        Route::get('invoices', [InvoiceController::class, 'index'])->name('index');
        Route::get('/invoices/{invoice}', [InvoiceController::class, 'show'])->name('show');
        Route::post('/invoices/{invoice}/update', [InvoiceController::class, 'updateStatus'])->name('status');
    });

    Route::middleware(['can:invoice-add'])->group(function () {
        Route::get('invoice/create', [InvoiceController::class, 'create'])->name('create');
        Route::post('invoices', [InvoiceController::class, 'store'])->name('store');
    });
    Route::middleware(['can:invoice-edit'])->group(function () {
        Route::middleware(['CheckEditPermission'])->group(function () {
            Route::get('invoices/{invoice}/edit', [InvoiceController::class, 'edit'])->name('edit');
            Route::post('invoices/{invoice}', [InvoiceController::class, 'update'])->name('update');
        });
    });

    Route::middleware(['can:invoice-delete'])->group(function () {
        Route::delete('/invoices/{invoice}', [InvoiceController::class, 'destroy'])->name('destroy');
    });
});

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.gerai.'], function () {

    Route::middleware(['can:invoice-view'])->group(function () {
        Route::get('gerai', [GeraiController::class, 'index'])->name('index');
    });

    Route::middleware(['can:invoice-add'])->group(function () {
        Route::get('gerai/create', [GeraiController::class, 'create'])->name('create');
        Route::post('gerai', [GeraiController::class, 'store'])->name('store');
    });

    Route::middleware(['can:invoice-edit'])->group(function () {
        Route::get('gerai/{gerai}/edit', [GeraiController::class, 'edit'])->name('edit');
        Route::post('gerai/{gerai}', [GeraiController::class, 'update'])->name('update');
    });

    Route::middleware(['can:invoice-view'])->group(function () {
        Route::get('gerai/{gerai}', [GeraiController::class, 'show'])->name('show');
    });

    Route::middleware(['can:invoice-delete'])->group(function () {
        Route::delete('gerai/{gerai}', [GeraiController::class, 'destroy'])->name('destroy');
    });
});

// Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.audit.'], function () {

//     Route::middleware(['can:invoice-view'])->group(function () {
//         Route::get('admin/audit', [AuditController::class, 'index'])->name('index');
//     });
// });

// Audit Trail
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'verified'], 'as' => 'admin.audit.'], function () {
    Route::get('audit-trail', function () {
        return view('audit.index');
    })->name('index');
});



require __DIR__ . '/auth.php';
