<?php

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

use App\Http\Controllers\API\BaseController;
use App\Models\Hodnotenie\HodnotenieEnum;
use App\Repositories\HodnotenieRepositoryContract;
use App\Repositories\InformaciaRepositoryContract;
use Dingo\Api\Http\Response;

/**
 * Class ZiskatVsetkyHodnoteniaInformacieAction
 */
final class ZiskatVsetkyHodnoteniaInformacieAction extends BaseController
{
    private HodnotenieRepositoryContract $hodnotenieRepository;

    private InformaciaRepositoryContract $informaciaRepository;

    public function __construct(
        InformaciaRepositoryContract $informaciaRepository,
        HodnotenieRepositoryContract $hodnotenieRepository,
    ) {
        $this->informaciaRepository = $informaciaRepository;
        $this->hodnotenieRepository = $hodnotenieRepository;
    }

    public function __invoke(int $informaciaId): Response
    {
        $informacia = $this->informaciaRepository->find($informaciaId);

        $hodnoteniaKladne = $this->hodnotenieRepository->pocetHodnoteniPodlaDruhu(
            $informacia,
            HodnotenieEnum::KLADNE(),
        );
        $hodnoteniaZaporne = $this->hodnotenieRepository->pocetHodnoteniPodlaDruhu(
            $informacia,
            HodnotenieEnum::ZAPORNE(),
        );
        $hodnoteniaVsetky = $this->hodnotenieRepository->pocetVsetkychHodnoteni($informacia);
        $hodnoteniePouzivatela = $this->hodnotenieRepository->hodnoteniePouzivatela(auth()->user(), $informacia);

        $sumar = SumarHodnoteniDto::from(
            [
                'id' => $informacia->id,
                'pocetKladnychHodnoteni' => $hodnoteniaKladne,
                'pocetZapornychHodnoteni' => $hodnoteniaZaporne,
                'pocetVsetkychHodnoteni' => $hodnoteniaVsetky,
                'hodnoteniePouzivatela' => $hodnoteniePouzivatela,
            ],
        );

        return $this->response->item($sumar);
    }
}
