import { NextResponse } from 'next/server';
import pool from '../../../../../lib/db/db';

export async function GET(request: Request) {
  try {
    // Simple query to get all countries
    const query = 'SELECT id, code, country, currency, currency_symbol FROM countries ORDER BY country ASC';
    
    const [countries] = await pool.query(query);
    
    return NextResponse.json({
      success: true,
      data: countries,
      count: Array.isArray(countries) ? countries.length : 0
    });
    
  } catch (error) {
    console.error('Error fetching countries:', error);
    return NextResponse.json(
      {
        success: false,
        message: 'Failed to fetch countries',
        error: error instanceof Error ? error.message : 'Unknown error'
      },
      { status: 500 }
    );
  }
}