<?php

namespace App\Factory;

use App\Entity\MediaObject;
use Symfony\Component\HttpKernel\KernelInterface;
use Vich\UploaderBundle\FileAbstraction\ReplacingFile;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

/**
 * @extends PersistentProxyObjectFactory<MediaObject>
 */
final class MediaObjectFactory extends PersistentProxyObjectFactory
{
    /**
     * @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();
    }

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

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

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

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

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

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