<?php

namespace App\PaymentGateways;

use App\Models\PaymentGateway;
use App\PaymentGateways\Contracts\BasePaymentGateway;

class CryptoGateway extends BasePaymentGateway
{
    public function getCode(): string
    {
        return 'crypto';
    }

    public function getName(): string
    {
        return 'Cryptocurrency';
    }

    public function getDescription(): string
    {
        return 'Pay with Bitcoin, Ethereum, USDT and other cryptocurrencies.';
    }

    public function getVersion(): string
    {
        return '1.0.0';
    }

    public function getAuthor(): string
    {
        return 'AutoDiag';
    }

    public function getConfigFields(): array
    {
        return [
            [
                'key' => 'wallet_address_btc',
                'label' => 'Bitcoin Wallet Address',
                'type' => 'text',
                'required' => false,
            ],
            [
                'key' => 'wallet_address_eth',
                'label' => 'Ethereum Wallet Address',
                'type' => 'text',
                'required' => false,
            ],
            [
                'key' => 'wallet_address_usdt',
                'label' => 'USDT Wallet Address',
                'type' => 'text',
                'required' => false,
            ],
            [
                'key' => 'accepted_coins',
                'label' => 'Accepted Coins',
                'type' => 'text',
                'required' => false,
            ],
        ];
    }

    public function validateConfig(array $config): array
    {
        return [
            'wallet_address_btc' => $config['wallet_address_btc'] ?? '',
            'wallet_address_eth' => $config['wallet_address_eth'] ?? '',
            'wallet_address_usdt' => $config['wallet_address_usdt'] ?? '',
            'accepted_coins' => $config['accepted_coins'] ?? 'Bitcoin, Ethereum, USDT',
        ];
    }

    public function process(array $orderData, PaymentGateway $gateway): array
    {
        $config = $gateway->config ?? [];

        return [
            'success' => true,
            'status' => 'pending',
            'transaction_id' => 'CRYPTO-' . strtoupper(substr(md5(uniqid()), 0, 12)),
            'message' => 'Order placed. Send payment to the provided wallet address.',
            'wallets' => [
                'bitcoin' => $config['wallet_address_btc'] ?? '',
                'ethereum' => $config['wallet_address_eth'] ?? '',
                'usdt' => $config['wallet_address_usdt'] ?? '',
            ],
        ];
    }
}
