<?php

namespace App\Controller\Backend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Symfony\Component\HttpFoundation\Response;

use App\Service\Frontend\ListmonkMailerService;

use Symfony\Component\Routing\Attribute\Route;

use Psr\Log\LoggerInterface;

use Symfony\Component\Process\Process;

#[Route('/admin/test')]
final class TestController extends AbstractController
{

    public function __construct(
        private readonly ListmonkMailerService $mailer,
    ) {
        
    }

    #[Route('/test1', name: 'app_test_test1')]
    public function test1()
    {
        dd("test1");
    }

    #[Route('/logger', name: 'app_test_logger')]
    public function logger(LoggerInterface $logger): Response
    {
        // Ceci va déclencher l'envoi vers Mattermost si vous êtes en environnement 'prod'
        $logger->error('Bonjour Log Maher Test.', [
            'user' => 'John Doe',
            'code' => 500
        ]);
        
        //composer require symfony/monolog-bundle

        throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException('Csrf non valide Test Test');
        //$logger->info('Test log Symfony');
        //$logger->warning('Test warning');
        //$logger->error('Test error');

        //return new Response('Log envoyé !');
    }


    #[Route('/mailer', name: 'app_test_mailer')]
    public function mailer()
    {
        $result = $this->mailer->send(
            ['maher.tili@gmail.com'],
            7,
            ['name'=> 'Monjie',  'video'=> 'mon happy movie test', 'date'=> '19/06/2026 15:45', 'url'=> 'https://video.powerpme.com/downloder/user/4/'],
            'external',
            ['Content-Type' => 'application/json; charset=utf-8']
        );

        dd($result);
      
        return $this->render('/frontend/test.html.twig', []);
    }

    #[Route('/url', name: 'app_test_url')]
    public function url()
    {
        $process = new Process([
            'yt-dlp',
            '--print',
            '%(title)s|%(filesize,filesize_approx)s',
            '--', // 🛡️ Protection cruciale contre l'injection d'arguments
            'https://www.youtube.com/watch?v=Z8hhP38-4Eg'
        ]);

        $process->run();

        //dd($process->getOutput());
        if (!$process->isSuccessful()) {
            echo $process->getCommandLine() . "<br>" . $process->getErrorOutput();
            die;
            return new Response("error1", Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }
    //
}
