<?php

namespace App\Factory;

use App\Entity\MediaObject;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\KernelInterface;
use Vich\UploaderBundle\FileAbstraction\ReplacingFile;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;

/**
 * @extends ModelFactory<MediaObject>
 *
 * @method        MediaObject|Proxy                create(array|callable $attributes = [])
 * @method static MediaObject|Proxy                createOne(array $attributes = [])
 * @method static MediaObject|Proxy                find(object|array|mixed $criteria)
 * @method static MediaObject|Proxy                findOrCreate(array $attributes)
 * @method static MediaObject|Proxy                first(string $sortedField = 'id')
 * @method static MediaObject|Proxy                last(string $sortedField = 'id')
 * @method static MediaObject|Proxy                random(array $attributes = [])
 * @method static MediaObject|Proxy                randomOrCreate(array $attributes = [])
 * @method static EntityRepository|RepositoryProxy repository()
 * @method static MediaObject[]|Proxy[]            all()
 * @method static MediaObject[]|Proxy[]            createMany(int $number, array|callable $attributes = [])
 * @method static MediaObject[]|Proxy[]            createSequence(iterable|callable $sequence)
 * @method static MediaObject[]|Proxy[]            findBy(array $attributes)
 * @method static MediaObject[]|Proxy[]            randomRange(int $min, int $max, array $attributes = [])
 * @method static MediaObject[]|Proxy[]            randomSet(int $number, array $attributes = [])
 */
final class MediaObjectLogoFactory extends ModelFactory
{
    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
     *
     * @todo inject services if required
     */
    public function __construct(
        private KernelInterface $appKernel
    )
    {
        parent::__construct();
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
     *
     * @todo add your default values here
     */
    protected function getDefaults(): array
    {
        $projectRoot = $this->appKernel->getProjectDir();

        $folderPath = $projectRoot . '/src/DataFixtures/images/logos/';
        $files = glob($folderPath . '*.*');
        $randomFile = null;
        if ($files !== false && count($files) > 0) {
            $randomFile = $files[array_rand($files)];
        }

        return [
            'file' => new ReplacingFile($randomFile),
            'createdBy' => UserFactory::random()
        ];
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
     */
    protected function initialize(): self
    {
        return $this
            // ->afterInstantiate(function(MediaObject $mediaObject): void {})
        ;
    }

    protected static function getClass(): string
    {
        return MediaObject::class;
    }
}
