<?php
    namespace App\Utils;
    
    use Psr\Log\LoggerInterface;
    use Symfony\Component\Filesystem\Filesystem;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
    use Symfony\Component\Filesystem\Path;

    class ImageFixturesHelper {

        public function __construct(private LoggerInterface $logger) {
            
        }

        public function prepareUploadedFile(string $fileName, string $typeFile) : UploadedFile {

            $src = __DIR__."/../../fixtures/$typeFile/".$fileName;

            $this->logger->info("= [fixtures/uploaded file] = $src");

            $tmp_src = Path::normalize(sys_get_temp_dir().'/'.random_int(0, 1000).'_'.$fileName);

            $extension = Path::getExtension($fileName);
            $typeMime = "xxx";

            // dump("$fileName / extension = " . $extension);

            switch ($extension) {
                case 'png':
                    $typeMime = 'image/png';
                    break;
                case 'jpg':
                case 'jpeg':
                    $typeMime = 'image/png';
                    break;
                
                default:
                    throw new \Exception("Extension $extension non gérée");
                    # code...
                    // break;
            }

            // $this->logger->info("= [fixtures/uploaded file] = tmp_src : $tmp_src");
            // $this->logger->info("= [fixtures/uploaded file] = mime : $typeMime");

            $filesystem = new Filesystem();
            $filesystem->copy($src, $tmp_src);

            $img = new UploadedFile(
                $tmp_src,
                $fileName,
                $typeMime,
                null,
                true //  Set test mode true !!! " Local files are used in test mode hence the code should not enforce HTTP uploads."
            );

            return $img;

        }


    }