<?php

namespace App\Http\Resources;

use App\Classes\ValueObjects\Constants\AccountStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Models\CompanyConnections;
use Illuminate\Http\Resources\Json\JsonResource;

class CompanyResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $companyModule = $this->modules->first();
        return [
            'id' => $this->id,
            'account_no' => $this->reference_no,
            'marking' => CompanyConnections::where('module_one', $companyModule->id)->first()->marking,
            'email' => $companyModule->employees->first()->email,
            'name' => $this->name,
            'type' => $this->type,
            'status' => (int) $this->status,
            'delivery_address' => new AddressResource($this->deliveryAddress),
            'contact' => new ContactResource($this->contacts->first()),
            'total_orders' => count($this->orders),
            'identification' => new DocumentResource($this->whenLoaded('documents', $this->documents->whereIn('document_type', DocumentType::IDENTIFICATION_DOCUMENTS)->first())),
        ];
    }
}
