<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Models\Space;
use App\Enums\BookingStatus;

class CronJobController extends Controller
{
    public function bookingExpire()
    {
        $bookings = Booking::where('status', BookingStatus::Booked)->where('booking_to', '<', now())->get();

        foreach ($bookings as $booking) {
            $space = Space::where('id', $booking->space_id)->first();
            $booking->update([
                'status' => 'completed',
            ]);
            $space->update([
                'stock' => $space->stock + $booking->stock,
            ]);
        }
        return '....cron job successfully completed';
    }
}