<?php

namespace App\Data\Pagarme\Request\OrderRequestData;

use App\Data\Pagarme\PagarmeData;
use App\Data\Pagarme\Request\CustomerRequestData\CustomerRequestData as CustomerData;
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderCreditCardData;
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPaymentData;
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderPixData;
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderSplitData;
use App\Data\Pagarme\Request\OrderRequestData\OrderPaymentData\OrderSplitOptionsData;
use App\Models\PaymentSplit;
use Illuminate\Support\Collection;

final readonly class OrderRequestData extends PagarmeData
{
    /**
     * @param  OrderItemData[]  $items
     * @param  OrderPaymentData[]  $payments
     */

    public function __construct(
        public string  $code,
        public array   $items,
        public array   $payments,
        public array   $metadata,
        public ?string $customerId = null,
        public ?string $channel    = null,
        public bool    $closed     = true,

        public ?CustomerData $customer = null,
    ) {
        self::requireFilled($this->code, 'code');

        if (empty($this->items)) {
            throw new \InvalidArgumentException('items nao pode estar vazio.');
        }

        if (empty($this->payments)) {
            throw new \InvalidArgumentException('payments nao pode estar vazio.');
        }

        if (! $this->customerId && ! $this->customer) {
            throw new \InvalidArgumentException('customer ou customer_id e obrigatorio.');
        }
    }

    //

    public static function amountInCents(float $amount): int
    {
        return (int) round($amount * 100);
    }

    /**
     * @param  OrderSplitData[]|null  $split
     */

    public static function creditCardPaymentMethod(OrderCreditCardData $creditCard, ?array $split = null): OrderPaymentData
    {
        return OrderPaymentData::creditCard($creditCard, $split);
    }

    /**
     * @param  OrderSplitData[]|null  $split
     */

    public static function pixPaymentMethod(OrderPixData $pix, ?array $split = null): OrderPaymentData
    {
        return OrderPaymentData::pix($pix, $split);
    }

    /**
     * @param  Collection<PaymentSplit>  $transfers
     * @return OrderSplitData[]
     */

    public static function splitFromTransfers(Collection $transfers): array
    {
        return $transfers
            ->filter(fn (PaymentSplit $split) => ! empty($split->gateway_transfer_target_reference))
            ->map(function (PaymentSplit $split) {
                return new OrderSplitData(
                    amount:      self::amountInCents((float) $split->gross_amount),
                    recipientId: $split->gateway_transfer_target_reference,
                    type:        'flat',

                    options: new OrderSplitOptionsData(
                        chargeProcessingFee: false,
                        chargeRemainderFee:  false,
                        liable:              false,
                    ),
                );
            })
            ->values()
            ->all();
    }

    //

    public function toArray(): array
    {
        return $this->filterFilledRecursive([
            'code'        => $this->code,
            'items'       => $this->items,
            'payments'    => $this->payments,
            'closed'      => $this->closed,
            'metadata'    => $this->metadata,
            'customer_id' => $this->customerId,
            'channel'     => $this->channel,
            'customer'    => $this->customer,
        ]);
    }
}
