<?php

declare(strict_types=1);

namespace KupShop\GraphQLBundle\ApiShared\Types\Product;

use KupShop\GraphQLBundle\ApiAdmin\Annotation\Module;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

/**
 * Měrná jednotka produktu.
 *
 * @Type()
 */
class ProductUnit
{
    private array $unit;

    public function __construct(array $unit)
    {
        $this->unit = $unit;
    }

    #[Field(description: 'ID jednotky')]
    public function getId(): int
    {
        return $this->unit['id'];
    }

    #[Field(description: 'Název jednotky')]
    public function getName(): string
    {
        return $this->unit['short_name'] ?? 'ks';
    }

    #[Field(description: 'Název jednotky v administraci')]
    public function getAdminName(): ?string
    {
        return $this->unit['short_name_admin'];
    }

    #[Field(description: 'Dlouhý název jednotky')]
    public function getLongName(): ?string
    {
        return !empty($this->unit['long_name']) ? $this->unit['long_name'] : null;
    }

    #[Field(description: 'Počet desetiných míst')]
    #[Module(\Modules::PRODUCTS, \Modules::SUB_UNITS_FLOAT)]
    public function getPiecesPrecision(): ?float
    {
        return !empty($this->unit['pieces_precision']) ? $this->unit['pieces_precision'] : null;
    }

    #[Field(description: 'Přepočet na')]
    public function getRecalculateTo(): ?float
    {
        return !empty($this->unit['recalculate_to']) ? $this->unit['recalculate_to'] : null;
    }

    #[Field(description: 'Množství')]
    public function getMeasureQuantity(): ?float
    {
        return !empty($this->unit['measure_quantity']) ? floatval($this->unit['measure_quantity']) : null;
    }
}
