<?php

namespace App\Http\Controllers\API\V1\Informacia\Frontend;

use App\Dtos\Factory\InformaciaDtoFactory;
use App\Dtos\PouzivatelDto;
use App\Http\Controllers\API\V1\Pouzivatel\PouzivatelDtoTransformer;
use App\Models\Informacia\Informacia;
use App\Repositories\HodnotenieRepositoryContract;
use App\Repositories\PouzivatelRepositoryContract;
use League\Fractal\Resource\ResourceAbstract;
use League\Fractal\TransformerAbstract;

/**
 * Class InformaciaTransformer
 *
 * @package App\Http\Controllers\API\V1\Informacia\Frontend
 */
class InformaciaTransformer extends TransformerAbstract
{
    /**
     * @var string[]
     */
    protected $availableIncludes = [
        'hodnotenia',
        'vytvoril',
    ];

    /**
     * @var string[]
     */
    protected $defaultIncludes = [];

    /**
     * @param Informacia $informacia
     * @return array
     */
    public function transform(Informacia $informacia): array
    {
        $informaciaDto = InformaciaDtoFactory::make($informacia)->only(
            'id',
            'titulok',
            'uvod',
            'obsah',
            'stav',
            'cas_zverejnenia',
            'odporucane',
            'hash_id',
        );

        return array_merge(
            $informaciaDto->toArray(),
            [
                'links' => [
                    'self' => route(
                        'app.api.frontend.informacie.get',
                        [
                            'informaciaHashId' => $informacia->hashId(),
                        ],
                    ),
                ],
            ],
        );
    }

    /**
     * @param Informacia $informacia
     * @return ResourceAbstract
     */
    public function includeHodnotenia(Informacia $informacia): ResourceAbstract
    {
        $hodnotenieRepository = app(HodnotenieRepositoryContract::class);
        $hodnoteniePouzivatela = $hodnotenieRepository->hodnoteniePouzivatela(auth()->user(), $informacia);

        $sumar = new SumarHodnoteniDto(
            [
                'id' => $informacia->id,
                'pocetKladnychHodnoteni' => $informacia->pocetKladnychHodnoteni,
                'pocetZapornychHodnoteni' => $informacia->pocetZapornychHodnoteni,
                'pocetVsetkychHodnoteni' => $informacia->pocetVsetkychHodnoteni,
                'hodnoteniePouzivatela' => $hodnoteniePouzivatela,
            ],
        );

        return $this->item(
            $sumar,
            new SumarHodnoteniaDtoTransformer(),
            'hodnotenie',
        );
    }

    /**
     * @param Informacia $informacia
     * @return ResourceAbstract
     */
    public function includeVytvoril(Informacia $informacia): ResourceAbstract
    {
        $pouzivatelRepository = app(PouzivatelRepositoryContract::class);
        $pouzivatel = $pouzivatelRepository->find($informacia->vytvoril_id);

        $pouzivatelDto = (new PouzivatelDto($pouzivatel->toArray()))
            ->only(
                'id',
                'meno',
                'priezvisko',
                'cele_meno',
            );

        return $this->item(
            $pouzivatelDto,
            new PouzivatelDtoTransformer(),
            'vytvoril',
        );
    }
}
