<?php

namespace App\Factory;

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

/**
 * @extends ModelFactory<DocumentObject>
 *
 * @method        DocumentObject|Proxy             create(array|callable $attributes = [])
 * @method static DocumentObject|Proxy             createOne(array $attributes = [])
 * @method static DocumentObject|Proxy             find(object|array|mixed $criteria)
 * @method static DocumentObject|Proxy             findOrCreate(array $attributes)
 * @method static DocumentObject|Proxy             first(string $sortedField = 'id')
 * @method static DocumentObject|Proxy             last(string $sortedField = 'id')
 * @method static DocumentObject|Proxy             random(array $attributes = [])
 * @method static DocumentObject|Proxy             randomOrCreate(array $attributes = [])
 * @method static EntityRepository|RepositoryProxy repository()
 * @method static DocumentObject[]|Proxy[]         all()
 * @method static DocumentObject[]|Proxy[]         createMany(int $number, array|callable $attributes = [])
 * @method static DocumentObject[]|Proxy[]         createSequence(iterable|callable $sequence)
 * @method static DocumentObject[]|Proxy[]         findBy(array $attributes)
 * @method static DocumentObject[]|Proxy[]         randomRange(int $min, int $max, array $attributes = [])
 * @method static DocumentObject[]|Proxy[]         randomSet(int $number, array $attributes = [])
 */
final class DocumentObjectFactory 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/documents/';
        $files = glob($folderPath . '*.*');
        $randomFile = null;
        if ($files !== false && count($files) > 0) {
            $randomFile = $files[array_rand($files)];
        }

        $randBool = (bool)random_int(0, 1);
        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(DocumentObject $documentObject): void {})
        ;
    }

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