<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\KamarController;
use App\Http\Controllers\DashboardAdminController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\RoomController;
use App\Http\Controllers\TransaksiController;
use App\Http\Controllers\KeuanganController;
use App\Http\Controllers\BookingController;
use App\Http\Controllers\PembayaranController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\ExpenseController;
use App\Http\Controllers\LaporanController;
use App\Http\Controllers\DashboardPemilikController;
use Illuminate\Support\Facades\Route;
use SebastianBergmann\CodeCoverage\Report\Html\Dashboard;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

// Halaman utama
Route::get('/home', [HomeController::class, 'index'])->name('users.home');

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

// Route untuk autentikasi (hanya guest)
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login']);
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register']);
});

// Menampilkan form lupa password
Route::get('/forgot-password', [AuthController::class, 'showForgotPasswordForm'])->name('password.request');

// Mengarahkan ke form reset password setelah email ditemukan
Route::post('/forgot-password', [AuthController::class, 'sendResetForm'])->name('password.sendResetForm');

// Menampilkan form reset password
Route::get('/reset-password', [AuthController::class, 'showResetForm'])->name('password.reset');

// Proses reset password
Route::post('/reset-password', [AuthController::class, 'resetPassword'])->name('password.update');

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

// ===============================
// Route untuk User
// ===============================
Route::middleware(['auth', 'roles:user'])->group(function () {
    Route::get('/dashboard-user', fn() => view('users.peta'))->name('users.peta');
    // Route::get('/profile', [UserController::class, 'edit'])->name('users.editprofile');
    // Route::put('/user/profile', [UserController::class, 'update'])->name('users.updateProfile');
    Route::get('/petakos', [KamarController::class, 'index'])->name('users.peta');
    Route::get('/detail-kamar/{id_kamar}', [RoomController::class, 'show'])->name('users.detail_kamar');
    Route::post('/booking-kamar', [BookingController::class, 'storebooking'])->name('users.storebooking');
    Route::get('/pembayaran/{id_booking}', [PembayaranController::class, 'create'])->name('users.pembayarancreate');
    Route::post('/pembayaran/{id_booking}', [PembayaranController::class, 'store'])->name('users.pembayaran');
    Route::get('/detail-pesanan/{id_booking}', [UserController::class, 'detailPesanan'])->name('users.detail-pesanan');
    Route::get('/perpanjang/{id_booking}', [TransaksiController::class, 'formPerpanjang'])->name('users.perpanjangan-sewa');
    Route::post('/perpanjang/{id_booking}', [TransaksiController::class, 'prosesPerpanjang'])->name('users.perpanjangproses');
    Route::get('/profile', [UserController::class, 'showProfile'])->name('users.profile');
    Route::get('/profile/edit', [UserController::class, 'editProfile'])->name('users.edit-profile');
    Route::put('/profile/update', [UserController::class, 'updateProfile'])->name('users.updateProfile');
});


