<?php

namespace App\Http\Controllers\Ops;

use App\Http\Controllers\Controller;
use App\Models\Floorplan;
use App\Models\Reservation;
use Illuminate\Http\Request;

class OpsFloorplanController extends Controller
{
    public function show(Request $request)
    {
        // Selected date (default: today)
        $date = $request->query('date', now()->toDateString());

        // Get first floorplan + tables
        $floorplan = Floorplan::with('tables')->firstOrFail();

        // Get reservations for date + attached tables
        $reservations = Reservation::with('tables')
            ->whereDate('date', $date)
            ->orderBy('expected_arrival_time')
            ->get()
            ->map(function ($r) {
                return [
                    'id' => $r->id,
                    'guest_name' => $r->guest_name,
                    'phone' => $r->phone,
                    'people_count' => $r->people_count,
                    'expected_arrival_time' => $r->expected_arrival_time,
                    'advance_amount' => $r->advance_amount,
                    'locked_move' => (bool) $r->locked_move,
                    'is_seated' => (bool) $r->is_seated,
                    'table_ids' => $r->tables->pluck('id')->values(),
                ];
            })
            ->values();

        return view('ops.floorplan', [
            'floorplan' => $floorplan,
            'tables' => $floorplan->tables,
            'reservations' => $reservations,
            'date' => $date,
        ]);
    }
}