<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaHodnotenia;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\Enums\HodnotenieEmisnejKontrolyEnum;
use Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaHodnotenia;
use Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\DataTableRowContract;

/**
 * Class PocetKontrolPodlaHodnoteniaDataTableRow
 *
 * @package Seka\Portal\Modul\Statistiky\Repositories\Eloquent\Transformers\Summary\DataTables\Technik\PocetKontrolPodlaHodnotenia
 */
final class PocetKontrolPodlaHodnoteniaDataTableRow implements DataTableRowContract, Arrayable, Jsonable, JsonSerializable
{
    /**
     * @var array
     */
    private array $hodnoteniePocet = [];

    /**
     * @var string
     */
    private string $technik;

    /**
     * @var int
     */
    private int $spolu = 0;

    /**
     * @param string $technik
     */
    public function __construct(string $technik)
    {
        $this->technik = $technik;

        foreach (HodnotenieEmisnejKontrolyEnum::cases() as $case) {
            $this->hodnoteniePocet[$case->value] = 0;
        }
    }

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaHodnotenia $pocetKontrolPodlaHodnotenia
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaHodnotenia $pocetKontrolPodlaHodnotenia): self
    {
        $this->hodnoteniePocet[$pocetKontrolPodlaHodnotenia->hodnotenie()->value] = $pocetKontrolPodlaHodnotenia->pocet();
        $this->spolu += $pocetKontrolPodlaHodnotenia->pocet();

        return $this;
    }

    /**
     * @return string
     */
    public function technik(): string
    {
        return $this->technik;
    }

    /**
     * @param int $options
     *
     * @return string
     */
    public function toJson($options = 0): string
    {
        return json_encode($this->toArray(), $options);
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return [
            'label' => $this->technik,
            'data' => [
                'hodnotenie' => $this->hodnoteniePocet,
                'spolu' => $this->spolu,
            ],
        ];
    }

    /**
     * @return array
     */
    public function jsonSerialize(): array
    {
        return $this->toArray();
    }
}
