<?php

namespace App\Console\Commands;

use App\Services\Mail\IngestInboundMail;
use Illuminate\Console\Command;

/**
 * Read the support mailbox into the console.
 *
 * Runs on the scheduler. A mail server has no way to tell us a message arrived
 * — IMAP is a mailbox you look in — so this is polling, and the interval is the
 * honest answer to "how long until I see it".
 */
class FetchInboundMail extends Command
{
    protected $signature = 'clupilot:fetch-mail {--limit=50 : How many messages to take in one run}';

    protected $description = 'Fetch new customer mail from the support mailbox';

    public function handle(IngestInboundMail $ingest): int
    {
        $taken = $ingest((int) $this->option('limit'));

        $this->info($taken === 0 ? 'Nothing new.' : $taken.' new message(s).');

        return self::SUCCESS;
    }
}
