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

export async function POST(req: NextRequest, {params}) {
  try {

    const {slug} = await params;

    const [checkRoute] = await pool.query("SELECT id FROM silo_routes WHERE slug = ?", [slug])
    
    const [rows] = await pool.query<RowDataPacket[]>("SELECT id, slug, created_at, updated_at FROM silo_route_details WHERE id = ? ORDER BY created_at DESC", [checkRoute[0].id])

    if(rows.length > 0)
    {
        return NextResponse.json(
            {
                success: true,
                data: rows
            },
            {
                status: 200
            }
        )
    }
    else{
        return NextResponse.json(
            {
                success: false,
                message: "No Route Found"
            },
            {
                status: 200
            }
        )
    }

  } catch (err: any) {
    return NextResponse.json(
      {
        success: false,
        message: err.message
      },
      {
        status: 500
      }
    )
  }
}