// app/api/wallets/route.ts
import { NextResponse } from 'next/server';

import { prisma } from '@/lib/prisma';

export async function GET() {
  try {
    const wallets = await prisma.wallet.findMany({
      orderBy: { createdAt: 'desc' }
    });
    return NextResponse.json(wallets);
  } catch (error) {
    console.error('Error fetching wallets:', error);
    return NextResponse.json({ error: 'Failed to fetch wallets' }, { status: 500 });
  }
}

// POST /api/wallets - Create a new wallet
export async function POST(request: Request) {
  try {
    const body = await request.json();
    
    // Validate input
    const { network, name, address, description } = body;
    
    if (!network || !name || !address) {
      return NextResponse.json(
        { error: 'Network, name, and address are required' },
        { status: 400 }
      );
    }
    
    const wallet = await prisma.wallet.create({
      data: {
        network,
        name, 
        address,
        description
      }
    });
    
    return NextResponse.json(wallet, { status: 201 });
  } catch (error) {
    console.error('Error creating wallet:', error);
    return NextResponse.json({ error: 'Failed to create wallet' }, { status: 500 });
  }
}