<?php

namespace App\Livewire\Shop;

use App\Models\Reservation;
use Livewire\Component;

class Bookings extends Component
{

    public $bookings;
    public $dateFormat;
    public $timeFormat;

    public function mount()
    {
        if (is_null(customer())) {

            if (module_enabled('Subdomain')) {
                return $this->redirect('/');
            }

            return $this->redirect(route('home'));
        }

        $this->bookings = Reservation::with('customer', 'table', 'branch.restaurant')->where('customer_id', customer()->id)
            ->orderBy('id', 'desc')
            ->get();

        // Get restaurant from first booking's branch or use shop() helper
        $restaurant = null;
        if ($this->bookings->isNotEmpty() && $this->bookings->first()->branch) {
            $restaurant = $this->bookings->first()->branch->restaurant;
        } else {
            $restaurant = shop();
        }

        // Set date and time formats
        $this->dateFormat = $restaurant?->date_format ?? dateFormat();
        $this->timeFormat = $restaurant?->time_format ?? timeFormat();
    }

    public function render()
    {
        return view('livewire.shop.bookings');
    }
}
