<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Persistence\InMemory;

use App\Administration\Domain\Exception\HomeworkRulesNotFoundException;
use App\Administration\Domain\Model\HomeworkRules\HomeworkRules;
use App\Administration\Domain\Repository\HomeworkRulesRepository;
use App\Shared\Domain\Tenant\TenantId;
use Override;

final class InMemoryHomeworkRulesRepository implements HomeworkRulesRepository
{
    /** @var array<string, HomeworkRules> Indexed by tenant_id */
    private array $byTenant = [];

    #[Override]
    public function save(HomeworkRules $homeworkRules): void
    {
        $this->byTenant[(string) $homeworkRules->tenantId] = $homeworkRules;
    }

    #[Override]
    public function get(TenantId $tenantId): HomeworkRules
    {
        $rules = $this->findByTenantId($tenantId);

        if ($rules === null) {
            throw HomeworkRulesNotFoundException::pourTenant($tenantId);
        }

        return $rules;
    }

    #[Override]
    public function findByTenantId(TenantId $tenantId): ?HomeworkRules
    {
        return $this->byTenant[(string) $tenantId] ?? null;
    }
}
