<?php

namespace App\Modules\Invoice\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class CustomerResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'phone' => $this->phone,
            'address' => $this->address,
            'status' => $this->status,
            'portal_account' => $this->whenLoaded('portalAccount', fn () => $this->portalAccount ? [
                'id' => $this->portalAccount->id,
                'name' => $this->portalAccount->name,
                'email' => $this->portalAccount->email,
                'status' => $this->portalAccount->status,
                'last_login_at' => optional($this->portalAccount->last_login_at)->toISOString(),
            ] : null),
        ];
    }
}
