<?php

namespace App\Services\Mail;

/**
 * A mailbox that answers with whatever a test put in it.
 *
 * The socket half of ImapMailbox cannot be exercised without a mail server, and
 * a test that needs one is a test nobody runs. What CAN be exercised — and is,
 * in tests/Feature/Admin/InboundMailTest.php — is the parsing (MailParser
 * against real raw messages) and everything the ingest decides afterwards.
 */
class FakeMailbox implements InboundMailbox
{
    public bool $configured = true;

    /** @var array<int, FetchedMail> */
    public array $mails = [];

    /** @var array<int, string> */
    public array $seen = [];

    public function isConfigured(): bool
    {
        return $this->configured;
    }

    /**
     * What a test wants the check to answer, if not the obvious.
     *
     * @var array{ok: bool, message: string, unseen: int|null}|null
     */
    public array|null $checkAnswer = null;

    public function check(): array
    {
        return $this->checkAnswer ?? [
            'ok' => $this->configured,
            'message' => $this->configured ? '' : 'not configured',
            'unseen' => $this->configured ? count($this->mails) : null,
        ];
    }

    public function fetch(int $limit = 50): array
    {
        return array_slice($this->mails, 0, $limit);
    }

    public function markSeen(string $uid): void
    {
        $this->seen[] = $uid;
    }
}
