<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Vzdelavanie\Charts;

use Chartisan\PHP\Chartisan;
use ConsoleTVs\Charts\BaseChart;
use Elceka\Laravel\HashId\Hasher;
use Illuminate\Http\Request;
use Seka\Portal\Modul\Vzdelavanie\Models\Pouzivatel\Testovanie\Test;

/**
 * Class UspesnostRieseniaTestovNaPekChart
 *
 * @package Seka\Portal\Modul\Vzdelavanie\Charts
 */
final class UspesnostRieseniaTestovNaPekChart extends BaseChart
{
    /**
     * Determines the chart name to be used on the
     * route. If null, the name will be a snake_case
     * version of the class name.
     */
    public ?string $name = 'vzdelavanie_testy_uspesnost_riesenia_na_pek';

    /**
     * Determines the name suffix of the chart route.
     * This will also be used to get the chart URL
     * from the blade directrive. If null, the chart
     * name will be used.
     */
    public ?string $routeName = null;

    /**
     * Determines the prefix that will be used by the chart
     * endpoint.
     */
    public ?string $prefix = 'vzdelavanie/testy';

    /**
     * Determines the middlewares that will be applied
     * to the chart endpoint.
     */
    public ?array $middlewares = ['auth'];

    /**
     * @param \Illuminate\Http\Request $request
     *
     * @return \Chartisan\PHP\Chartisan
     */
    public function handler(Request $request): Chartisan
    {
        $pouzivatelId = auth()->id();
        $query = Test::query()
            ->with('pouzivatel')
            ->orderBy('casova_znacka_vyhodnotenia')
            ->vyhodnoteny();

        /** @var \Illuminate\Database\Eloquent\Collection $testy */
        if (auth()->user()->hasRole('spravca_pek')) {
            if (!is_null($hashId = $request->query('pouzivatel_id'))) {
                $pouzivatelId = Hasher::decode($hashId);

                // ak nie je pouzivatel, pre ktoreho sa pozaduju udaje z rovnakeho PEK ako spravca
                if (is_null(auth()->user()->pracovisko->pouzivatelia()->whereId($pouzivatelId)->get())) {
                    return Chartisan::build();
                }
            }
        }

        $testy = $query->where('pouzivatel_id', $pouzivatelId)->take(50)->get();

        return Chartisan::build()
            ->labels(
                $testy->map(function ($test) {
                    return $test->casova_znacka_vyhodnotenia->format('d.m.Y');
                })->toArray(),
            )
            ->dataset(
                'Úspešné',
                $testy->map(function ($test) {
                    return $test->spravne_zodpovedane_otazky_percentualne === 100.0 ? $test->spravne_zodpovedane_otazky_percentualne : null;
                })->toArray(),
            )
            ->dataset(
                'Neúspešné',
                $testy->map(function ($test) {
                    return $test->spravne_zodpovedane_otazky_percentualne < 100.0 ? $test->spravne_zodpovedane_otazky_percentualne : null;
                })->toArray(),
            )
            ->dataset(
                'Tendencia úspešnosti',
                $testy->map(function ($test) {
                    return $test->spravne_zodpovedane_otazky_percentualne;
                })->toArray(),
            );
    }
}
