import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { verifyAdminPermission } from '@/lib/auth';

// POST - Seed default payment gateways
export async function POST(request: NextRequest) {
  try {
    const authResult = await verifyAdminPermission(request, 'canManageOrders');
    if ('error' in authResult) return authResult.error;

    const defaultGateways = [
      {
        name: 'bKash',
        slug: 'bkash',
        type: 'mobile_banking',
        icon: '📱',
        description: 'bKash mobile banking payment',
        config: JSON.stringify({ number: '', type: 'Send Money' }),
        instructions: JSON.stringify({ number: '01XXXXXXXXX', type: 'Send Money' }),
        sortOrder: 1,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'Nagad',
        slug: 'nagad',
        type: 'mobile_banking',
        icon: '📱',
        description: 'Nagad mobile banking payment',
        config: JSON.stringify({ number: '', type: 'Cash Out' }),
        instructions: JSON.stringify({ number: '01XXXXXXXXX', type: 'Cash Out' }),
        sortOrder: 2,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'Rocket',
        slug: 'rocket',
        type: 'mobile_banking',
        icon: '📱',
        description: 'Rocket mobile banking payment',
        config: JSON.stringify({ number: '', type: 'Send Money' }),
        instructions: JSON.stringify({ number: '01XXXXXXXXX', type: 'Send Money' }),
        sortOrder: 3,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'Visa/Debit Card',
        slug: 'visa-debit-card',
        type: 'card',
        icon: '💳',
        description: 'Visa or Debit card payment',
        config: JSON.stringify({ gateway: 'stripe', apiKey: '', secret: '' }),
        instructions: '',
        sortOrder: 4,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'Master Card',
        slug: 'master-card',
        type: 'card',
        icon: '💳',
        description: 'MasterCard payment',
        config: JSON.stringify({ gateway: 'stripe', apiKey: '', secret: '' }),
        instructions: '',
        sortOrder: 5,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'PayPal',
        slug: 'paypal',
        type: 'digital_wallet',
        icon: '🅿️',
        description: 'PayPal digital wallet payment',
        config: JSON.stringify({ clientId: '', clientSecret: '', sandboxMode: true }),
        instructions: '',
        sortOrder: 6,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'Google Pay',
        slug: 'google-pay',
        type: 'digital_wallet',
        icon: '🟢',
        description: 'Google Pay digital wallet payment',
        config: JSON.stringify({ merchantId: '' }),
        instructions: '',
        sortOrder: 7,
        isSystem: true,
        isActive: false,
      },
      {
        name: 'Crypto',
        slug: 'crypto',
        type: 'crypto',
        icon: '₿',
        description: 'Cryptocurrency payment (BTC, ETH, etc.)',
        config: JSON.stringify({ walletAddress: '', network: 'BTC' }),
        instructions: '',
        sortOrder: 8,
        isSystem: true,
        isActive: false,
      },
    ];

    let created = 0;
    let skipped = 0;

    for (const gw of defaultGateways) {
      const existing = await db.paymentGateway.findUnique({ where: { slug: gw.slug } });
      if (existing) {
        skipped++;
        continue;
      }
      await db.paymentGateway.create({ data: gw });
      created++;
    }

    return NextResponse.json({
      success: true,
      message: `Seeded ${created} gateways, ${skipped} already existed`,
      created,
      skipped,
    });
  } catch (error) {
    console.error('Payment gateway seed error:', error);
    return NextResponse.json({ success: false, error: 'Failed to seed payment gateways' }, { status: 500 });
  }
}
