<?php

namespace Seka\Vzdelavanie\Http\Resources;

use Core\Facades\Hasher;
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' => $this->resource->hash_id,
            'kategoria_id' => Hasher::encode($this->resource->kategoria_id),
            'autor' => $this->resource->autor,
            'nazov' => $this->resource->nazov,
            'popis' => $this->resource->popis,
            'obrazok' => $this->resource->obrazok,
            'stav' => $this->resource->stav,
            '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->hash_id]),
        ]);
    }
}
