<?php

namespace App\Billing\ValueObject;

final readonly class Money
{
    public function __construct(public int $rials)
    {
        if ($rials < 0) {
            throw new \InvalidArgumentException('Money cannot be negative');
        }
    }

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

    public function add(Money $o): self
    {
        return new self($this->rials + $o->rials);
    }

    public function sub(Money $o): self
    {
        return new self(max(0, $this->rials - $o->rials));
    }

    public function percent(float $p): self
    {
        return new self((int) round($this->rials * $p / 100));
    }

    public function min(Money $o): self
    {
        return new self(min($this->rials, $o->rials));
    }
}
