<?php

declare(strict_types=1);

namespace App\SuperAdmin\Infrastructure\Persistence\InMemory;

use App\SuperAdmin\Domain\Exception\SuperAdminIntrouvableException;
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdmin;
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdminId;
use App\SuperAdmin\Domain\Repository\SuperAdminRepository;
use Override;

final class InMemorySuperAdminRepository implements SuperAdminRepository
{
    /** @var array<string, SuperAdmin> */
    private array $superAdmins = [];

    #[Override]
    public function save(SuperAdmin $superAdmin): void
    {
        $this->superAdmins[(string) $superAdmin->id] = $superAdmin;
    }

    #[Override]
    public function get(SuperAdminId $id): SuperAdmin
    {
        return $this->superAdmins[(string) $id]
            ?? throw SuperAdminIntrouvableException::avecId($id);
    }

    #[Override]
    public function findByEmail(string $email): ?SuperAdmin
    {
        foreach ($this->superAdmins as $superAdmin) {
            if ($superAdmin->email === $email) {
                return $superAdmin;
            }
        }

        return null;
    }
}
