<?php

namespace App\Controller\OgRepository\Image;

use App\Controller\OgRepository\AbstractOgRepositoryController;
use App\Entity\Image;
use App\Entity\ImageImageRepository;
use App\Entity\ImageRepository;
use App\Model\ImageStatus;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

#[AsController]
class SyncAction extends AbstractOgRepositoryController
{
    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function __invoke(ImageRepository $input): JsonResponse
    {
        $content = $this->createRequest('GET', 'http://' . $input->getIp() . ':8006/ogrepository/v1/images');

        if (!isset($content['output']['REPOSITORY']['images'])) {
            return new JsonResponse(data: 'No images found', status: Response::HTTP_NOT_FOUND);
        }

        $repository = $this->entityManager->getRepository(ImageImageRepository::class);

        $existingImages = $repository->findBy(['repository' => $input]);

        $newImageFullsums = array_column($content['output']['REPOSITORY']['images'], 'fullsum');

        foreach ($content['output']['REPOSITORY']['images'] as $image) {
            $imageImageRepositoryEntity = $repository->findOneBy([
                'imageFullsum' => $image['fullsum'],
                'repository' => $input
            ]);

            $imageEntity = $this->entityManager->getRepository(Image::class)->findOneBy(['name' => $image['name']]);

            if (!$imageEntity) {
                $imageEntity = new Image();
                $imageEntity->setName($image['name']);
                $imageEntity->setRemotePc(false);
                $imageEntity->setIsGlobal(false);
                $imageEntity->setType('monolithic');

                $this->entityManager->persist($imageEntity);
            }

            if (!$imageImageRepositoryEntity) {
                $imageImageRepositoryEntity = new ImageImageRepository();
            }

            $imageImageRepositoryEntity->setName($image['name']);
            $imageImageRepositoryEntity->setImageFullsum($image['fullsum']);
            $imageImageRepositoryEntity->setDatasize($image['datasize']);
            $imageImageRepositoryEntity->setStatus(ImageStatus::SUCCESS);
            $imageImageRepositoryEntity->setImage($imageEntity);
            $imageImageRepositoryEntity->setRepository($input);

            $this->entityManager->persist($imageImageRepositoryEntity);

        }

        foreach ($existingImages as $existingImage) {
            if (!in_array($existingImage->getImageFullsum(), $newImageFullsums)) {
                $this->entityManager->remove($existingImage);
            }
        }

        $this->entityManager->flush();

        return new JsonResponse(data: $content, status: Response::HTTP_OK);
    }

}