<?php

namespace App\Http\Resources;

use App\Models\CartItem;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class CartItemResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id'          => $this->id,
            'cart_id'     => $this->cart_id,
            'schedule_id' => $this->schedule_id,
            'schedule'    => new ScheduleResource($this->whenLoaded('schedule')),
            'created_at'  => $this->created_at?->toISOString(),
            'updated_at'  => $this->updated_at?->toISOString(),
        ];
    }

    /**
     * @param \Illuminate\Database\Eloquent\Collection<CartItem> $resource
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<CartItemResource>
     */
    public static function collection($resource): AnonymousResourceCollection
    {
        return parent::collection($resource);
    }
}
