<?php

namespace Seka\Vzdelavanie\Http\Resources;

use Elceka\Laravel\HashId\Facades\Hasher;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * Class SekciaResource
 * @package Seka\Vzdelavanie\Http\Resources
 */
class SekciaResource 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' => Hasher::encode($this->resource->id),
            'parent_id' => Hasher::encode($this->resource->parent_id) ?: null,
            'ucebnica_id' => Hasher::encode($this->resource->ucebnica_id),
            'nazov' => $this->resource->nazov,
            'obsah' => $this->resource->obsah,
            'datum_vytvorenia' => (string) $this->resource->created_at,
            'datum_zmeny' => (string) $this->resource->updated_at,
            'ucebnica' => $this->whenLoaded('ucebnica', function () {
                return UcebnicaResource::make($this->resource->ucebnica);
            }),
            'link' => route('vzdelavanie.kniznica.ucebnica.sekcia.show', [Hasher::encode($this->resource->ucebnica_id), Hasher::encode($this->resource->id)]),
        ]);
    }

    /**
     * @param mixed $resource
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection|mixed
     */
    public static function collection($resource)
    {
        return tap(new SekciaResourceCollection($resource), function ($collection) use ($resource) {
            $collection->collects = __CLASS__;
        });
    }
}
