<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\ComplaintController;
use App\Http\Controllers\SatisfactionController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\CustomerController;

/*
|--------------------------------------------------------------------------
| 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()) {
        if (auth()->user()->role === 'admin') {
            return redirect()->route('admin.dashboard');
        } else {
            return redirect('/home');
        }
    }
    return redirect('/login');
})->name('dashboard');

// Rute untuk autentikasi
Route::middleware('guest')->group(function () {
    // Login
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);

    // Register Customer
    Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
    Route::post('/register', [RegisterController::class, 'register']);

    // Forgot Password
    Route::get('/forgot-password', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
    Route::post('/forgot-password', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');

    // Reset Password
    Route::get('/reset-password/{token}', [ResetPasswordController::class, 'showResetForm'])->name('password.reset');
    Route::post('/reset-password', [ResetPasswordController::class, 'reset'])->name('password.update');
});

// Logout
Route::post('/logout', [LoginController::class, 'logout'])->name('logout')->middleware('auth');

// Rute untuk Admin
Route::prefix('admin')->middleware(['auth', 'admin'])->name('admin.')->group(function () {
    // Dashboard Admin
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');

    // Register Admin (hanya admin yang bisa mendaftarkan admin baru)
    Route::post('/register-admin', [RegisterController::class, 'registerAdmin'])->name('register.admin');
    Route::get('/register-admin', [RegisterController::class, 'showAdminRegisterForm'])->name('register');

    // Pengelolaan Admin
    Route::resource('admins', AdminController::class);
});


// Rute untuk fitur-fitur regular
Route::middleware(['auth'])->group(function () {
    Route::resource('complaints', ComplaintController::class);
    Route::get('complaints/{complaint}/pdf', [ComplaintController::class, 'pdf'])->name('complaints.pdf');
    Route::get('complaints-pdf', [ComplaintController::class, 'allPdf'])->name('complaints.all-pdf')->middleware('admin');
    Route::resource('services', ServiceController::class);
    Route::get('services/{service}/pdf', [ServiceController::class, 'generatePdf'])->name('services.pdf');
    Route::get('services-pdf', [ServiceController::class, 'generateAllPdf'])->name('services.all-pdf')->middleware('admin');
    Route::resource('satisfactions', SatisfactionController::class);
    Route::get('satisfactions/{satisfaction}/pdf', [SatisfactionController::class, 'pdf'])->name('satisfactions.pdf');
    Route::get('satisfactions-pdf', [SatisfactionController::class, 'allPdf'])->name('satisfactions.all-pdf')->middleware('admin');
    Route::get('satisfaction-dashboard', [SatisfactionController::class, 'dashboard'])->name('satisfactions.dashboard')->middleware('admin');
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::put('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::get('/home', [CustomerController::class, 'home'])->name('customer.home');
});
