<?php

declare(strict_types=1);

namespace App\Framework\Svg\ValueObjects\Text;

use App\Framework\Exception\ErrorCode;
use App\Framework\Exception\FrameworkException;

/**
 * Value Object representing font size
 */
final readonly class FontSize
{
    public function __construct(
        public float $value,
        public FontSizeUnit $unit = FontSizeUnit::PX
    ) {
        if ($value <= 0) {
            throw FrameworkException::create(
                ErrorCode::VAL_INVALID_FORMAT,
                'Font size must be greater than 0'
            )->withData(['value' => $value]);
        }
    }

    /**
     * Named font sizes
     */
    public static function tiny(): self
    {
        return new self(10.0);
    }

    public static function small(): self
    {
        return new self(12.0);
    }

    public static function medium(): self
    {
        return new self(14.0);
    }

    public static function large(): self
    {
        return new self(18.0);
    }

    public static function extraLarge(): self
    {
        return new self(24.0);
    }

    public static function huge(): self
    {
        return new self(32.0);
    }

    /**
     * Scale font size by factor
     */
    public function scale(float $factor): self
    {
        if ($factor <= 0) {
            throw FrameworkException::create(
                ErrorCode::VAL_INVALID_FORMAT,
                'Scale factor must be greater than 0'
            )->withData(['factor' => $factor]);
        }

        return new self($this->value * $factor, $this->unit);
    }

    /**
     * Convert to SVG attribute value
     */
    public function toSvgValue(): string
    {
        return sprintf('%.2f%s', $this->value, $this->unit->value);
    }

    public function __toString(): string
    {
        return $this->toSvgValue();
    }
}
