<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Customers\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Maestro\Core\Modules\Customers\Models\Customer;

/** @mixin Customer */
class CustomerResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $data = [
            'id' => $this->id,
            'organization_id' => $this->organization_id,
            'name' => $this->name,
            'email' => $this->email,
            'phone' => $this->phone,
            'website' => $this->website,
            'notes' => $this->notes,
            'is_active' => $this->is_active,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];

        if ($this->relationLoaded('contacts')) {
            $data['contacts'] = ContactResource::collection($this->contacts);
        }
        if ($this->relationLoaded('addresses')) {
            $data['addresses'] = AddressResource::collection($this->addresses);
        }

        return $data;
    }
}
