<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Facades\Settings;
use App\Jobs\SendSiteAccessLinkJob;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Mtc\MercuryDataModels\SiteAccessRequest;

class SendSiteAccessLinks extends Command
{
    use DispatchesJobs;

    protected $signature = 'site-access:send-pending-links';

    protected $description = 'Dispatch magic-link emails for pending site access requests';

    public function handle(): int
    {
        if (!Settings::get('site-access-enabled') || Settings::get('site-access-mode') !== 'email-request') {
            return self::SUCCESS;
        }

        SiteAccessRequest::query()
            ->pendingSend()
            ->pluck('id')
            ->each(fn (int $id) => $this->dispatch(new SendSiteAccessLinkJob($id)));

        return self::SUCCESS;
    }
}
