<?php

namespace Database\Seeders\Pages;

use App\Models\Pages\Page;
use App\Models\Pages\PageType;
use App\Models\Publications\PublicationType;
use App\Models\Registries\RegistryType;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class PagesTableSeeder extends Seeder {
    public array $pages = [
        'Главная' => [
            'slug' => '',
            'children' => [
                'Контентная страница' => [
                    'children' => [
                        'Пример 1' => [],
                        'Пример 2' => [],
                        'Пример 3' => [],
                    ]
                ],
                'Реестры' => [
                    'type' => PageType::NAV_PAGE,
                    'children' => [
                        RegistryType::TITLES[RegistryType::SIMPLE] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::SIMPLE],
                        RegistryType::TITLES[RegistryType::CATEGORIZED] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::CATEGORIZED],
                        RegistryType::TITLES[RegistryType::COMPANIES] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::COMPANIES],
                        RegistryType::TITLES[RegistryType::RULESET] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::RULESET],
                        RegistryType::TITLES[RegistryType::DEVELOPMENTS] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::DEVELOPMENTS],
                        RegistryType::TITLES[RegistryType::TECHNICAL_CERTIFICATES] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::TECHNICAL_CERTIFICATES],
                        RegistryType::TITLES[RegistryType::NTD] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::NTD],
                        RegistryType::TITLES[RegistryType::RESEARCHES] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::RESEARCHES],
                        RegistryType::TITLES[RegistryType::DISCUSSIONS] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::DISCUSSIONS],
                        RegistryType::TITLES[RegistryType::CERTIFICATES] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::CERTIFICATES],
                        RegistryType::TITLES[RegistryType::EXPERTS] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::EXPERTS],
                        RegistryType::TITLES[RegistryType::LABORATORIES] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::LABORATORIES],
                        RegistryType::TITLES[RegistryType::CERTIFIERS] => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::CERTIFIERS],
                    ]
                ],
                'Публикации' => [
                    'type' => PageType::NAV_PAGE,
                    'children' => [
                        PublicationType::TITLES[PublicationType::NEWS] => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::NEWS],
                        PublicationType::TITLES[PublicationType::PHOTOS] => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::PHOTOS],
                        PublicationType::TITLES[PublicationType::PORTFOLIO] => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::PORTFOLIO],
                        PublicationType::TITLES[PublicationType::SMI] => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::SMI],
                        PublicationType::TITLES[PublicationType::VIDEO] => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::VIDEO]
                    ]
                ]
            ]
        ],
        'Агнлийский' => [
            'slug' => 'en',
            'children' => [
                'Content page' => [
                    'children' => [
                        'Example 1' => [],
                        'Example 2' => [],
                        'Example 3' => []
                    ]
                ],
                'Registries' => [
                    'type' => PageType::NAV_PAGE,
                    'children' => [
                        'Simple registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::SIMPLE],
                        'Categorized registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::CATEGORIZED],
                        'Companies registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::COMPANIES],
                        'Ruleset registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::RULESET],
                        'Developments registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::DEVELOPMENTS],
                        'Technical certificates registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::TECHNICAL_CERTIFICATES],
                        'STD registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::NTD],
                        'Researches registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::RESEARCHES],
                        'Public discussions registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::DISCUSSIONS],
                        'Certificates registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::CERTIFICATES],
                        'Experts registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::EXPERTS],
                        'Laboratories registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::LABORATORIES],
                        'Certification agencies registry' => ['type' => PageType::REGISTRY, 'registry_type' => RegistryType::CERTIFIERS],
                    ]
                ],
                'Publications' => [
                    'type' => PageType::NAV_PAGE,
                    'children' => [
                        'News' => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::NEWS],
                        'Photo gallery' => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::PHOTOS],
                        'Portfolio' => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::PORTFOLIO],
                        'Mass media' => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::SMI],
                        'Video' => ['type' => PageType::PUBLICATIONS, 'sub_type' => PublicationType::VIDEO]
                    ]
                ]
            ]
        ]
    ];

    public function run() {
        if (!Page::query()->count()) {
            $ord = 0;
            collect($this->pages)->each(function ($data, $name) use (&$ord) {
                $data['ord'] = $ord++;
                $this->importPage($name, $data);
            });
        }
    }

    public function importPage($name, $data, ?Page $parent = null) {
        $slug = array_key_exists('slug', $data) ? $data['slug'] : Str::slug(Str::transliterate($name));
        $data += ['type' => $data['type'] ?? PageType::CONTENT, 'name' => $name];
        $page = Page::firstOrCreate(['parent_id' => $parent->id ?? 0, 'slug' => $slug]);
        if ($v = collect($data)->except('children', 'registry_type')->all()) $page->update($v);
        if ($page->type === PageType::REGISTRY) $page->registry->update(['type' => $data['registry_type'] ?? RegistryType::SIMPLE]);
        $ord = 0;
        collect($data['children'] ?? [])->each(function($data, $name) use($page, &$ord) {
            $data['ord'] = $ord++;
            $this->importPage($name, $data, $page);
        });
    }

}
