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

export async function GET(req) {
  const connection = await pool.getConnection();

  try {
    const [videos] = await connection.query<RowDataPacket[]>(`
      SELECT 
        id,
        title,
        description,
        thumbnail,
        video_id,
        url,
        published_at,
        view_count,
        duration,
        is_active,
        featured,
        country_id,
        created_at
      FROM youtube_videos 
      WHERE is_active = 1
      ORDER BY 
        featured DESC,
        published_at DESC
    `);

    return NextResponse.json(
      {
        success: true,
        data: videos,
        count: videos.length,
        message: 'Videos retrieved successfully'
      },
      { status: 200 }
    );

  } catch (err) {
    console.error('Videos API Error:', err);
    return NextResponse.json(
      {
        success: false,
        message: err.message,
        data: []
      },
      { status: 500 }
    );
  } finally {
    connection.release();
  }
}