<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Model;

/**
 * Value object grouping the payment parameters extracted in
 * ProcessPayment::execute() and shared by the payment-processing helpers.
 * Populated once in the constructor; fields are public for low-overhead access and callers
 * must not mutate them afterwards (it is a plain DTO, not an entity).
 *
 * Built once in execute() and passed to the helper methods so they do not
 * exceed the ParameterList threshold. Carries the exact values previously
 * passed positionally — no transformation.
 */
class PaymentProcessingContext
{
    /**
     * @var array
     */
    public $orderData;

    /**
     * @var mixed
     */
    public $paymentMethodType;

    /**
     * @var mixed
     */
    public $blikCode;

    /**
     * @var mixed
     */
    public $cardToken;

    /**
     * @var mixed
     */
    public $cardLast4;

    /**
     * @var mixed
     */
    public $cardProvider;

    /**
     * @var mixed
     */
    public $cardName;

    /**
     * @var mixed
     */
    public $saveCard;

    /**
     * @var mixed
     */
    public $useSavedCard;

    /**
     * @var mixed
     */
    public $savedCardId;

    /**
     * @var mixed
     */
    public $shopUserEmail;

    /**
     * @var mixed
     */
    public $amount;

    /**
     * @var mixed
     */
    public $currency;

    /**
     * @param array $orderData
     */
    public function __construct(
        array $orderData
    ) {
        // Field extraction mirrors the original ProcessPayment::execute() defaults exactly
        $this->orderData = $orderData;
        $this->paymentMethodType = $orderData['paymentMethod'] ?? 'blik';
        $this->blikCode = $orderData['blikCode'] ?? null;
        $this->cardToken = $orderData['cardToken'] ?? null;
        $this->cardLast4 = $orderData['cardLast4'] ?? null;
        $this->cardProvider = $orderData['cardProvider'] ?? 'visa';
        $this->cardName = $orderData['cardName'] ?? 'Card Holder';
        $this->saveCard = $orderData['saveCard'] ?? false;
        $this->useSavedCard = $orderData['useSavedCard'] ?? false;
        $this->savedCardId = $orderData['savedCardId'] ?? null;
        $this->shopUserEmail = $orderData['shopUserEmail'] ?? '';
        $this->amount = $orderData['price'] ?? 0;
        $this->currency = $orderData['currency'] ?? 'PLN';
    }

    /**
     * Map the configured payment method type to the SimplyIN target type
     *
     * @return mixed
     */
    public function getTargetType()
    {
        $targetType = $this->paymentMethodType;
        if ($this->paymentMethodType == 'card' || $this->paymentMethodType == 'cc' || $this->paymentMethodType == 'saved_card' || $this->paymentMethodType == 'add_card') {
            $targetType = 'add_card';
        }

        return $targetType;
    }

    /**
     * Select the first available payment method ID matching the target type
     *
     * @param array $paymentMethods
     * @return mixed|null
     */
    public function selectMethodId($paymentMethods)
    {
        $targetType = $this->getTargetType();
        $selectedMethodId = null;
        foreach ($paymentMethods as $method) {
            if (isset($method['type']) && $method['type'] === $targetType) {
                $selectedMethodId = $method['paymentMethodId'];
                break;
            }
        }

        return $selectedMethodId;
    }

    /**
     * Select the payment method ID honouring a saved card selection
     *
     * @param array $paymentMethods
     * @return mixed|null
     */
    public function selectMethodIdWithSavedCard($paymentMethods)
    {
        if ($this->useSavedCard && $this->savedCardId) {
            return $this->savedCardId;
        }

        return $this->selectMethodId($paymentMethods);
    }
}
