<?php

namespace App\DataFixtures;

use App\Entity\User;
use App\Entity\Sponsor;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class UserFixtures extends Fixture implements DependentFixtureInterface
{
    public function __construct(
        private  UserPasswordHasherInterface $hashPassword, 
        private ParameterBagInterface $params
    ) {
    }

    public function load(ObjectManager $manager): void
    {
        $fixtures_type = $this->params->get('app.fixtures-type');
        
        $raw_users = [
            [ "name" => "Vincent RUDEAU",      "email" => "vrudeau@gmail.com",                 "ROLE_ADMIN" => true,  "sponsor" => "Points Communs" ],
            [ "name" => "Arnaud VASSEUR",      "email" => "arnaud.vasseur@points-communs.com", "ROLE_ADMIN" => true,  "sponsor" => "Points Communs" ],
        ];

        if ($fixtures_type == "test") {
            $raw_users[] = [ "name" => "Camille VIGOUROUX",   "email" => "camille.vigouroux@gmail.com",       "ROLE_ADMIN" => false, "sponsor" => "Festival Jazz au fil de l’Oise" ];
            $raw_users[] = [ "name" => "Antoine ROBINET",     "email" => "robinet.antoine@icloud.com",        "ROLE_ADMIN" => false, "sponsor" => "Festival Jazz au fil de l’Oise" ];

            $raw_users[] = [ "name" => "Mandible AMBIGUOUS",  "email" => "mandible.ambiguous@email.com",      "ROLE_ADMIN" => false, "sponsor" => "Festival Baroque de Pontoise" ]; 
            $raw_users[] = [ "name" => "Competitor SNORT",    "email" => "competitor.snort@email.com",        "ROLE_ADMIN" => false, "sponsor" => "Festival Baroque de Pontoise" ]; 
            $raw_users[] = [ "name" => "Papaya PRODUCTIVE",   "email" => "papaya.productive@email.com",       "ROLE_ADMIN" => false, "sponsor" => "Le Moulin Fondu, Oposito" ]; 
            $raw_users[] = [ "name" => "Skiver TABOO",        "email" => "skiver.taboo@email.com",            "ROLE_ADMIN" => false, "sponsor" => "Le Moulin Fondu, Oposito" ]; 
            $raw_users[] = [ "name" => "Informal CHICKPEAS",  "email" => "informal.chickpeas@email.com",      "ROLE_ADMIN" => false, "sponsor" => "Fondation Royaumont" ]; 
            $raw_users[] = [ "name" => "Offer FLAGRANT",      "email" => "offer.flagrant@email.com",          "ROLE_ADMIN" => false, "sponsor" => "Fondation Royaumont" ]; 
            $raw_users[] = [ "name" => "Unlike CUBE",         "email" => "unlike.cube@email.com",             "ROLE_ADMIN" => false, "sponsor" => "Festival Baroque de Pontoise" ]; 
            $raw_users[] = [ "name" => "Disfigured TRAVELER", "email" => "disfigured.traveler@email.com",     "ROLE_ADMIN" => false, "sponsor" => "Festival Baroque de Pontoise" ]; 
            $raw_users[] = [ "name" => "Burn SWEEPER",        "email" => "burn.sweeper@email.com",            "ROLE_ADMIN" => false, "sponsor" => "Festival Baroque de Pontoise" ]; 
            $raw_users[] = [ "name" => "Demanding RINSE",     "email" => "demanding.rinse@email.com",         "ROLE_ADMIN" => false, "sponsor" => "Espace Michel Berger de Sannois" ]; 
            $raw_users[] = [ "name" => "Incomplete HERO",     "email" => "incomplete.hero@email.com",         "ROLE_ADMIN" => false, "sponsor" => "Centre des arts d'Enghien-les-Bains" ]; 
            $raw_users[] = [ "name" => "Fitzroy BRICK",       "email" => "fitzroy.brick@email.com",           "ROLE_ADMIN" => false, "sponsor" => "Centre des arts d'Enghien-les-Bains" ]; 
        }

        $i = 0;
        foreach ($raw_users as $r_user) {

            $roles = ["ROLE_USER","ROLE_EDITOR"];
            if ($r_user["ROLE_ADMIN"]) {
                $roles[] = "ROLE_ADMIN";
            }
            
            $password = "password+".$r_user["email"];

            $user = new User();
            $user->setName($r_user["name"])
                ->setEmail($r_user["email"])
                ->setPassword($this->hashPassword->hashPassword($user, $password))
                ->setRoles($roles)
                ->setActive($i % 5 != 0 and $i > 0);

            $sponsor = $manager->getRepository(Sponsor::class)->findOneBy(["name" => $r_user["sponsor"]]);
            $user->setSponsor($sponsor);

            $manager->persist($user);

            $i++;
        }

        $manager->flush();
    }

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