<?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-images' => [
            'common' => [
                'fields' => ['images-required', 'image-type']
            ]
        ],
        '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-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']
            ]
        ],
        'page-section-maps' => [
            'common' => [
                'fields' => ['maps-url']
            ]
        ],
        'feedback-form-support' => [
            'common' => [
                'fields' => ['feedback-email', 'feedback-name', 'feedback-type', 'feedback-message']
            ]
        ],

        'entry-properties-laboratory' => [
            'common' => ['fields' => ['company-name', 'activities']]
        ],
        'entry-properties-certifier' => [
            'common' => ['fields' => ['company-name', 'activities']]
        ],
        'entry-properties-expert' => [
            'common' => ['fields' => []]
        ],
        'entry-properties-certificate' => [
            'common' => [
                'fields' => ['applicant-name', 'applicant-address', 'applicant-email', 'applicant-phone', 'producer-name', 'producer-address', 'producer-email', 'producer-phone']
            ]
        ],
        'entry-properties-company' => [
            'common' => ['fields' => ['company-address', 'company-email', 'company-phone']]
        ],
        'entry-properties-development' => [
            'common' => ['fields' => ['operation-type', 'primary-developer', 'funding-source', 'plan-year']]
        ],
        'entry-properties-discussion' => [
            'common' => ['fields' => ['discussion-start-date', 'discussion-finish-date', 'funding-source']]
        ],
        'entry-properties-research' => [
            'common' => ['fields' => ['research-type', 'research-objective', 'plan-year']]
        ],
        'entry-properties-technical-certificate' => [
            'common' => [
                'fields' => ['technical-conclusion', 'developer-name', 'developer-address', 'company-site', 'company-email', 'company-phone', 'producer-name', 'producer-address']
            ]
        ],

        'entry-operation-ruleset' => [
            'common' => [
                'fields' => ['operation-type', 'order-name', 'order-date', 'order-document', 'listings', 'active-since', 'active-till', 'developer']
            ]
        ],

        'company-member-properties' => [
            'common' => [
                'fields' => ['moderate-pages', 'moderate-permissions']
            ]
        ],

        'application-properties' => [
            'common' => [
                'fields' => ['documents']
            ]
        ]
    ];

    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);
    }
}
