<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Entity\Classe;
use App\Entity\Ecole;
use App\Entity\Etudiant;
use App\Entity\Inscription;
use App\Entity\Niveau;
use App\Entity\Section;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

final class RegistrationFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        $qamar = $manager->getRepository(Ecole::class)->findOneBy(['slug' => 'qamar']);
        $daroul = $manager->getRepository(Ecole::class)->findOneBy(['slug' => 'daroul-ilm']);

        if ($qamar instanceof Ecole) {
            $this->syncStudents($manager, $qamar, 'MQO', [
                ['26MQO901', 'Amina', 'Bintou', 'FEMME'],
                ['26MQO902', 'Mariam', 'Oumou', 'FEMME'],
            ]);

            $this->syncInscriptions($manager, $qamar, [
                ['DOS-26-QAMAR-0901', 'TMP-26-MQO-0901', 'Fatima', 'Hawa', Inscription::STATUT_SOUMISE],
                ['DOS-26-QAMAR-0902', 'TMP-26-MQO-0902', 'Khadija', 'Aissatou', Inscription::STATUT_SOUMISE],
                ['DOS-26-QAMAR-0903', 'TMP-26-MQO-0903', 'Safiya', 'Nana', Inscription::STATUT_A_TESTER],
            ]);
        }

        if ($daroul instanceof Ecole) {
            $this->syncStudents($manager, $daroul, 'DI', [
                ['26DI901', 'Ousmane', 'Ibrahim', 'HOMME'],
                ['26DI902', 'Aicha', 'Zainab', 'FEMME'],
            ]);

            $this->syncInscriptions($manager, $daroul, [
                ['DOS-26-DAROULILM-0901', 'TMP-26-DI-0901', 'Moussa', 'Abdoul', Inscription::STATUT_SOUMISE],
                ['DOS-26-DAROULILM-0902', 'TMP-26-DI-0902', 'Halima', 'Ramatou', Inscription::STATUT_SOUMISE],
            ]);
        }

        $manager->flush();
    }

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

    /** @param list<array{0:string,1:string,2:string,3:string}> $rows */
    private function syncStudents(ObjectManager $manager, Ecole $ecole, string $prefix, array $rows): void
    {
        $section = $manager->getRepository(Section::class)->findOneBy(['ecole' => $ecole]);
        $niveau = $manager->getRepository(Niveau::class)->findOneBy(['ecole' => $ecole]);
        $classe = $manager->getRepository(Classe::class)->findOneBy(['ecole' => $ecole]);

        foreach ($rows as [$matricule, $nom, $prenom, $sexe]) {
            $etudiant = $manager->getRepository(Etudiant::class)->findOneBy(['matricule' => $matricule]) ?? new Etudiant();
            $etudiant
                ->setEcole($ecole)
                ->setMatricule($matricule)
                ->setNom($nom)
                ->setPrenom($prenom)
                ->setSexe($sexe)
                ->setTelephoneWhatsapp('237690000'.substr($matricule, -1))
                ->setSection($section)
                ->setNiveau($niveau)
                ->setClasse($classe)
                ->setActif(true)
                ->setAncien(false);

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

    /** @param list<array{0:string,1:string,2:string,3:string,4:string}> $rows */
    private function syncInscriptions(ObjectManager $manager, Ecole $ecole, array $rows): void
    {
        $section = $manager->getRepository(Section::class)->findOneBy(['ecole' => $ecole]);
        $niveau = $manager->getRepository(Niveau::class)->findOneBy(['ecole' => $ecole]);
        $classe = $manager->getRepository(Classe::class)->findOneBy(['ecole' => $ecole]);

        foreach ($rows as [$numero, $temp, $nom, $prenom, $statut]) {
            $inscription = $manager->getRepository(Inscription::class)->findOneBy(['numeroDossier' => $numero]) ?? new Inscription();
            $inscription
                ->setEcole($ecole)
                ->setNumeroDossier($numero)
                ->setMatriculeTemporaire($temp)
                ->setTypeCandidat(Inscription::TYPE_NOUVEAU)
                ->setNom($nom)
                ->setPrenom($prenom)
                ->setSexe('FEMME')
                ->setTelephonePrincipal('690000000')
                ->setTelephoneWhatsapp('237690000000')
                ->setSectionSouhaitee($section)
                ->setNiveauSouhaite($niveau)
                ->setClasseSouhaitee($classe)
                ->setStatut($statut)
                ->setObservationCandidat('Demande de démonstration.');

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