<?php

namespace Database\Seeders\Objects;

use App\Models\Dictionaries\DictionaryItem;
use App\Models\Objects\Field;
use App\Models\Objects\FieldType;
use App\Models\Objects\ObjectType;
use App\Models\Pages\Page;
use App\Transformers\Dictionaries\DictionaryItemTransformer;
use App\Transformers\Objects\ObjectTypeTransformer;
use App\Transformers\Pages\PageTransformer;
use Illuminate\Database\Seeder;

class FieldsTableSeeder extends Seeder {
    public array $fields = [
        'header' => [
            'title' => 'Текст заголовка',
            'type' => FieldType::TEXT
        ],
        'header-type' => [
            'title' => 'Тип заголовка',
            'type' => FieldType::RELATION,
            'required' => true,
            'params' => [
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'title-types']]]
            ]
        ],
        'header-required' => [
            'title' => 'Текст заголовка',
            'type' => FieldType::TEXT,
            'required' => true
        ],

        'documents' => [
            'title' => 'Документы',
            'type' => FieldType::DOCUMENT,
            'multiple' => true
        ],
        'documents-required' => [
            'title' => 'Документы',
            'type' => FieldType::DOCUMENT,
            'multiple' => true,
            'required' => true
        ],

        'html' => [
            'title' => 'Содержимое текстового блока',
            'type' => FieldType::HTML
        ],
        'html-required' => [
            'title' => 'Содержимое текстового блока',
            'type' => FieldType::HTML,
            'required' => true
        ],

        'images' => [
            'title' => 'Изображения',
            'type' => FieldType::IMAGE,
            'multiple' => true
        ],
        'images-required' => [
            'title' => 'Изображения',
            'type' => FieldType::IMAGE,
            'multiple' => true,
            'required' => true
        ],
        'image-type' => [
            'title' => 'Тип изображения',
            'type' => FieldType::RELATION,
            'required' => true,
            'params' => [
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'image-type']]]
            ]
        ],

        'video-url' => [
            'title' => 'Ссылка на видео',
            'type' => FieldType::STRING,
            'required' => true
        ],

        'button-title' => [
            'title' => 'Текст',
            'type' => FieldType::STRING,
            'required' => true
        ],
        'button-url' => [
            'title' => 'Ссылка на кнопку',
            'type' => FieldType::STRING,
            'required' => true
        ],

        'contact-name' => [
            'title' => 'Наименование',
            'type' => FieldType::STRING,
        ],
        'contact-legal-address' => [
            'title' => 'Юридический адрес',
            'type' => FieldType::STRING,
        ],
        'contact-location-address' => [
            'title' => 'Фактический адрес',
            'type' => FieldType::STRING,
        ],
        'contact-email' => [
            'title' => 'Email',
            'type' => FieldType::STRING,
        ],
        'contact-phone' => [
            'title' => 'Телефон',
            'type' => FieldType::STRING,
        ],
        'contact-description' => [
            'title' => 'Информация',
            'type' => FieldType::HTML,
        ],

        'feedback-support-email' => [
            'title' => 'Email кому будет отправлен ответ',
            'type' => FieldType::STRING,
            'required' => true,
        ],
        'feedback-form-type' => [
            'title' => 'Тип формы',
            'type' => FieldType::RELATION,
            'required' => true,
            'params' => [
                'related' => ObjectType::class, 'transformer' => ObjectTypeTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['parent' => ['name' => 'feedback-form']]]
            ]
        ],

        'feedback-email' => [
            'title' => 'Электронная почта',
            'type' => FieldType::STRING,
            'required' => true,
        ],
        'feedback-name' => [
            'title' => 'Ваше имя',
            'type' => FieldType::STRING,
            'required' => true,
        ],
        'feedback-type' => [
            'title' => 'Тема обращения',
            'type' => FieldType::RELATION,
            'required' => true,
            'params' => [
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'feedback-types']]],
            ]
        ],
        'feedback-message' => [
            'title' => 'Текст сообщения',
            'type' => FieldType::TEXT,
            'required' => true,
        ],

        'company-name' => [
            'title' => 'Наименование организации',
            'type' => FieldType::STRING,
            'required' => true
        ],
        'activities' => [
            'title' => 'Объект сертификации',
            'type' => FieldType::RELATION,
            'required' => true,
            'multiple' => true,
            'params' => [
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'activities']]],
            ]
        ],

        'applicant-name' => [
            'title' => 'Заявитель',
            'type' => FieldType::STRING,
            'required' => true
        ],
        'applicant-address' => [
            'title' => 'Адрес заявителя',
            'type' => FieldType::STRING
        ],
        'applicant-email' => [
            'title' => 'Электронная почта заявителя',
            'type' => FieldType::STRING
        ],
        'applicant-phone' => [
            'title' => 'Телефон заявителя',
            'type' => FieldType::STRING
        ],
        'producer-name' => [
            'title' => 'Производитель',
            'type' => FieldType::STRING
        ],
        'producer-address' => [
            'title' => 'Адрес производителя',
            'type' => FieldType::STRING
        ],
        'producer-email' => [
            'title' => 'Электронная почта производителя',
            'type' => FieldType::STRING
        ],
        'producer-phone' => [
            'title' => 'Телефон производителя',
            'type' => FieldType::STRING
        ],

        'company-address' => [
            'title' => 'Адрес',
            'type' => FieldType::STRING
        ],
        'company-site' => [
            'title' => 'Сайт',
            'type' => FieldType::STRING
        ],
        'company-email' => [
            'title' => 'Электронная почта',
            'type' => FieldType::STRING
        ],
        'company-phone' => [
            'title' => 'Телефон',
            'type' => FieldType::STRING
        ],

        'primary-developer' => [
            'title'=> 'Основной исполнитель',
            'type' => FieldType::STRING
        ],
        'funding-source' => [
            'title' => 'Источник финансирования',
            'type' => FieldType::STRING
        ],
        'plan-year' => [
            'title' => 'Год плана',
            'type' => FieldType::INTEGER
        ],

        'discussion-start-date' => [
            'title' => 'Дата начала обсуждения',
            'type' => FieldType::DATE
        ],
        'discussion-finish-date' => [
            'title' => 'Дата окончания обсуждения',
            'type' => FieldType::DATE
        ],

        'research-type' => [
            'title' => 'Вид работы',
            'type' => FieldType::RELATION,
            'required' => true,
            'params' => [
                'appearance' => 'radio',
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'research-types']]]
            ]
        ],
        'research-objective' => [
            'title' => 'Цель исследования',
            'type' => FieldType::TEXT
        ],

        'technical-conclusion' => [
            'title' => 'Техническое заключение',
            'type' => FieldType::DOCUMENT,
            'required' => true
        ],
        'developer-name' => [
            'title' => 'Разработчик',
            'type' => FieldType::STRING
        ],
        'developer-address' => [
            'title' => 'Адрес разработчика',
            'type' => FieldType::STRING
        ],

        'operation-type' => [
            'title' => 'Вид работы',
            'type' => FieldType::RELATION,
            'required' => true,
            'params' => [
                'appearance' => 'radio',
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'operation-types']]]
            ]
        ],
        'order-name' => [
            'title' => 'Наименование приказа',
            'type' => FieldType::STRING,
            'required' => true
        ],
        'order-date' => [
            'title' => 'Дата приказа',
            'type' => FieldType::DATE,
            'required' => true
        ],
        'order-document' => [
            'title' => 'Документ приказа',
            'type' => FieldType::DOCUMENT,
            'required' => true
        ],

        'listings' => [
            'title' => 'Вхождение в перечень ПП',
            'type' => FieldType::RELATION,
            'multiple' => true,
            'params' => [
                'appearance' => 'checkbox',
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'listings']]]
            ]
        ],

        'active-since' => [
            'title' => 'Дата начала действия',
            'type' => FieldType::DATE,
            'required' => true
        ],
        'active-till' => [
            'title' => 'Дата окончания действия',
            'type' => FieldType::DATE
        ],

        'developer' => [
            'title' => 'Разработчик',
            'type' => FieldType::STRING
        ],

        'maps-url' => [
            'title' => 'Адрес карты',
            'type' => FieldType::STRING,
            'required' => true,
        ],

        'moderate-pages' => [
            'title' => 'Модерируемые разделы сайта',
            'type' => FieldType::RELATION,
            'multiple' => true,
            'params' => [
                'related' => Page::class, 'transformer' => PageTransformer::class,
                'options' => ['show' => true]
            ]
        ],
        'moderate-permissions' => [
            'title' => 'Права',
            'type' => FieldType::RELATION,
            'multiple' => true,
            'params' => [
                'appearance' => 'checkbox',
                'related' => DictionaryItem::class, 'transformer' => DictionaryItemTransformer::class,
                'options' => ['show' => true, 'whereHas' => ['dictionary' => ['name' => 'moderate-permissions']]]
            ]
        ]
    ];

    public function run() {
        collect($this->fields)->each(function($data, $name) {
            $field = Field::firstOrCreate(['name' => $name]);
            $field->update($data);
        });
    }
}
