<?php

namespace App\Http\Resources;

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

class ProjectSprintForBacklogResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'     => $this->id,
            'name'   => $this->name,
            'status' => $this->status,

            'sprints' => $this->whenLoaded('sprints', function () {
                return $this->sprints->map(fn($sprint) => [
                    'id'   => $sprint->id,
                    'name' => $sprint->name,
                ]);
            }, []), // ✅ fallback ensures it's always an array
        ];
    }
}
