<?php

declare (strict_types=1);
namespace Syde\Vendor\Cawl\Inpsyde\WorldlineForWoocommerce\Webhooks\Helper;

use Syde\Vendor\Cawl\OnlinePayments\Sdk\Domain\AmountOfMoney;
use Syde\Vendor\Cawl\OnlinePayments\Sdk\Domain\WebhooksEvent;
class WebhookHelper
{
    /**
     * @psalm-suppress RedundantConditionGivenDocblockType
     */
    public static function reference(WebhooksEvent $webhook) : ?string
    {
        $payment = $webhook->getPayment();
        $refund = $webhook->getRefund();
        $output = null;
        if ($payment) {
            $output = $payment->getPaymentOutput();
        } elseif ($refund) {
            $output = $refund->getRefundOutput();
        }
        if (!$output) {
            return null;
        }
        $ref = $output->getReferences();
        if (!$ref) {
            return null;
        }
        return $ref->getMerchantReference();
    }
    /**
     * @psalm-suppress RedundantConditionGivenDocblockType
     */
    public static function transactionId(WebhooksEvent $webhook) : ?string
    {
        $payment = $webhook->getPayment();
        $refund = $webhook->getRefund();
        if ($payment) {
            return $payment->getId();
        } elseif ($refund) {
            return $refund->getId();
        }
        return null;
    }
    public static function statusCode(WebhooksEvent $webhook) : ?int
    {
        $payment = $webhook->getPayment();
        $refund = $webhook->getRefund();
        $output = null;
        if ($payment) {
            $output = $payment->getStatusOutput();
        } elseif ($refund) {
            $output = $refund->getStatusOutput();
        }
        if (!$output) {
            return null;
        }
        return $output->getStatusCode();
    }
    public static function transactionIdForUi(WebhooksEvent $webhook) : string
    {
        $transactionId = self::transactionId($webhook);
        if (\is_null($transactionId)) {
            return \__('Unknown transaction ID', 'cawl-for-woocommerce');
        }
        return $transactionId;
    }
    public static function paymentCapturedAmount(WebhooksEvent $webhook) : ?AmountOfMoney
    {
        $payment = $webhook->getPayment();
        if (!$payment) {
            return null;
        }
        $output = $payment->getPaymentOutput();
        if (!$output) {
            return null;
        }
        return $output->getAcquiredAmount();
    }
    public static function cancelledPaymentAmount(WebhooksEvent $webhook) : ?AmountOfMoney
    {
        if ($webhook->type !== 'payment.cancelled') {
            return null;
        }
        $payment = $webhook->getPayment();
        if (!$payment) {
            return null;
        }
        if ($payment->getStatus() !== 'CANCELLED') {
            return null;
        }
        $paymentOutput = $payment->getPaymentOutput();
        if (!$paymentOutput) {
            return null;
        }
        return $paymentOutput->getAmountOfMoney();
    }
    public static function paymentRefundedAmount(WebhooksEvent $webhook) : ?AmountOfMoney
    {
        $refund = $webhook->getRefund();
        if (!$refund) {
            return self::cancelledPaymentAmount($webhook);
        }
        $output = $refund->getRefundOutput();
        if (!$output) {
            return null;
        }
        return $output->getAmountOfMoney();
    }
    public static function cleanupId(?string $id) : ?string
    {
        if ($id === null) {
            return null;
        }
        if (\str_contains($id, '_')) {
            // ID like 9322370898_1
            $parts = \explode('_', $id);
            return $parts[0];
        }
        return \substr($id, 0, 10);
    }
}
