<?php

namespace App\Factory;

use App\Entity\Posting;
use App\Repository\PostingRepository;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryProxy;

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

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
     *
     * @todo add your default values here
     */
    protected function getDefaults(): array
    {
        $randomBoolean = ContactFactory::count() > 0 && (bool)random_int(0, 1);
        $random4 = SaleFactory::count() > 0 && !(bool)random_int(0, 3);
        return [
            'headline' => self::faker()->words(random_int(1, 5), true),
            'message' => $randomBoolean ? self::faker()->sentence() : self::faker()->sentences(random_int(1, 3), true),
            'owner' => UserFactory::random(),
            'partner' => PartnerFactory::random(),
            'contact' => $randomBoolean ? ContactFactory::random() : null,
            'sale' => !$random4 ? SaleFactory::random() : null,
            'product' => ProductFactory::random()
        ];
    }

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

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