<?php

namespace App\Domain\Pricing\Services;

class PriceResult
{
    public function __construct(
        public readonly int $baseAmount,
        public readonly int $finalAmount,
        public readonly int $totalDiscount,
        public readonly array $appliedRules,
        public readonly ?array $appliedPromotion,
        /**
         * The base_prices row the price came from — null when the base was the
         * product's own member/non-member column, which is a price without a
         * row of its own.
         */
        public readonly ?int $basePriceId,
    ) {}

    /**
     * Discount as a percentage of base amount (e.g., 25.0 for 25%).
     */
    public function discountPercentage(): float
    {
        if ($this->baseAmount <= 0) {
            return 0;
        }

        return round(($this->totalDiscount / $this->baseAmount) * 100, 1);
    }

    public function hasDiscount(): bool
    {
        return $this->totalDiscount > 0;
    }

    /**
     * Snapshot-ready array for freezing on invoice items.
     */
    public function toArray(): array
    {
        return [
            'base_amount' => $this->baseAmount,
            'final_amount' => $this->finalAmount,
            'total_discount' => $this->totalDiscount,
            'discount_percentage' => $this->discountPercentage(),
            'applied_rules' => $this->appliedRules,
            'applied_promotion' => $this->appliedPromotion,
            'base_price_id' => $this->basePriceId,
        ];
    }
}