// ===============================
// Route untuk Admin
// ===============================
Route::middleware(['auth', 'roles:admin'])->group(function () {
    Route::get('/dashboard-admin', [DashboardAdminController::class, 'index'])->name('admin.dashboard');

    // Profil admin
    Route::get('/profil', [AdminController::class, 'showProfil'])->name('admin.profil');
    Route::get('/profil/edit', [AdminController::class, 'editProfil'])->name('admin.edit-profil');
    Route::put('/profil/update', [AdminController::class, 'updateProfil'])->name('admin.updateProfil');

    // Customer
    Route::prefix('customer')->group(function () {
        Route::get('/', [CustomerController::class, 'index'])->name('admin.customer');
        Route::get('/create', [CustomerController::class, 'create'])->name('admin.createcustomer');
        Route::post('/store', [CustomerController::class, 'store'])->name('admin.storecustomer');
        Route::get('/edit/{id_user}', [CustomerController::class, 'edit'])->name('admin.editcustomer');
        Route::put('/update/{id_user}', [CustomerController::class, 'update'])->name('admin.updatecustomer');
        Route::get('/delete/{id_user}', [CustomerController::class, 'destroy'])->name('admin.deletecustomer');
    });

    // Rooms
    Route::prefix('rooms')->group(function () {
        Route::get('/', [RoomController::class, 'index'])->name('admin.rooms');
        Route::get('/create', [RoomController::class, 'create'])->name('admin.createroom');
        Route::post('/store', [RoomController::class, 'store'])->name('admin.storeroom');
        Route::get('/edit/{id_kamar}', [RoomController::class, 'edit'])->name('admin.editroom');
        Route::post('/update/{id_kamar}', [RoomController::class, 'update'])->name('admin.updateroom');
        Route::get('/delete/{id_kamar}', [RoomController::class, 'destroy'])->name('admin.deleteroom');
    });

    // Transaksi
    Route::prefix('transaksi')->group(function () {
        Route::get('/', [TransaksiController::class, 'index'])->name('admin.transaksi');
        Route::get('/create', [TransaksiController::class, 'create'])->name('admin.createtransaksi');
        Route::post('/store', [TransaksiController::class, 'store'])->name('admin.storetransaksi');
        Route::get('/edit/{id_transaksi}', [TransaksiController::class, 'edit'])->name('admin.edittransaksi');
        Route::post('/update/{id_transaksi}', [TransaksiController::class, 'update'])->name('admin.updatetransaksi');
        Route::get('/delete/{id_transaksi}', [TransaksiController::class, 'destroy'])->name('admin.deletetransaksi');
    });

    // Booking
    Route::prefix('booking')->group(function () {
        Route::get('/', [BookingController::class, 'index'])->name('admin.booking');
        Route::get('/create', [BookingController::class, 'create'])->name('admin.createbooking');
        Route::post('/store', [BookingController::class, 'store'])->name('admin.storebooking');
        Route::get('/edit/{id_booking}', [BookingController::class, 'edit'])->name('admin.editbooking');
        Route::put('/update/{id_booking}', [BookingController::class, 'update'])->name('admin.updatebooking');
        Route::get('/delete/{id_booking}', [BookingController::class, 'destroy'])->name('admin.deletebooking');
        Route::put('/confirm/{id_booking}', [BookingController::class, 'confirm'])->name('admin.bookingconfirm');
        Route::put('/cancel/{id_booking}', [BookingController::class, 'cancel'])->name('admin.bookingcancel');
        Route::put('/admin/booking/perpanjangan/{id}/approve', [BookingController::class, 'approvePerpanjangan'])->name('admin.approvePerpanjangan');
        Route::get('/admin/booking/selesai/{id_booking}', [BookingController::class, 'markAsCompleted'])->name('admin.bookingselesai');
    });

    // Expense
    Route::prefix('expense')->group(function () {
        Route::get('/', [ExpenseController::class, 'index'])->name('admin.expense');
        Route::get('/create', [ExpenseController::class, 'create'])->name('admin.createexpense');
        Route::post('/store', [ExpenseController::class, 'store'])->name('admin.storeexpense');
        Route::get('/edit/{id_pengeluaran}', [ExpenseController::class, 'edit'])->name('admin.editexpense');
        Route::post('/update/{id_pengeluaran}', [ExpenseController::class, 'update'])->name('admin.updateexpense');
        Route::get('/delete/{id_pengeluaran}', [ExpenseController::class, 'destroy'])->name('admin.deleteexpense');
    });

    // Keuangan
    Route::get('/keuangan', [KeuanganController::class, 'index'])->name('admin.keuangan');
});


// ===============================
// Route untuk Pemilik
// ===============================
Route::middleware(['auth', 'roles:pemilik'])->group(function () {
    Route::get('/dashboard_pemilik', [DashboardPemilikController::class, 'index'])->name('pemilik.dashboard');
    Route::get('/laporan', [LaporanController::class, 'index'])->name('pemilik.laporan');
});
