<?php

namespace App\DataFixtures;

use App\Entity\Member;
use App\Entity\MemberCase;
use App\Enums\GenderType;
use App\Enums\RaceType;
use App\Enums\RelationshipType;
use App\Enums\State;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

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

        foreach ($cases as $case) {
            /** @var MemberCase $case */
            $caseLastName = $case->getLastName();
            print "Adding members for case {$case->getCaseName()}".PHP_EOL;
            $memCount = $gen->numberBetween(0, $_ENV['CREATE_MEMBERS']);
            for ($x = 0; $x < $memCount; $x++) {
                $isChild = $gen->boolean();
                $isParent = ($isChild ? false : $gen->boolean());
                $isLegalGuardian = (!$isParent && !$isChild ? $gen->boolean() : false);
                $dcsApproved = (!$isParent && !$isChild ? $gen->boolean() : false);
                $sameLastName = $gen->boolean();
                $mem = new Member();
                $name = $gen->firstName().' '.$gen->lastName();
                $username = strtolower(str_replace(' ', '.', $name));
                $phone = substr(str_replace(['-', '+', ' ', '(', ')'], '', $gen->e164PhoneNumber()), -10);

                $mem->setRelationship($gen->randomElement(RelationshipType::class))
                    ->setPersonalId($gen->numberBetween(1000000, 9999999))
                    ->setGender($gen->randomElement(GenderType::class))
                    ->setRace($gen->randomElement(RaceType::class))
                    ->setPhone($phone)
                    ->setCellPhone($phone)
                    ->setEmail($username.'@'.$gen->freeEmailDomain())
                    ->setAddress($gen->streetAddress())
                    ->setCity($gen->city())
                    ->setState(State::IN->name)
                    ->setZip($gen->postcode())
                    ->setDob(
                        $gen->dateTimeBetween(
                            ($isChild ? '-16 years' : '-50 years'),
                            ($isParent || $isLegalGuardian ? '-16 years' : '-1 month')
                        )
                    )
                    ->setFirstName($gen->firstName())
                    ->setLastName($sameLastName ? $caseLastName : $gen->lastName())
                    ->setChild($isChild)
                    ->setParent($isParent)
                    ->setLegalGuardian($isLegalGuardian)
                    ->setAdultChild(false)
                    ->setParentsLiveTogether(false)
                    ->setDcsApproved($dcsApproved)
                    ->setCaseId($case)
                ;

                print "Adding member {$mem->getFirstName()} {$mem->getLastName()}".PHP_EOL;

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

        $manager->flush();
    }

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