<?php

namespace App\Services\Forms\Advisories;

use App\Models\Advisories\Advisory;
use App\Models\Objects\FieldType;
use App\Services\Forms\FormsService;
use App\Transformers\Companies\CompanyMemberTransformer;

class AdvisoryInfoForms extends FormsService {
    public array $formTitles = ['create' => '', 'update' => 'Профиль подкомитета'];

    public function form(?string $id = null, array $data = []): array {
        $model = Advisory::byUuid($id)->first();
        $groups = [
            ['name' => 'contacts', 'title' => 'Контакты', 'fields' => $this->contactsGroupFields($model)],
            ['name' => 'management', 'title' => 'Руководители и секретариат', 'fields' => $this->managementGroupFields($model)]
        ];
        return ['title' => $this->formTitle($model), 'btnSaveHidden' => true, 'data' => $groups];
    }

    public function contactsGroupFields(?Advisory $model): array {
        $fields = [
            [
                'name' => 'caption',
                'title' => 'Наименование',
                'type' => FieldType::STRING,
                'readonly' => true,
                'value' => $model->caption ?? null
            ],
            [
                'name' => 'email',
                'title' => 'Электронная почта',
                'type' => FieldType::STRING,
                'readonly' => true,
                'value' => $model->email ?? null
            ],
            [
                'name' => 'phone',
                'title' => 'Телефон',
                'type' => FieldType::STRING,
                'readonly' => true,
                'value' => $model->phone ?? null
            ]
        ];
        return ['data' => $fields];
    }

    public function managementGroupFields(?Advisory $model): array {
        $fields = [
            [
                'name' => 'chairman',
                'title' => 'Руководитель',
                'type' => FieldType::RELATION,
                'readonly' => true,
                'value' => ($val = $model->chairman ?? null) ? fractal($val, new CompanyMemberTransformer()) : null
            ],
            [
                'name' => 'vices',
                'title' => 'Заместители',
                'type' => FieldType::RELATION,
                'readonly' => true,
                'value' => $model ? fractal($model->chairmanVices, new CompanyMemberTransformer()) : null
            ],
            [
                'name' => 'secretary',
                'title' => 'Ответственный секретарь',
                'type' => FieldType::RELATION,
                'readonly' => true,
                'value' => ($val = $model->secretary ?? null) ? fractal($val, new CompanyMemberTransformer()) : null
            ],
            [
                'name' => 'assistants',
                'title' => 'Помощники секретаря',
                'type' => FieldType::RELATION,
                'readonly' => true,
                'value' => $model ? fractal($model->secretaryAssistants, new CompanyMemberTransformer()) : null
            ]
        ];
        return ['data' => $fields];
    }



    public function store(array $data): void {
    }

    public function update(string $id, array $data): void {
    }


}
