import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';

// GET /api/wallets/[id] - Get a specific wallet
export async function GET(
  request: Request,
  { params }: { params: Promise<{ id: string }> } // Change to string
) {
  try {
    const { id } = await params;
    const numericId = parseInt(id, 10); // Convert to number

    if (isNaN(numericId)) {
      return NextResponse.json({ error: 'Invalid ID format' }, { status: 400 });
    }

    const wallet = await prisma.wallet.findUnique({
      where: { id: numericId } // Use converted number
    });

    if (!wallet) {
      return NextResponse.json({ error: 'Wallet not found' }, { status: 404 });
    }

    return NextResponse.json(wallet);
  } catch (error) {
    console.error('Error fetching wallet:', error);
    return NextResponse.json({ error: 'Failed to fetch wallet' }, { status: 500 });
  }
}

// PUT /api/wallets/[id] - Update a wallet
export async function PUT(
  request: Request,
  { params }: { params: Promise<{ id: string }> } // Change to string
) {
  try {
    const { id } = await params;
    const numericId = parseInt(id, 10); // Convert to number

    if (isNaN(numericId)) {
      return NextResponse.json({ error: 'Invalid ID format' }, { status: 400 });
    }

    const body = await request.json();

    // Validate input
    const { network, name, address, description } = body;

    if (!network && !name && !address && description === undefined) {
      return NextResponse.json(
        { error: 'At least one field must be provided for update' },
        { status: 400 }
      );
    }

    // Check if wallet exists
    const existingWallet = await prisma.wallet.findUnique({
      where: { id: numericId } // Use converted number
    });

    if (!existingWallet) {
      return NextResponse.json({ error: 'Wallet not found' }, { status: 404 });
    }

    // Update wallet
    const updateData: any = {};
    if (network) updateData.network = network;
    if (name) updateData.name = name;
    if (address) updateData.address = address;
    if (description !== undefined) updateData.description = description;

    const updatedWallet = await prisma.wallet.update({
      where: { id: numericId }, // Use converted number
      data: updateData
    });

    return NextResponse.json(updatedWallet);
  } catch (error) {
    console.error('Error updating wallet:', error);
    return NextResponse.json({ error: 'Failed to update wallet' }, { status: 500 });
  }
}

// DELETE /api/wallets/[id] - Delete a wallet
export async function DELETE(
  request: Request,
  { params }: { params: Promise<{ id: string }> } // Change to string
) {
  try {
    const { id } = await params;
    const numericId = parseInt(id, 10); // Convert to number

    if (isNaN(numericId)) {
      return NextResponse.json({ error: 'Invalid ID format' }, { status: 400 });
    }

    // Check if wallet exists
    const existingWallet = await prisma.wallet.findUnique({
      where: { id: numericId } // Use converted number
    });

    if (!existingWallet) {
      return NextResponse.json({ error: 'Wallet not found' }, { status: 404 });
    }

    // Delete wallet
    await prisma.wallet.delete({
      where: { id: numericId } // Use converted number
    });

    return NextResponse.json({ message: 'Wallet deleted successfully' });
  } catch (error) {
    console.error('Error deleting wallet:', error);
    return NextResponse.json({ error: 'Failed to delete wallet' }, { status: 500 });
  }
}