<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BookingsListController extends Controller
{
	public function index()
	{
		// Ensure these use's exist at the top of the file:
		// use App\Models\Booking;
		// use Carbon\Carbon;

		$user  = auth()->user();
		$name  = $user?->name ?? '';
		$now   = \Carbon\Carbon::now('Europe/London');
		$today = $now->toDateString();

		// Upcoming “booked” sessions for this student (names-based for now)
		$raw = \App\Models\Booking::with('slot')
			->where('student_name', $name)
			->where('status', 'booked')
			->whereDate('date', '>=', $today)
			->orderBy('date')
			->get();

		// Pre-format labels so Blade stays Carbon-free
		$bookings = $raw->map(function ($b) use ($now) {
			$when = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $b->date.' '.$b->slot->time, $now->timezone);
			return [
				'id'         => $b->id,
				'date'       => $b->date,
				'prettyDate' => $when->format('D d M Y'),
				'prettyTime' => $when->format('H:i'),
				'weekday'    => $when->format('l'),
				'status'     => $b->status,
				'slot_id'    => $b->slot_id,
			];
		});

		return view('student.bookings.index', [
			'bookings' => $bookings,
		]);
	}
}
