<?php

namespace App\Domain\Setting\Infrastructure\Persistence\Repository;

use App\Domain\Setting\Domain\Contract\Repository\ContentSourceRepositoryInterface;
use App\Domain\Setting\Domain\Model\ContentSource;
use App\Domain\Setting\Infrastructure\Persistence\Mapper\ContentSourceMapper;
use App\Entity\ContentSource as ContentSourceEntity;
use Doctrine\ORM\EntityManagerInterface;

readonly class DoctrineContentSourceRepository implements ContentSourceRepositoryInterface
{
    public function __construct(
        private EntityManagerInterface $entityManager,
        private ContentSourceMapper $mapper
    ) {
    }

    public function findAll(): array
    {
        $entities = $this->entityManager->getRepository(ContentSourceEntity::class)->findAll();

        return array_map(
            fn (ContentSourceEntity $entity) => $this->mapper->toDomain($entity),
            $entities
        );
    }

    public function findById(int $id): ?ContentSource
    {
        $entity = $this->entityManager->find(ContentSourceEntity::class, $id);

        return $entity ? $this->mapper->toDomain($entity) : null;
    }

    public function findByBaseUrl(string $baseUrl): ?ContentSource
    {
        $entity = $this->entityManager->getRepository(ContentSourceEntity::class)
            ->findOneBy(['baseUrl' => $baseUrl]);

        return $entity ? $this->mapper->toDomain($entity) : null;
    }

    public function save(ContentSource $contentSource): void
    {
        if ($contentSource->getId()) {
            // Update existing entity
            $entity = $this->entityManager->find(ContentSourceEntity::class, $contentSource->getId());
            if ($entity) {
                // Update existing entity with new values
                $this->mapper->updateEntity($entity, $contentSource);
                $this->entityManager->flush();
            }
        } else {
            // Create new entity
            $entity = $this->mapper->toEntity($contentSource);
            $this->entityManager->persist($entity);
            $this->entityManager->flush();

            // Met à jour l'ID du modèle du domaine
            if ($entity->getId()) {
                $contentSource->updateId($entity->getId());
            }
        }
    }

    public function delete(ContentSource $contentSource): void
    {
        if ($contentSource->getId()) {
            $entity = $this->entityManager->find(ContentSourceEntity::class, $contentSource->getId());
            if ($entity) {
                $this->entityManager->remove($entity);
                $this->entityManager->flush();
            }
        }
    }

    public function deleteAll(): void
    {
        $this->entityManager->createQuery('DELETE FROM ' . ContentSourceEntity::class)->execute();
    }

    public function saveMultiple(array $contentSources): void
    {
        foreach ($contentSources as $contentSource) {
            $entity = $this->mapper->toEntity($contentSource);
            $this->entityManager->persist($entity);
        }

        $this->entityManager->flush();
    }
}
