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

// GET - Fetch countries with pagination and search
export async function GET(request) {
  try {

    const [countries] = await pool.query<RowDataPacket[]>('SELECT id, country FROM countries');
    const response = { success: true, data: countries };
    
    return NextResponse.json(response);
  } catch (err) {
    console.error("❌ GET API Error:", err);
    
    // Handle specific MySQL errors
    if (err.code === 'ECONNREFUSED') {
      return NextResponse.json({ 
        success: false,
        error: "Database connection failed",
        details: "Cannot connect to database server"
      }, { status: 500 });
    }
    
    return NextResponse.json({ 
      success: false,
      error: "Failed to fetch countries",
      details: err.message
    }, { status: 500 });
  }
    
}