<?php

namespace App\Data\Pagarme\Recipient\Parts\Request;

use App\Data\Pagarme\PagarmeData;

final readonly class AutomaticAnticipationSettingsData extends PagarmeData
{
    public function __construct(
        public bool    $enabled,
        public ?string $type             = null,
        public ?int    $volumePercentage = null,
        public ?int    $delay            = null,
        public ?array  $days             = null,
    ) {
        if ($this->type !== null) {
            self::requireIn($this->type, ['full', '1025'], 'automatic_anticipation_settings.type');
        }

        if ($this->volumePercentage !== null && ($this->volumePercentage < 0 || $this->volumePercentage > 100)) {
            throw new \InvalidArgumentException('automatic_anticipation_settings.volume_percentage deve estar entre 0 e 100.');
        }

        if ($this->delay !== null && $this->delay < 1) {
            throw new \InvalidArgumentException('automatic_anticipation_settings.delay deve ser maior ou igual a um.');
        }
    }

    public function toArray(): array
    {
        return $this->filterFilledRecursive([
            'enabled'           => $this->enabled,
            'type'              => $this->type,
            'volume_percentage' => $this->volumePercentage,
            'delay'             => $this->delay,
            'days'              => $this->days,
        ]);
    }
}
