<?php

namespace App\Http\Resources;

use App\Classes\Modules\Companies\Services\FetchesCompanyServices;
use App\Classes\ValueObjects\Constants\BankAccountType;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Models\CompanyConnection;
use App\Models\CompanyModule;
use App\Models\Currency;
use App\Models\SegmentConstant;
use Illuminate\Http\Resources\Json\JsonResource;

class CompanyModuleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $defaultAddress = $this->addresses()->defaultAddress()->first();

        /** @var CompanyModule $defaultAddress */
        $defaultAddress = $defaultAddress ? $defaultAddress : $this->addresses()->first();

        $connection = $this->inviters()->withPivot('invitee_reference')->first();
        $marking = $connection ? $connection->pivot->invitee_reference:'';

        return [
            'id' => $this->id,
            'type' => $this->type,
            'name' => $this->name,
            'reference' => $this->reference,
            'address' => new AddressResource($defaultAddress),
            'company' => new CompanyResource($this->whenLoaded('company')),
            'marking' => $marking
        ];

    }
}
