<?php

namespace App\Http\Resources;

use App\CourseOfConstruction;
use App\Http\Resources\CourseOfConstruction\CourseOfConstructionImageCollection;
use Illuminate\Http\Resources\Json\ResourceCollection;

class CourseOfConstructionCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $result = [];

        /** @var CourseOfConstruction $courseOfConstruction */
        foreach ($this->collection as $key => $courseOfConstruction) {
            $result[$key] = [
                'id' => $courseOfConstruction->id,
                'report_date' => $courseOfConstruction
                    ->getReportYearAttribute($courseOfConstruction->report_date, []),
                'youtube_link' => $courseOfConstruction->youtube_link,
                'images' => new CourseOfConstructionImageCollection($courseOfConstruction->getMedia('images'))
            ];
        }

        return $result;
    }
}
