<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Slot;
use Carbon\Carbon;

class CalendarController extends Controller
{
    public function index(Request $request)
    {
        $now = Carbon::now('Europe/London');

        // Default: this Mon–Sun, but flip to next week after Fri 15:00
        $start = $now->copy()->startOfWeek(Carbon::MONDAY);
        $fridayCutoff = $start->copy()->addDays(4)->setTime(15, 0);
        if ($now->greaterThanOrEqualTo($fridayCutoff)) {
            $start->addWeek();
        }

        // Week navigation
        $weekOffset = (int) $request->query('week', 0);
        if ($weekOffset !== 0) {
            $start->addWeeks($weekOffset);
        }
        $end = $start->copy()->addDays(6);

        // Load slots + bookings for this visible week
        $slots = Slot::with(['bookings' => function ($q) use ($start, $end) {
            $q->whereBetween('date', [$start->toDateString(), $end->toDateString()]);
        }])->get();

        // Reuse the same view for now (we'll hide admin-only bits next)
        return view('calendar.index', [
            'start'      => $start,
            'end'        => $end,
            'weekOffset' => $weekOffset,
            'slots'      => $slots,
            'now'        => $now,
			'viewer'     => 'student',
        ]);
    }

}
