<?php

declare(strict_types=1);

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

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

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

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

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

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

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

    /**
     * @param \Seka\Portal\Modul\Statistiky\Entities\Zaznam\ValueObjects\PocetKontrolPodlaStavu $pocetKontrolPodlaStavu
     *
     * @return $this
     */
    public function add(PocetKontrolPodlaStavu $pocetKontrolPodlaStavu): self
    {
        $this->stavPocet[$pocetKontrolPodlaStavu->stav()->value] = $pocetKontrolPodlaStavu->pocet();
        $this->spolu += $pocetKontrolPodlaStavu->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' => [
                'stav' => $this->stavPocet,
                'spolu' => $this->spolu,
            ],
        ];
    }

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