<?php

namespace App\DataFixtures;

use App\Factory\MediaObjectFactory;
use App\Factory\UserFactory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\HttpKernel\KernelInterface;

class AppFixtures extends Fixture
{
    public function __construct(
        private KernelInterface $appKernel
    )
    {
    }

    public function load(ObjectManager $manager): void
    {
        $projectRoot = $this->appKernel->getProjectDir();
        $mediaPath = $projectRoot . '/public/media/';
        $files = glob($mediaPath . '*');
        foreach ($files as $file) {
            if (is_file($file)) {
                unlink($file);
            }
        }

        $documentPath = $projectRoot . '/public/media/';
        $files = glob($documentPath . '*');
        foreach ($files as $file) {
            if (is_file($file)) {
                unlink($file);
            }
        }

        $system = UserFactory::createOne(
            [
                'email' => 'jakob@spawntree.de',
                'firstName' => 'Jakob',
                'lastName' => 'Nordstrøm',
                'password' => '12spawntree345',
                'roles' => ['ROLE_ADMIN'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $system->_save();

        $adminD = UserFactory::createOne(
            [
                'email' => 'd.knudsen@spawntree.de',
                'firstName' => 'Daniel',
                'lastName' => 'Knudsen',
                'password' => '12spawntree345',
                'roles' => ['ROLE_ADMIN'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $adminD->_save();

        UserFactory::createOne(
            [
                'email' => 'f.eisenmenger@spawntree.de',
                'firstName' => 'Florian',
                'lastName' => 'Eisenmenger',
                'password' => '12spawntree345',
                'image' => MediaObjectFactory::createOne(),
                'roles' => ['ROLE_ADMIN'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $adminD->_save();

        UserFactory::createOne(
            [
                'email' => 'pilot1@spawntree.de',
                'firstName' => 'Pilot1',
                'lastName' => 'Pilot1',
                'password' => '12spawntree345',
                'image' => MediaObjectFactory::createOne(),
                'roles' => ['ROLE_USER'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $adminD->_save();

        UserFactory::createOne(
            [
                'email' => 'pilot2@spawntree.de',
                'firstName' => 'Pilot2',
                'lastName' => 'Pilot2',
                'password' => '12spawntree345',
                'image' => MediaObjectFactory::createOne(),
                'roles' => ['ROLE_USER'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $adminD->_save();

        UserFactory::createOne(
            [
                'email' => 'pilot3@spawntree.de',
                'firstName' => 'Pilot3',
                'lastName' => 'Pilot3',
                'password' => '12spawntree345',
                'image' => MediaObjectFactory::createOne(),
                'roles' => ['ROLE_USER'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $adminD->_save();

        UserFactory::createOne(
            [
                'email' => 'pilot4@spawntree.de',
                'firstName' => 'Pilot4',
                'lastName' => 'Pilot4',
                'password' => '12spawntree345',
                'image' => MediaObjectFactory::createOne(),
                'roles' => ['ROLE_USER'],
                'reference_id' => 1
            ]
        );
        $system->_set('image', MediaObjectFactory::createOne()->_real());
        $adminD->_save();

    }
}
