<?php

use Illuminate\Database\Seeder;
use App\Models\Publication;
use App\Models\PublicationLanguage;
use App\Models\Theme;
use App\Models\ThemeLanguage;

class PublicationsTableSeeder extends BaseSeeder
{
    private $amountToCreate = 20;
    private $createdPublicationsIds;
    
    public function __construct()
    {
        $this->availableLanguages = getAvailableLanguages();
        $this->createdPublicationsIds = [];
    }

    public function run()
    {
        DB::beginTransaction();
        try {
            $this->call([ThemesTableSeeder::class]);
            factory(Publication::class, $this->amountToCreate)
                ->create()
                ->each(function ($publication) {
                    $this->command->info("=== Starting Publications Seeder ===");
                    // Get all multilangue objects
                    $availableLanguages = $this->availableLanguages;

                    // Create Website Objects
                    $websiteObject = $this->createWebsiteObject($publication, 'publication');

                    $publication->websites()
                        ->save($websiteObject);

                    //! --- Custom Code ---

                    //! --- End of Custom Code ---

                    // Create language relationships
                    foreach ($availableLanguages as $langCode => $lang) {
                        $publication->languages()
                            ->save(
                                factory(PublicationLanguage::class)->make([
                                    'language_id' => $langCode,
                                    'publication_id' => $publication->id
                                ])
                            );

                        // Create Website Object Language
                        $this->createWebsiteObjectLanguage($websiteObject, $langCode);
                    }

                    $this->updateUri($websiteObject, 'publication');

                    // Save all changes
                    $publication->save();
                    $this->createdPublicationsIds[] = $publication->id;
                    $this->command->info("Publication created with ID: " . ' ' . $publication->id);
                });
                $this->command->info("All publications were created! now we need to assign each one of them some themes!");

                $createdPublications = Publication::whereIn('id', $this->createdPublicationsIds)->get();
                $themes = Theme::all();
                $this->command->info("=== Starting to populate publication themes... ===");
                foreach($createdPublications as $createdPublication) {
                    $randomThemes = Theme::inRandomOrder()->take(5)->get();
                    $idsString = (String)$randomThemes[0]->id;
                    foreach($randomThemes as $key => $randomTheme) {
                        if ($key !== 0) {   
                            $idsString = $idsString . ', ' . $randomTheme->id;
                        }
                    }
                    $createdPublication->themes()->sync($randomThemes);
                    $this->command->info('The Publication with ID ' . $createdPublication->id . ' have the following related themes: ' . $idsString);
                }
            DB::commit();
        } catch(Throwable $exception) {
            DB::rollback();
            throw($exception);
        }
    }
}
