<?php

declare(strict_types=1);

namespace App\Framework\Svg\ValueObjects\Geometry;

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

/**
 * Value Object representing a radius value for circles and rounded corners
 */
final readonly class Radius
{
    public function __construct(
        public float $value
    ) {
        if ($value < 0) {
            throw FrameworkException::create(
                ErrorCode::VAL_INVALID_FORMAT,
                'Radius cannot be negative'
            )->withData(['value' => $value]);
        }
    }

    public static function zero(): self
    {
        return new self(0.0);
    }

    /**
     * Get diameter (2 * radius)
     */
    public function getDiameter(): float
    {
        return $this->value * 2;
    }

    /**
     * Get circumference (2 * π * radius)
     */
    public function getCircumference(): float
    {
        return 2 * M_PI * $this->value;
    }

    /**
     * Get area (π * radius²)
     */
    public function getArea(): float
    {
        return M_PI * $this->value * $this->value;
    }

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

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

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

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