<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

/** @mixin \App\Models\Service */
class ServiceResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'service_type' => $this->service_type,
            'platform' => $this->platform,
            'status' => $this->status,
            'hostname' => $this->hostname,
            'ipv4_address' => $this->ipv4_address,
            'ipv6_address' => $this->ipv6_address,
            'domain' => $this->domain,
            'plan' => $this->whenLoaded('plan', fn () => [
                'name' => $this->plan->name,
                'price' => $this->plan->price,
                'billing_cycle' => $this->plan->billing_cycle,
                'prices' => $this->plan->prices?->mapWithKeys(fn ($p) => [
                    $p->billing_cycle => $p->price,
                ]) ?? [],
            ]),
            'provisioned_at' => $this->provisioned_at?->toIso8601String(),
            'created_at' => $this->created_at?->toIso8601String(),
        ];
    }
}
