<?php

declare(strict_types=1);

namespace App\Http\Resources\Api\V1;

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

class DnsZoneResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'account_id' => $this->account_id,
            'name' => $this->name,
            'type' => $this->type,
            'master' => $this->master,
            'notified_serial' => $this->notified_serial,
            'status' => $this->status,
            'last_check' => $this->last_check,
            'records_count' => $this->whenLoaded('records', fn () => $this->records->count()),
            'records' => DnsRecordResource::collection($this->whenLoaded('records')),
            'created_at' => $this->created_at?->toIso8601String(),
            'links' => [
                'self' => route('api.v1.dns/zones.show', $this->id),
                'records' => url('/api/v1/dns/zones/'.$this->id.'/records'),
            ],
        ];
    }
}
