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

namespace Simplyin\Payment\Model;

use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Framework\DataObject;

/**
 * SimplyIN payment method model
 *
 * Handles payment method logic and data assignment
 */
class Payment extends AbstractMethod
{
    public const CODE = 'simplyin_payment';

    /**
     * Supported currencies for SimplyIN payment
     * BLIK only works with PLN, card payments work with other currencies
     */
    public const SUPPORTED_CURRENCIES = ['PLN'];

    /**
     * @var string
     */
    protected $_code = self::CODE;

    /**
     * @var bool
     */
    protected $_isGateway = true;

    /**
     * @var bool
     */
    protected $_canCapture = true;

    /**
     * @var bool
     */
    protected $_canCapturePartial = true;

    /**
     * @var bool
     */
    protected $_canRefund = true;

    /**
     * @var bool
     */
    protected $_canRefundInvoicePartial = true;

    /**
     * @var bool
     */
    protected $_canVoid = true;

    /**
     * @var bool
     */
    protected $_canAuthorize = true;

    /**
     * Check whether payment method can be used
     *
     * @param CartInterface|null $quote
     * @return bool
     */
    public function isAvailable(?CartInterface $quote = null): bool
    {
        if (!parent::isAvailable($quote)) {
            return false;
        }

        // SimplyIN payment is available for all currencies
        // But BLIK will be filtered out on frontend for non-PLN currencies
        return true;
    }

    /**
     * Check if currency is supported for BLIK payments
     *
     * BLIK only works with PLN currency
     *
     * @param string $currencyCode
     * @return bool
     */
    public function isBlikAvailableForCurrency(string $currencyCode): bool
    {
        return strtoupper($currencyCode) === 'PLN';
    }

    /**
     * Check if currency is fully supported (all payment methods)
     *
     * @param string $currencyCode
     * @return bool
     */
    public function isFullySupportedCurrency(string $currencyCode): bool
    {
        return in_array(strtoupper($currencyCode), self::SUPPORTED_CURRENCIES, true);
    }

    /**
     * Assign data to info model instance
     *
     * @param DataObject|mixed $data
     * @return $this
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function assignData(DataObject $data): static
    {
        parent::assignData($data);

        $additionalData = $data->getAdditionalData();

        $fields = [
            'simplyin_order_id',
            'simplyin_payment_id',
            'simplyin_provider_payment_id',
            'simplyin_payment_status',
            'simplyin_registered_at',
            'simplyin_redirect_url',
            'simplyin_redirect_iframe_allowed',
        ];

        foreach ($fields as $field) {
            if (isset($additionalData[$field])) {
                $this->getInfoInstance()->setAdditionalInformation($field, $additionalData[$field]);
            }
        }

        return $this;
    }
}
