<?php

namespace App\DataFixtures;

use App\Entity\MemberCase;
use App\Entity\Referral;
use App\Enums\ReferralServiceType;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

class ReferralFixture extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        if(!$_ENV['CREATE_REFERRALS']) {
            return;
        }
        $gen = \Faker\Factory::create();
        $cases = $manager->getRepository(MemberCase::class)->findAll();

        foreach ($cases as $case) {
            print "Adding referrals for case {$case->getCaseNumber()}".PHP_EOL;
            $refCount = $gen->numberBetween(0, $_ENV['CREATE_REFERRALS']);
            for ($x = 0; $x < $refCount; $x++) {
                $ref = new Referral();
                $ref->setReferralId($gen->numberBetween(1000000, 9999999))
                    ->setMemberCase($case)
                    ->setServiceCode($gen->randomElement(ReferralServiceType::class))
                    ->setEndDate($gen->dateTimeBetween('+1 month', '+1 year'))
                    ->setHours($gen->numberBetween(1, 75))
                ;

                print "Adding referral {$ref->getReferralId()}".PHP_EOL;

                $manager->persist($ref);
            }
        }

        $manager->flush();
    }

    public function getDependencies(): array
    {
        return [
            MemberCaseFixture::class,
        ];
    }
}
