<?php

use App\Http\Controllers\AdminAuthController;
use App\Http\Controllers\AdminUserController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Admin Authentication Routes
|--------------------------------------------------------------------------
|
| Routes untuk autentikasi admin (login, logout).
|
*/

// Guest routes (belum login)
Route::middleware('guest')->group(function () {
    Route::redirect('/admin/login', '/login')->name('admin.login');
});

// Authenticated admin routes (sudah login sebagai admin)
Route::middleware('auth')->group(function () {
    Route::post('/admin/logout', [AdminAuthController::class, 'logout'])->name('admin.logout');

    // Admin Dashboard
    Route::get('/admin/dashboard', function () {
        if (auth()->user()->role !== 'admin') {
            return redirect()->route('user.dashboard')->with('error', 'Anda tidak memiliki akses ke halaman admin.');
        }
        return view('admin.dashboard');
    })->name('admin.dashboard');

    /*
    |--------------------------------------------------------------------------
    | Admin User Management Routes
    |--------------------------------------------------------------------------
    |
    | Routes untuk mengelola pengguna (hanya admin yang bisa akses).
    |
    */

    Route::resource('/admin/users', AdminUserController::class, [
        'as' => 'admin',
        'names' => [
            'index' => 'admin.users.index',
            'create' => 'admin.users.create',
            'store' => 'admin.users.store',
            'show' => 'admin.users.show',
            'edit' => 'admin.users.edit',
            'update' => 'admin.users.update',
            'destroy' => 'admin.users.destroy',
        ]
    ]);
});


