<?php

declare(strict_types=1);

/*
 * This file is part of the VitiCore 2.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\SocialNetwork\Repository;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Client\ConnectCpCreationClient;
use CpCreation\VitiCore\Repository\BaseRepository;
use CpCreation\VitiCore\SocialNetwork\Model\SocialNetworkNew;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;

class SocialNetworkNewRepository extends BaseRepository
{
    /**
     * @throws \ReflectionException
     */
    public function __construct(
        ManagerRegistry $registry,
    ) {
        parent::__construct($registry, SocialNetworkNew::class);
    }

    /**
     * @throws NonUniqueResultException
     */
    public function findByLikeSocial(string $social): ?SocialNetworkNew
    {
        $qb = $this->createQueryBuilder('socialNetworkNew')
            ->andWhere('socialNetworkNew.network LIKE :social')
            ->setParameter('social', '%'.$social.'%')
            ->orderBy('socialNetworkNew.createdAt', 'DESC')
            ->setMaxResults(1);

        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * @throws NonUniqueResultException
     */
    public function findByConnectId(string $id): ?SocialNetworkNew
    {
        $qb = $this->createQueryBuilder('socialNetworkNew')
            ->andWhere('socialNetworkNew.connectId = :connectId')
            ->setParameter('connectId', $id)
            ->orderBy('socialNetworkNew.createdAt', 'DESC')
            ->setMaxResults(1);

        return $qb->getQuery()->getOneOrNullResult();
    }

    public function removeInstagram(SocialNetworkNew $socialNetworkNew): void
    {
        // $this->connectCpCreationClient->delete('api/client_socials/' . $socialNetworkNew->getConnectId());

        $this->delete($socialNetworkNew);
    }
}
