<?php

namespace App\Http\Controllers\Bookings;

use App\Http\Controllers\Controller;
use App\Models\Room;
use App\Models\RoomAvailability;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Inertia\Inertia;

class BookingCalendarController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    // public function index()
    // {
    //     return Inertia::render('dashboard/booking-calendar/index');
    // }
    public function index(Request $request)
    {
        $rooms = Room::query()->select(['id', 'room_number'])->get();

        $activeRoomId = $request->room_id ?? $rooms->first()->id;
        $availability = RoomAvailability::query()->forRoom($activeRoomId)->latest()->paginate(10)->withQueryString();

        $days = 20;
        $startDate = Carbon::parse($request->start_date ?? now()->startOfDay());
        $endDate = $startDate->copy()->addDays($days - 1);

        $calendar_items = RoomAvailability::query()->whereBetween('start_date', [$startDate, $endDate])->orWhereBetween('end_date', [$startDate, $endDate])->get();

        return Inertia::render('dashboard/booking-calendar/index', [
            'rooms' => $rooms,
            'activeRoomId' => (int) $activeRoomId,
            'availability' => $availability,
            'days' => $days,
            'startDate' => $startDate->toDateString(),
            'calendar_items' => $calendar_items,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
