<?php
declare(strict_types=1);

namespace App\Domain\Media;

use App\Framework\Database\ConnectionInterface;
use App\Framework\Database\EntityManager;
use App\Framework\Database\Transaction;

final readonly class ImageRepository
{
    public function __construct(
        private EntityManager $entityManager,
        private ConnectionInterface $connection,
    ) {}


    public function save(Image $image, string $tempPath): void
    {
        new SaveImageFile()($image, $tempPath);

        $this->entityManager->save($image);
    }

    public function findBySlotName(string $slotName): Image
    {
        return $this->entityManager->findOneBy(ImageSlot::class, ['slot_name' => $slotName])->image;
    }


    public function findById(string $id): ?Image
    {
        return $this->entityManager->find(Image::class, $id);
    }

    public function findByFilename(string $filename): ?Image
    {
        return $this->entityManager->findOneBy(Image::class, ['filename' => $filename]);
    }

    public function findByHash(string $hash): ?Image
    {
        return $this->entityManager->findOneBy(Image::class, ['hash' => $hash]);
    }

    public function findAll(): array
    {
        return $this->entityManager->findAll(Image::class);
    }
}
