<?php

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Site;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Manager\SocialNetworkProviderManager;
use CpCreation\VitiCore\Library\Repository\LibraryRepository;
use CpCreation\VitiCore\SocialNetwork\Model\SocialNetwork;
use CpCreation\VitiCore\SocialNetwork\Repository\SocialNetworkNewRepository;
use CpCreation\VitiCore\SocialNetwork\Repository\SocialNetworkRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

final class LibraryController extends AbstractController
{
    public function __construct(
        private readonly LibraryRepository $libraryRepository,
        private readonly SocialNetworkRepository $socialNetworkRepository,
        private readonly SocialNetworkNewRepository $socialNetworkNewRepository,
        private readonly SocialNetworkProviderManager $socialNetworkProviderManager,
    ) {
    }

    /**
     * @throws \Exception
     */
    public function index(): Response
    {
        $libraries = $this->libraryRepository->findBy([], ['position' => 'ASC']);

        /** @var SocialNetwork|null $social */
        $social = $this->socialNetworkRepository->findByLikeSocial('instagram')
            ?? $this->socialNetworkNewRepository->findByLikeSocial('instagram');

        if ($social) {
            $medias = $this->socialNetworkProviderManager->getMedias($social->getNetwork(), 12);
            $variable = $social->getNetwork();
        } else {
            $medias = [];
            $variable = 'instagram';
        }

        return $this->render('library.html.twig', [
            'classMenu' => 'black',
            'libraries' => $libraries,
            $variable => $medias,
        ]);
    }
}
