<?php

namespace App\Services\Forms\Companies;

use App\Models\Companies\Company;
use App\Models\Objects\FieldType;
use App\Services\Forms\FormsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Companies\CompanyTransformer;
use Illuminate\Http\JsonResponse;

class CompanyForms extends FormsService {
    public array $formTitles = ['create' => 'Создание новой организации', 'update' => 'Редактирование организации'];

    public function form(?string $id = null, array $data = []): array {
        $model = Company::byUuid($id)->first();
        $groups = [
            ['name' => 'common', 'fields' => $this->commonGroupFields($model)]
        ];
        return ['title' => $this->formTitle($model), 'data' => $groups];
    }

    public function commonGroupFields(?Company $model): array {
        $fields = [
            [
                'name' => 'inn',
                'title' => 'ИНН',
                'type' => FieldType::STRING,
                'required' => true,
                'readonly' => !!$model,
                'value' => $model->inn ?? null
            ],
            [
                'name' => 'name',
                'title' => 'Наименование организации',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->name ?? null
            ],
            [
                'name' => 'address',
                'title' => 'Юридический адрес',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->legalAddress->full ?? null
            ],
            [
                'name' => 'phone',
                'title' => 'Телефон',
                'type' => FieldType::STRING,
                'required' => false,
                'value' => $model->phone->value ?? null
            ],
            [
                'name' => 'email',
                'title' => 'Email',
                'type' => FieldType::STRING,
                'required' => false,
                'value' => $model->email->value ?? null
            ],
            [
                'name' => 'logo',
                'title' => 'Логотип',
                'type' => FieldType::IMAGE,
                'value' => ($model->logo ?? null) ? fractal($model->logo, new AssetTransformer()) : null
            ]
        ];
        return ['data' => $fields];
    }



    public function store(array $data): ?JsonResponse {
        if (!empty($data['inn']) && ($model = Company::getByData($data))) {
            return fractal($model, new CompanyTransformer())->respond();
        }
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = Company::byUuid($id)->firstOrFail();
        if ($val = trim($data['name'] ?? null)) $model->update(['name' => $val]);
        if ($val = trim($data['address'] ?? null)) $model->setAddress($val);
        if ($val = trim($data['phone'] ?? null)) $model->setPhone($val);
        if ($val = trim($data['email'] ?? null)) $model->setEmail($val);
        $model->setTypes($data['types'] ?? []);
        $model->setLogo($data['logo'] ?? null);
        return fractal($model->fresh(), new CompanyTransformer())->respond();
    }


}
