<?php

namespace Seka\Vzdelavanie\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
 * Class UcebnicaResource
 * @package Seka\Vzdelavanie\Http\Resources
 */
class UcebnicaResource extends JsonResource
{
    use HideResourceFieldsTrait;

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return $this->filterFields([
            'id' => (int) $this->resource->id,
            'kategoria_id' => (int) $this->resource->kategoria_id,
            'autor' => $this->resource->autor,
            'nazov' => $this->resource->nazov,
            'popis' => $this->resource->popis,
            'obrazok' => $this->resource->obrazok,
            'verejne' => (bool) $this->resource->verejne,
            'kategoria' => $this->whenLoaded('kategoria', function () {
                return KategoriaResource::make($this->resource->kategoria);
            }),
            'sekcie' => $this->whenLoaded('sekcie', function () {
                return SekciaResource::collection($this->resource->sekcie)->hide(['obsah']);
            }),
            'datum_vytvorenia' => (string) $this->resource->created_at,
            'datum_zmeny' => (string) $this->resource->updated_at,
            'link' => route('vzdelavanie.kniznica.ucebnica.show', [$this->resource->id]),
        ]);
    }
}
