<?php

namespace App\Console\Commands;

use App\Models\Room;
use App\Models\User;
use App\Services\Meet\MeetNotificationService;
use Illuminate\Console\Command;

class SendMeetingReminders extends Command
{
    protected $signature = 'meet:send-reminders';

    protected $description = 'Send reminders for meetings starting within the next 15 minutes';

    public function handle(MeetNotificationService $notifications): int
    {
        $rooms = Room::where('status', 'scheduled')
            ->whereBetween('scheduled_at', [now(), now()->addMinutes(15)])
            ->whereNull('deleted_at')
            ->get();

        foreach ($rooms as $room) {
            $host = User::where('public_id', $room->host_user_ref)->first();
            if ($host) {
                $notifications->sendReminder($room, $host);
            }
        }

        $this->info('Sent '.$rooms->count().' reminder(s).');

        return self::SUCCESS;
    }
}
