// app/api/internal/blogs/route.js
import { NextRequest, NextResponse } from 'next/server';
import pool from '../../../../lib/db/db';
import { RowDataPacket } from 'mysql2';


export async function GET(request: NextRequest) {

    const connection = await pool.getConnection();

  try{


    const [getStories] = await connection.query<RowDataPacket[]>('SELECT * FROM success_stories ORDER BY story_date DESC');

    if(getStories.length === 0)
    {
        return NextResponse.json({
            success: false,
            error: "No Story found",
        }, { status: 404 });
    }

    return NextResponse.json({
        success: true,
        data: getStories,
    }, { status: 200 });

  } catch (err) {
    console.error("❌ GET API Error:", err);
    return NextResponse.json({ 
      success: false, 
      error: "Failed to fetch blogs", 
      details: err.message 
    }, { status: 500 });
  } finally {
    connection.release();
  }
}