/**
 * Add Korapay and Cryptomus Payment Methods
 * Run with: node scripts/add-payment-methods.js
 */

const mysql = require('mysql2/promise');
require('dotenv').config();

async function addPaymentMethods() {
    let connection;

    try {
        // Create database connection
        connection = await mysql.createConnection({
            host: process.env.DB_HOST,
            user: process.env.DB_USER,
            password: process.env.DB_PASSWORD,
            database: process.env.DB_NAME,
            port: process.env.DB_PORT
        });

        console.log('✅ Connected to database');

        // Check if methods already exist
        const [existing] = await connection.execute(
            "SELECT code FROM payment_methods WHERE code IN ('korapay', 'cryptomus')"
        );

        if (existing.length > 0) {
            console.log('⚠️  Payment methods already exist. Updating instead...');

            // Update existing methods
            await connection.execute(`
                UPDATE payment_methods SET
                    name = 'Korapay',
                    type = 'api',
                    description = 'Pay with bank transfer, debit card, or mobile money via Korapay',
                    transaction_fee_percentage = 1.5,
                    transaction_fee_fixed = 0,
                    transaction_fee_cap = 2000,
                    min_amount = 100,
                    max_amount = 5000000,
                    is_active = 1,
                    is_automated = 1,
                    requires_proof = 0,
                    processing_time_minutes = 0,
                    instructions = 'You will be redirected to Korapay secure checkout. Supports bank transfer, debit cards (Visa, Mastercard, Verve), and mobile money. Payment is instant and secure.',
                    display_order = 2,
                    updated_at = NOW()
                WHERE code = 'korapay'
            `);

            await connection.execute(`
                UPDATE payment_methods SET
                    name = 'Cryptocurrency (USDT, BTC, ETH)',
                    type = 'crypto',
                    description = 'Pay with Bitcoin, Ethereum, USDT, USDC, BNB, and 100+ cryptocurrencies',
                    transaction_fee_percentage = 0.4,
                    transaction_fee_fixed = 0,
                    transaction_fee_cap = NULL,
                    min_amount = 100,
                    max_amount = 10000000,
                    is_active = 1,
                    is_automated = 1,
                    requires_proof = 0,
                    processing_time_minutes = 10,
                    instructions = 'Pay with cryptocurrency. Supports Bitcoin (BTC), Ethereum (ETH), USDT, USDC, BNB, and 100+ coins. Fast, secure, and no chargebacks. Payment confirms in 2-10 minutes.',
                    display_order = 5,
                    updated_at = NOW()
                WHERE code = 'cryptomus'
            `);

            console.log('✅ Payment methods updated');
        } else {
            // Insert new methods
            await connection.execute(`
                INSERT INTO payment_methods (
                    name, code, type, description,
                    transaction_fee_percentage, transaction_fee_fixed, transaction_fee_cap,
                    min_amount, max_amount,
                    is_active, is_automated, requires_proof,
                    processing_time_minutes, instructions, display_order,
                    created_at, updated_at
                ) VALUES
                (
                    'Korapay',
                    'korapay',
                    'api',
                    'Pay with bank transfer, debit card, or mobile money via Korapay',
                    1.5, 0, 2000,
                    100, 5000000,
                    1, 1, 0,
                    0,
                    'You will be redirected to Korapay secure checkout. Supports bank transfer, debit cards (Visa, Mastercard, Verve), and mobile money. Payment is instant and secure.',
                    2,
                    NOW(), NOW()
                ),
                (
                    'Cryptocurrency (USDT, BTC, ETH)',
                    'cryptomus',
                    'crypto',
                    'Pay with Bitcoin, Ethereum, USDT, USDC, BNB, and 100+ cryptocurrencies',
                    0.4, 0, NULL,
                    100, 10000000,
                    1, 1, 0,
                    10,
                    'Pay with cryptocurrency. Supports Bitcoin (BTC), Ethereum (ETH), USDT, USDC, BNB, and 100+ coins. Fast, secure, and no chargebacks. Payment confirms in 2-10 minutes.',
                    5,
                    NOW(), NOW()
                )
            `);

            console.log('✅ Payment methods inserted');
        }

        // Verify
        const [methods] = await connection.execute(`
            SELECT id, name, code, type, is_active, transaction_fee_percentage, min_amount, max_amount
            FROM payment_methods
            WHERE code IN ('korapay', 'cryptomus')
            ORDER BY code
        `);

        console.log('\n📊 Payment Methods Added:');
        console.table(methods);

        console.log('\n✅ Setup complete!');
        console.log('   - Korapay: 1.5% fee (max ₦2,000)');
        console.log('   - Cryptomus: 0.4% fee');

    } catch (error) {
        console.error('❌ Error:', error.message);
        process.exit(1);
    } finally {
        if (connection) {
            await connection.end();
        }
    }
}

// Run
addPaymentMethods();
