<?php

namespace App\DataFixtures;

use App\Entity\ContactPartnerProduct;
use App\Entity\Document;
use App\Factory\CommentFactory;
use App\Factory\ContactFactory;
use App\Factory\ContactPartnerProductFactory;
use App\Factory\DocumentFactory;
use App\Factory\DocumentObjectFactory;
use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectProductFactory;
use App\Factory\MediaObjectContactFactory;
use App\Factory\MediaObjectUserFactory;
use App\Factory\PartnerFactory;
use App\Factory\PartnerFollowFactory;
use App\Factory\PartnerProductFactory;
use App\Factory\PostingFactory;
use App\Factory\ProductFactory;
use App\Factory\SaleFactory;
use App\Factory\TaskFactory;
use App\Factory\TaskNoteFactory;
use App\Factory\UserFactory;
use App\Factory\UserProductFactory;
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' => 're@spawntree.de',
                'firstName' => 'System',
                'lastName' => 'System',
                'password' => '12spawntree345',
                'roles' => ['ROLE_ADMIN']
            ]
        );
        $system->save();

        $adminD = UserFactory::createOne(
            [
                'email' => 'd.knudsen@spawntree.de',
                'firstName' => 'Daniel',
                'lastName' => 'Knudsen',
                'password' => '12spawntree345',
                'roles' => ['ROLE_ADMIN']
            ]
        );
        $adminD->setImage(MediaObjectUserFactory::createOne()->object());
        $adminD->save();

        UserFactory::createOne(
            [
                'email' => 'f.eisenmenger@spawntree.de',
                'firstName' => 'Florian',
                'lastName' => 'Eisenmenger',
                'password' => '12spawntree345',
                'image' => MediaObjectUserFactory::createOne(),
                'roles' => ['ROLE_ADMIN']
            ]
        );

        UserFactory::createOne(
            [
                'email' => 'jan.hansen@matsen.de',
                'firstName' => 'Jan',
                'lastName' => 'Hansen',
                'password' => 'J1matsen23',
                'image' => MediaObjectUserFactory::createOne(),
                'roles' => ['ROLE_ADMIN']
            ]
        );

        for ($i = 0; $i < 5; $i++) {
            UserFactory::createOne(
                [
                    'email' => 'test' . $i+1 . '@matsen.de',
                    'image' => MediaObjectUserFactory::createOne()
                ]
            );
        }

        for ($i = 0; $i < 5; $i++) {
            UserFactory::createOne(
                [
                    'email' => 'sales' . $i+1 . '@matsen.de',
                    'image' => MediaObjectUserFactory::createOne(),
                    'roles' => ['ROLE_SALES']
                ]
            );
        }

        for ($i = 0; $i < 100; $i++) {
            PartnerFactory::createOne(
                [
                    'logo' => MediaObjectLogoFactory::createOne(),
                    'createdBy' => $adminD
                ]
            );
        }

        for ($i = 0; $i < 200; $i++) {
            ContactFactory::createOne(
                [
                    'image' => MediaObjectContactFactory::createOne(),
                    'createdBy' => $adminD
                ]
            );
        }

        $numProducts = count(FakeValues::PRODUCTS);
        for ($i = 0; $i < $numProducts; $i++) {
            ProductFactory::createOne(
                [
                    'name' => FakeValues::PRODUCTS[$i],
                    'image' => MediaObjectProductFactory::createOne(),
                    'createdBy' => $adminD
                ]
            );
        }

        SaleFactory::createMany(50);
        PostingFactory::createMany(200);
        CommentFactory::createMany(300);
        TaskFactory::createMany(50);
        TaskNoteFactory::createMany(100);
        PartnerFollowFactory::createMany(100);
        DocumentFactory::createMany(50);

        $products = ProductFactory::all();
        $partners = PartnerFactory::all();
        foreach ($partners as $partner) {
            $productsPicked = [];
            for ($i = 0; $i < 3; $i++) {
                $product = $products[array_rand($products)];
                $productsPicked[$product->getId()] = $product;
            }

            foreach ($productsPicked as $item) {
                PartnerProductFactory::createOne(
                    [
                        'partner' => $partner,
                        'product' => $item
                    ]
                );
            }
        }

        $partnerProducts = PartnerProductFactory::all();
        foreach($partnerProducts as $partnerProduct) {
            $partner = $partnerProduct->getPartner();
            $contacts = $partner?->getContacts()->toArray();
            if (count($contacts) > 0) {
                $randomKeys = array_rand($contacts, random_int(1, count($contacts)));
                if (is_array($randomKeys)) {
                    foreach ($randomKeys as $key) {
                        ContactPartnerProductFactory::createOne(
                            [
                                'contact' => $contacts[$key],
                                'partnerProduct' => $partnerProduct,
                            ]
                        );
                    }
                } else {
                    ContactPartnerProductFactory::createOne(
                        [
                            'contact' => $contacts[$randomKeys],
                            'partnerProduct' => $partnerProduct,
                        ]
                    );
                }
            }
        }

        $users = UserFactory::all();
        foreach ($users as $user) {
            $productsPicked = [];
            for ($i = 0; $i < 3; $i++) {
                $product = $products[array_rand($products)];
                $productsPicked[$product->getId()] = $product;
            }

            foreach ($productsPicked as $item) {
                UserProductFactory::createOne(
                    [
                        'user' => $user,
                        'product' => $item
                    ]
                );
            }
        }


    }
}
