<?php

namespace App\DataFixtures;

use App\Entity\MemberCase;
use App\Entity\ReferralSource;
use App\Entity\User;
use App\Entity\UserCase;
use App\Enums\CaseLevel;
use App\Enums\County;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

class MemberCaseFixture extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        if(!$_ENV['CREATE_CASES']) {
            return;
        }
        $sources = $manager->getRepository(ReferralSource::class)->findAll();
        $users = $manager->getRepository(User::class)->findAll();

        $gen = \Faker\Factory::create();

        for ($x = 0; $x < $_ENV['CREATE_CASES']; $x++) {
            $case = new MemberCase();
            $case->setCaseNumber($gen->numberBetween(1000000, 9999999))
                 ->setDcsCaseId($gen->numberBetween(1000000, 9999999))
                 ->setFirstName($gen->firstName())
                 ->setLastName($gen->lastName())
                 ->setCounty($gen->randomElement(County::class))
                 ->setReferralSource($gen->randomElement($sources))
                 ->setLevel($gen->randomElement(CaseLevel::class))
                 ->setAdmitDate($gen->dateTimeBetween('-1 year', 'now'))
                 ->setReferralType('DCS')
            ;

            $uc = new UserCase();
            $uc->setMemberCase($case);
            $uc->setUser($gen->randomElement($users));

            print "Adding member case {$case->getCaseNumber()}".PHP_EOL;

            $manager->persist($case);
            $manager->persist($uc);
        }

        $manager->flush();
    }

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