<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Persistence\InMemory;

use App\Administration\Domain\Exception\ImportBatchNotFoundException;
use App\Administration\Domain\Model\Import\ImportBatchId;
use App\Administration\Domain\Model\Import\TeacherImportBatch;
use App\Administration\Domain\Repository\TeacherImportBatchRepository;
use App\Shared\Domain\Tenant\TenantId;
use Override;

final class InMemoryTeacherImportBatchRepository implements TeacherImportBatchRepository
{
    /** @var array<string, TeacherImportBatch> */
    private array $byId = [];

    #[Override]
    public function save(TeacherImportBatch $batch): void
    {
        $this->byId[$batch->id->__toString()] = $batch;
    }

    #[Override]
    public function get(ImportBatchId $id): TeacherImportBatch
    {
        return $this->findById($id) ?? throw ImportBatchNotFoundException::withId($id);
    }

    #[Override]
    public function findById(ImportBatchId $id): ?TeacherImportBatch
    {
        return $this->byId[$id->__toString()] ?? null;
    }

    #[Override]
    public function findByTenant(TenantId $tenantId): array
    {
        return array_values(array_filter(
            $this->byId,
            static fn (TeacherImportBatch $batch): bool => $batch->tenantId->equals($tenantId),
        ));
    }
}
