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

  export async function GET(request: NextRequest) {
    // Check authorization header
    const incomingHeaders = request.headers.get("authorization");

    if (incomingHeaders !== process.env.APP_API_KEY) {
        return NextResponse.json(
            { success: false, message: 'Unauthorized Access' },
            { status: 401 }
        );
    }

  try {
    const [countries] = await pool.query(
      "SELECT * FROM countries_with_phone ORDER BY name"
    );
    
    return NextResponse.json({
      success: true,
      data: countries
    });
  } catch (error) {
    return NextResponse.json(
      { success: false, message: 'Error fetching countries' },
      { status: 500 }
    );
  }
}