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


export async function POST(request: NextRequest){

    try{

        const { searchParams } = new URL(request.url);
        const search = searchParams.get('query') || '';

        const searchPattern = `%${search}%`;

        const [rows] = await pool.query(
            `SELECT
            c.*,
            u.logo as image,
            u.name as university,
            p.title as qualification,
            c.yearly_fee as fee_with_symbol,
            c.application_fee as application_fee_with_symbol
            FROM courses c
            LEFT JOIN university_details u ON c.university_id = u.id
            LEFT JOIN posts p ON p.id = c.qualification
            WHERE c.name LIKE ? LIMIt 20`, [searchPattern]);

        return NextResponse.json(
            {
                message: "Find",
                data: rows
            },
            {
                status: 200
            }
        )

    } catch (erroe){
        return NextResponse.json(
            {
                message: "Error Fetching the results",
                success: false
            },
            {
                status: 500
            }
        )
    }
}