<?php

namespace App\DataFixtures;

use App\Entity\MemberCase;
use App\Entity\Referral;
use App\Entity\StandardNote;
use App\Entity\VisitNote;
use App\Enums\NoteLocation;
use App\Enums\NoteMethod;
use App\Enums\NoteStatus;
use App\Enums\ReferralServiceType;
use App\Enums\VisitQualityLevel;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

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

        foreach ($refs as $ref) {
            /** @var Referral $ref */
            print "Adding notes for referral {$ref->getReferralId()}".PHP_EOL;
            $noteCount = $gen->numberBetween(0, $_ENV['CREATE_NOTES']);

            $case = $ref->getMemberCase();
            $caseMembers = $case->getMembers();

            for ($x = 0; $x < $noteCount; $x++) {
                $startTime = $gen->dateTimeBetween('06:00:00', '14:00:00');
                $endTime = $gen->dateTimeBetween('12:00:00', '21:00:00');

                if ($startTime > $endTime) {
                    $tmp = clone $endTime;
                    $endTime = $startTime;
                    $startTime = $tmp;
                }

                if ($ref->getServiceCode() === ReferralServiceType::VS_THBB) {
                    $note = new VisitNote();
                    $note->setReferral($ref)
                         ->setDate($gen->dateTimeBetween($case->getAdmitDate()->format('Y-m-d'), 'now'))
                         ->setStartTime($startTime)
                         ->setEndTime($endTime)
                         ->setStatus($gen->randomElement(NoteStatus::class))
                         ->setLocation($gen->randomElement(NoteLocation::class))
                         ->setMethod(NoteMethod::BILLABLE_SUPERVISED_VISIT)
                         ->setParentalRole($gen->randomElement(VisitQualityLevel::class))
                         ->setChildDevelopment($gen->randomElement(VisitQualityLevel::class))
                         ->setAppropriateResponse($gen->randomElement(VisitQualityLevel::class))
                         ->setChildAheadOfSelf($gen->randomElement(VisitQualityLevel::class))
                         ->setShowsEmpathy($gen->randomElement(VisitQualityLevel::class))
                         ->setChildFocused($gen->randomElement(VisitQualityLevel::class))
                         ->setNarrative($gen->text(100))
                         ->setStrengths($gen->text(100))
                         ->setIssues($gen->text(100))
                         ->setRecommendation($gen->text(100))
                        //  ->setMembers(
                        //      (
                        //          count($caseMembers) > 0
                        //         ?
                        //         $gen->randomElements($caseMembers, $gen->numberBetween(1, count($caseMembers)))
                        //         :
                        //         null
                        //      )
                        //  )
                    ;
                    // dd($note);
                } else {
                    $note = new StandardNote();
                    $note->setReferral($ref)
                         ->setDate($gen->dateTimeBetween('-1 year', 'now'))
                         ->setStartTime($startTime)
                         ->setEndTime($endTime)
                         ->setStatus($gen->randomElement(NoteStatus::class))
                         ->setLocation($gen->randomElement(NoteLocation::class))
                         ->setMethod(NoteMethod::BILLABLE)
                         ->setNote($gen->text(100))
                        //  ->setMembers(
                        //      (
                        //          count($caseMembers) > 0
                        //         ?
                        //         $gen->randomElements($caseMembers, $gen->numberBetween(1, count($caseMembers)))
                        //         :
                        //         null
                        //      )
                        //  )
                    ;
                }

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

        $manager->flush();
    }

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