<?php

/*
 * This file is part of the Serenity package.
 *
 * (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\Bridge\Symfony\Bundle\Controller\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider\SocialNetwork\BasicInstagramProvider;
use CpCreation\VitiCore\SocialNetwork\Model\SocialNetworkNew;
use CpCreation\VitiCore\SocialNetwork\Repository\SocialNetworkNewRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/social-network-new')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_SOCIAL')"))]
final class SocialNetworkNewController extends AbstractController
{
    public function __construct(
        private readonly BasicInstagramProvider $basicInstagramProvider,
        private readonly SocialNetworkNewRepository $socialNetworkNewRepository,
    ) {
    }

    #[Route('', name: 'admin_socialNew')]
    public function index(Request $request): Response
    {
        $connectId = $request->query->get('connectId');
        $username = $request->query->get('username');
        $result = $request->query->get('result');
        $type = $request->query->get('type');

        $socialNetwork = null;
        $errorCode = null;
        $errorMessage = null;

        if (!$result) {
            $socialNetwork = $this->socialNetworkNewRepository->findByLikeSocial('instagram');
        } else {
            if ('success' === $result) {
                $socialNetwork = $this->socialNetworkNewRepository->findByConnectId((string) $connectId);

                if (!$socialNetwork) {
                    $socialNetwork = new SocialNetworkNew();
                }

                $socialNetwork->setConnectId((string) $connectId);
                $socialNetwork->setUsername((string) $username);
                $socialNetwork->setNetwork((string) $type);

                $this->socialNetworkNewRepository->save($socialNetwork);
            } else {
                $errorCode = $request->query->get('error_code');
                $errorMessage = $request->query->get('error_message');
            }
        }

        return $this->render('@CpCreationVitiCore/admin/socialNew/index.html.twig', [
            'socialNetwork' => $socialNetwork,
            'errorCode' => $errorCode,
            'errorMessage' => $errorMessage,
            'result' => $result,
        ]);
    }

    #[Route('/{id}/delete', name: 'admin_socialNew_delete')]
    public function deleteTokenInstagram(SocialNetworkNew $socialNetworkNew): Response
    {
        $this->socialNetworkNewRepository->removeInstagram($socialNetworkNew);
        $this->addFlash('alert-success', 'Votre compte instagram a bien été supprimé de nos bases de données');

        return $this->redirectToRoute('admin_socialNew');
    }

    #[Route('/{id}/refresh', name: 'admin_socialNew_refresh')]
    public function refreshTokenInstagram(SocialNetworkNew $socialNetworkNew): Response
    {
        $this->socialNetworkNewRepository->removeInstagram($socialNetworkNew);
        $this->addFlash('alert-success', 'Votre compte instagram a bien été supprimé de nos bases de données');

        return $this->redirectToRoute('admin_socialNew');
    }

    #[Route('/instagram/login', name: 'admin_socialNew_instagram_login')]
    public function instagramLogin(Request $request): RedirectResponse
    {
        return new RedirectResponse($this->basicInstagramProvider->loginUrl($request));
    }
}
