<?php

namespace App\Support\Marketplace;

use App\Enums\MarketplaceFeeScope;
use App\Enums\MarketplaceFeeValueType;

readonly class MarketplaceFeeRule
{
    public function __construct(
        public MarketplaceFeeScope $scope,
        public MarketplaceFeeValueType $valueType,
        public float $value,
    ) {}

    /**
     * @param  array{scope: string, value_type: string, value: float|int|string}  $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            scope: MarketplaceFeeScope::from($data['scope']),
            valueType: MarketplaceFeeValueType::from($data['value_type']),
            value: (float) $data['value'],
        );
    }

    public static function defaultPercent(float $value): self
    {
        return new self(
            scope: MarketplaceFeeScope::TRANSACTION,
            valueType: MarketplaceFeeValueType::PERCENT,
            value: $value,
        );
    }

    /**
     * @return array{scope: string, value_type: string, value: float}
     */
    public function toArray(): array
    {
        return [
            'scope' => $this->scope->value,
            'value_type' => $this->valueType->value,
            'value' => $this->value,
        ];
    }
}
