<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class CollectionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     */
    public function toArray($request): array
    {
        return [
            'uuid' => $this->uuid,
            'name' => $this->name,
            'slug' => $this->slug,
            'is_singleton' => $this->is_singleton,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            // Include fields only if relationship is loaded
            'fields' => $this->whenLoaded('fields', function () {
                return $this->fields->map(function ($field) {
                    return [
                        'type' => $field->type,
                        'label' => $field->label,
                        'name' => $field->name,
                        'description' => $field->description,
                        'placeholder' => $field->placeholder,
                        'options' => $field->options,
                        'validations' => $field->validations,
                    ];
                })->values();
            }),
        ];
    }
} 