<?php
declare(strict_types=1);

namespace App\Services\Payment;

abstract class AbstractPaymentProvider implements PaymentProviderInterface
{
    public function __construct(protected array $config = [], protected bool $testMode = true)
    {
    }

    public function createPaymentSession(array $order): array
    {
        return [
            'ok' => false,
            'provider' => $this->getProviderKey(),
            'provider_name' => $this->getProviderName(),
            'status' => 'not_integrated',
            'message' => $this->getProviderName() . ' adapter hazir; canli API entegrasyonu icin TODO.',
            'todo' => 'Hosted form, tokenization ve 3D Secure akisi provider dokumanina gore eklenecek.',
        ];
    }

    public function verifyCallback(array $request): array
    {
        return [
            'ok' => false,
            'provider' => $this->getProviderKey(),
            'signature_valid' => false,
            'status' => 'failed',
            'message' => $this->getProviderName() . ' callback dogrulama TODO.',
            'payload' => $this->sanitizePayload($request),
        ];
    }

    public function refund(array $order): array
    {
        return [
            'ok' => false,
            'provider' => $this->getProviderKey(),
            'message' => $this->getProviderName() . ' refund akisi TODO.',
        ];
    }

    public function supportsInstallment(): bool
    {
        return true;
    }

    public function supports3DSecure(): bool
    {
        return true;
    }

    public function supportsSavedCard(): bool
    {
        return false;
    }

    public function supportsTokenization(): bool
    {
        return false;
    }

    protected function getProviderKey(): string
    {
        return match (static::class) {
            TestPaymentProvider::class => 'test',
            PayTRPaymentProvider::class => 'paytr',
            IyzicoPaymentProvider::class => 'iyzico',
            ParamPaymentProvider::class => 'param',
            SipayPaymentProvider::class => 'sipay',
            MokaPaymentProvider::class => 'moka',
            PaycellPaymentProvider::class => 'paycell',
            BankPosPaymentProvider::class => 'bank_pos',
            default => 'test',
        };
    }

    protected function boolConfig(string $key, bool $default): bool
    {
        if (!array_key_exists($key, $this->config)) {
            return $default;
        }

        return filter_var($this->config[$key], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? $default;
    }

    protected function sanitizePayload(array $payload): array
    {
        $blocked = [
            'card_number',
            'cardNumber',
            'pan',
            'cc_number',
            'cvv',
            'cvc',
            'security_code',
            'expiry',
            'exp_month',
            'exp_year',
            'card_holder',
            'card_name',
        ];

        foreach ($payload as $key => $value) {
            if (in_array((string) $key, $blocked, true)) {
                unset($payload[$key]);
                continue;
            }

            if (is_array($value)) {
                $payload[$key] = $this->sanitizePayload($value);
            }
        }

        return $payload;
    }
}
