<?php

namespace Database\Seeders\Objects;

use App\Models\Objects\Field;
use App\Models\Objects\FieldsGroup;
use App\Models\Objects\ObjectType;
use Illuminate\Database\Seeder;

class ObjectTypeFieldsTableSeeder extends Seeder {
    public array $objectTypeFields = [
        'page-sidebar' => [
            'common' => [
                'fields' => ['header', 'html', 'documents']
            ]
        ],

        'page-section-header' => [
            'common' => [
                'fields' => ['header-type', 'header-required']
            ]
        ],
        'page-section-html' => [
            'common' => [
                'fields' => ['html-required']
            ]
        ],
//        'page-section-list' => [
//            'common' => [
//                'fields' => ['list-type', 'list-items']
//            ]
//        ],
        'page-section-images' => [
            'common' => [
                'fields' => ['images-required']
            ]
        ],
        'page-section-documents' => [
            'common' => [
                'fields' => ['documents-required']
            ]
        ],
        'page-section-video' => [
            'common' => [
                'fields' => ['video-url']
            ]
        ],
        'page-section-button' => [
            'common' => [
                'fields' => ['button-title', 'button-url']
            ]
        ],
        'page-section-iframe' => [
            'common' => [
                'fields' => ['iframe-url']
            ]
        ],
        'page-section-contacts' => [
            'common' => [
                'fields' => ['contact-name', 'contact-legal-address', 'contact-location-address', 'contact-email', 'contact-phone', 'contact-description']
            ]
        ],
        'page-section-feedback' => [
            'common' => [
                'fields' => ['feedback-support-email', 'feedback-form-type']
            ]
        ],
        'feedback-form-support' => [
            'common' => [
                'fields' => ['feedback-email', 'feedback-name', 'feedback-type', 'feedback-message']
            ]
        ],
        'entry-operation' => [
            'common' => [
                'fields' => ['operation-type', 'order-name', 'order-date', 'order-document', 'listings', 'active-since', 'active-till', 'developer']
            ]
        ]
    ];

    public function run() {
        collect($this->objectTypeFields)->each(function($data, $name) {
            $objectType = ObjectType::query()->where(['name' => $name])->firstOrFail();
            $this->syncGroups($objectType, $data);
        });
    }

    public function syncGroups(ObjectType $objectType, array $data) {
        $ord = 0;
        collect($data)->each(function($data, $name) use($objectType, &$ord) {
            $group = $objectType->groups()->firstOrCreate(['name' => $name]);
            $group->update(collect($data)->except('fields')->put('ord', $ord++)->all());
            $this->syncGroupFields($group, $data);
        });
        $objectType->groups()->whereNotIn('name', array_keys($data))->delete();
    }

    public function syncGroupFields(FieldsGroup $group, array $data) {
        $fields = [];
        collect($data['fields'] ?? [])->map(function($name, $ord) use(&$fields) {
            if ($field = Field::query()->where(['name' => $name])->first()) $fields[$field->id] = ['ord' => $ord];
        });
        $group->fields()->sync($fields);
    }
}
