<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Models\Transaksi;
use App\Models\User;
use App\Models\Room;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function showProfile()
    {
        return view('users.profile');
    }

    public function editProfile()
    {
        return view('users.edit-profile');
    }

    public function updateProfile(Request $request)
    {
        /** @var \App\Models\User $user */
        $user = Auth::user();

        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255|unique:users,email,' . $user->id_user . ',id_user',
            'password' => 'nullable|string|min:6',
        ]);

        $user->name = $request->name;
        $user->email = $request->email;
        if ($request->password) {
            $user->password = Hash::make($request->password);
        }
        $user->save();

        return redirect()->route('users.profile')->with('success', 'Profil berhasil diperbarui.');
    }

    public function detailPesanan($id_booking)
    {
        // Ambil data booking beserta relasi room
        $booking = Booking::with('room')->findOrFail($id_booking);

        // Ambil transaksi pembayaran berdasarkan id_booking
        $pembayaran = Transaksi::where('id_booking', $booking->id_booking)
            ->orderBy('id_transaksi', 'desc') // Gunakan 'id_transaksi' sesuai struktur tabel
            ->first();

        return view('users.detail-pesanan', compact('booking', 'pembayaran'));
    }

    public function showDetailPesanan($id_booking)
    {
        $booking = Booking::with('room')->findOrFail($id_booking); // Pastikan ada relasi room jika dibutuhkan
        $pembayaran = Transaksi::where('id_booking', $id_booking)
            ->orderBy('id_transaksi', 'desc')
            ->first(); // Ambil data pembayaran terbaru

        return view('users.detail-pesanan', compact('booking', 'pembayaran'));
    }

    // Menampilkan halaman form persetujuan dengan pengecekan
    public function showApprovalForm()
    {
        $user = auth()->user();

        if ($user->rental_approved) {
            return redirect()->route('users.peta')->with('info', 'Anda sudah menyetujui persyaratan penyewaan.');
        }

        return view('users.approve_rental');
    }

    // Memproses form persetujuan dengan pengecekan
    public function approveRental(Request $request)
{
    /** @var \App\Models\User $user */
    $user = Auth::user();

    if ($user->rental_approved) {
        return redirect()->route('users.peta')->with('info', 'Anda sudah menyetujui persyaratan penyewaan.');
    }

    $request->validate([
        'id_card' => 'required|mimes:jpg,jpeg,png,pdf|max:2048',
        'agree' => 'required',
    ]);

    // Simpan file Kartu Identitas ke public/assets/users
    if ($request->hasFile('id_card')) {
        $file = $request->file('id_card');
        $filename = time() . '_' . $file->getClientOriginalName();

        // Pindahkan ke public/assets/users
        $file->move(public_path('assets/users'), $filename);

        // Simpan hanya nama file
        $user->id_card_path = $filename;
    }

    // Update status persetujuan
    $user->rental_approved = 1;
    $user->save();

    return redirect()->route('users.peta')->with('success', 'Persetujuan berhasil disimpan. Anda sekarang bisa melakukan penyewaan.');
}

    public function showMap()
{
    $user = auth()->user();

    if (!$user->rental_approved) {
        return redirect()->route('user.showApprovalForm')->with('warning', 'Silakan menyetujui persyaratan terlebih dahulu.');
    }

    // Ambil semua kamar
    $rooms = Room::whereIn('status', ['tersedia', 'terisi', 'maintenance'])->get();

    // Ambil booking user (jika ada)
    $booking = Booking::where('id_user', $user->id_user)
                  ->whereIn('status_booking', ['pending', 'dikonfirmasi'])
                  ->orderBy('tanggal_booking', 'desc')
                  ->first();

    return view('users.peta', compact('rooms', 'booking'));
}

public function showDashboard()
{
    $user = auth()->user();

    if (!$user->rental_approved) {
        return redirect()->route('user.showApprovalForm')->with('warning', 'Silakan menyetujui persyaratan terlebih dahulu.');
    }

    // Jika sudah approve, arahkan ke peta
    return redirect()->route('users.peta');
}


}
