<?php

namespace App\Transformers\Companies;

use App\Models\Companies\Company;
use App\Services\PermissionsService;
use App\Transformers\Advisories\AdvisoryCompanyTransformer;
use App\Transformers\Assets\AssetTransformer;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\Primitive;
use League\Fractal\TransformerAbstract;

class CompanyTransformer extends TransformerAbstract {
    protected array $defaultIncludes = [];

    protected array $availableIncludes = [
        'logo', 'types', 'departments', 'rootDepartment', 'members', 'rootMembers', 'representativeMember',
        'addresses', 'legalAddress', 'actualAddress', 'bankDetails', 'contacts', 'phones', 'emails', 'permissions',
        'advisoryCompanies'
    ];

    public function transform(Company $model): array {
        return [
            'id' => $model->uuid,
            'name' => $model->name,
            'full_name' => $model->full_name,
            'inn' => $model->inn,
            'kpp' => $model->kpp,
            'ogrn' => $model->ogrn,
            'okved' => $model->okved,
            'okato' => $model->okato,
            'tax_registration_date' => $model->tax_registration_date ? $model->tax_registration_date->toIso8601String() : null
        ];
    }

    public function includeLogo(Company $model): ?Item {
        return $model->logo ? $this->item($model->logo, new AssetTransformer()) : null;
    }

    public function includeTypes(Company $model): Collection {
        return $this->collection($model->types, new CompanyTypeTransformer());
    }

    public function includeDepartments(Company $model): Collection {
        return $this->collection($model->departments, new DepartmentTransformer());
    }

    public function includeRootDepartment(Company $model): ?Item {
        return $model->rootDepartment ? $this->item($model->rootDepartment, new DepartmentTransformer()) : null;
    }

    public function includeMembers(Company $model): Collection {
        return $this->collection($model->members, new CompanyMemberTransformer());
    }

    public function includeRootMembers(Company $model): Collection {
        return $this->collection($model->rootMembers, new CompanyMemberTransformer());
    }

    public function includeRepresentativeMember(Company $model): ?Item {
        return $model->representativeMember ? $this->item($model->representativeMember, new CompanyMemberTransformer()) : null;
    }

    public function includeAddresses(Company $model): Collection {
        return $this->collection($model->addresses, new AddressTransformer());
    }

    public function includeLegalAddress(Company $model): Item {
        return $this->item($model->legalAddress, new AddressTransformer());
    }

    public function includeActualAddress(Company $model): Item {
        return $this->item($model->actualAddress, new AddressTransformer());
    }

    public function includeBankDetails(Company $model): Item {
        return $this->item($model->bankDetails, new BankDetailsTransformer());
    }

    public function includeContacts(Company $model): Collection {
        return $this->collection($model->contacts, new ContactTransformer());
    }

    public function includePhones(Company $model): Collection {
        return $this->collection($model->phones, new ContactTransformer());
    }

    public function includeEmails(Company $model): Collection {
        return $this->collection($model->emails, new ContactTransformer());
    }

    public function includePermissions(Company $model): Primitive {
        return $this->primitive((new PermissionsService($model))->get());
    }

    public function includeAdvisoryCompanies(Company $model): Collection {
        return $this->collection($model->advisoryCompanies, new AdvisoryCompanyTransformer());
    }
}