<?php

namespace TbiBank\FusionPay\Model\Payment;

use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Store\Model\ScopeInterface;
use Magento\Sales\Model\Order;

/**
 * FusionPay Payment Method Model
 * 
 * This class defines the FusionPay payment method for the TbiBank_FusionPay module.
 * It extends Magento's AbstractMethod class, which provides the basic functionalities
 * required by a payment method model in Magento.
 */
class FusionPay extends AbstractMethod
{
    /**
     * Payment method code
     * 
     * This code should be unique across all payment methods. It's used by Magento
     * to identify this particular payment method.
     *
     * @var string
     */
    protected $_code = 'fusionpay';

    /**
     * Payment method type flag
     * 
     * This flag indicates whether the payment method is an offline payment method.
     * Setting it to true means that this payment method does not involve any online
     * transaction processing.
     *
     * @var bool
     */
    protected $_isOffline = true;

    /**
     * Signal that initialize() must be called during order placement.
     *
     * @var bool
     */
    protected $_isInitializeNeeded = true;

    /**
     * Support the 'order' payment action (no auth/capture), so Magento won't error when processing 'order'.
     *
     * @var bool
     */
    protected $_canOrder = true;

    /**
     * Explicitly allow 'order' action.
     */
    public function canOrder()
    {
        return true;
    }

    /**
     * Whether initialize() is required for this payment method
     *
     * @return bool
     */
    public function isInitializeNeeded()
    {
        return true;
    }

    /**
     * No-op order action: do not authorize/capture; just allow order placement to proceed.
     *
     * @param \Magento\Payment\Model\InfoInterface $payment
     * @param float $amount
     * @return $this
     */
    public function order(\Magento\Payment\Model\InfoInterface $payment, $amount)
    {
        // Nothing to do for offline "order" flow
        return $this;
    }

    // You can add additional properties and methods as needed to implement
    // specific functionalities of your payment method.
    public function getTitle()
    {
        // Retrieve the title from configuration and wrap it in __() for translation.
        $title = $this->_scopeConfig->getValue(
            'payment/fusionpay/payment_name',
            ScopeInterface::SCOPE_STORE
        );
        return __($title);
    }

    public function getPaymentDescription()
    {
        // Similarly, for the payment description.
        $description = $this->_scopeConfig->getValue(
            'payment/fusionpay/payment_description',
            ScopeInterface::SCOPE_STORE
        );
        return __($description);
    }

    /**
     * Ensure a valid payment action is returned; default to 'order' to avoid auto-capture/invoice.
     *
     * @return string
     */
    public function getConfigPaymentAction()
    {
        // This method returns the action used during order placement. By forcing 'order',
        // Magento will call initialize() and will not attempt to authorize/capture automatically.
        return self::ACTION_ORDER;
    }

    /**
     * Initialize payment method
     * 
     * @param string $paymentAction
     * @param object $stateObject
     * @return $this
     */
    public function initialize($paymentAction, $stateObject)
    {
        // Get the configured order status from admin settings
        $configuredStatus = $this->_scopeConfig->getValue(
            'payment/' . $this->_code . '/default_order_status',
            ScopeInterface::SCOPE_STORE
        );

        // Fallback to 'pending' if nothing is configured
        if (empty($configuredStatus)) {
            $configuredStatus = 'pending';
        }

        // Set the order status and state
        $stateObject->setStatus($configuredStatus);
        $stateObject->setState($this->getStateForStatus($configuredStatus));
        $stateObject->setIsNotified(false);

        return $this;
    }

    /**
     * Get appropriate state for the given status
     * 
     * @param string $status
     * @return string
     */
    private function getStateForStatus($status)
    {
        $statusToStateMap = [
            'pending' => 'new',
            'pending_payment' => 'pending_payment',
            'processing' => 'processing',
            'complete' => 'complete',
            'canceled' => 'canceled',
            'closed' => 'closed',
            'holded' => 'holded'
        ];

        return $statusToStateMap[$status] ?? 'new';
    }
}
