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

type PageProps = {
    params: Promise<{
      unislug: string;
      courseslug: string;
    }>;
  };

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

    const {unislug, courseslug} = await params // Access query parameter

    if(unislug === "courses"){
        const [getCcourseDetails] = await pool.query<RowDataPacket[]>(
            `SELECT c.id, c.slug, ud.slug as university_slug
            FROM courses c
            LEFT JOIN university_details ud ON c.university_id = ud.id AND is_active = 1
            WHERE c.id = ?`
            , [courseslug])
        if(getCcourseDetails.length > 0)
        {
            return NextResponse.json(
                {
                    success: true,
                    course_slug: getCcourseDetails[0].slug,
                    university_slug: getCcourseDetails[0].university_slug
                }
            )
        }
    }

    const [getUniversityID] = await pool.query("SELECT id, slug AS university_slug FROM university_details WHERE slug = ? AND is_active = 1", [unislug])

    console.log("University ID", getUniversityID)

    const university_id = getUniversityID[0]?.id;
    
    const [rows] = await pool.query<RowDataPacket[]>(
      `SELECT 
        c.id AS id,
        c.name AS name, 
        c.slug AS slug,
        c.duration AS duration,
        c.duration_type AS duration_type,
        c.yearly_fee AS yearly_fee,
        c.scholarship AS scholarship,
        c.university_id AS university_id,
        c.subject_id AS subject_id,
        c.qualification AS qualification,
        c.languages AS languages,
        ud.name AS university_name,
        ud.slug AS university_slug,
        ud.logo AS universityLogo,
        ud.intake AS intake,
        ud.address AS location,
        s.name AS subject_name,
        co.currency AS currency,
        co.currency_symbol AS currency_symbol,
        co.consultation_fee AS consultation_fee,
        co.consultation_fee_discount AS consultation_fee_discount,
        (co.consultation_fee - (co.consultation_fee * (co.consultation_fee_discount / 100))) AS consultation_fee_after_discount,
        p.title AS qualification
      FROM courses c
      LEFT JOIN university_details ud ON c.university_id = ud.id AND is_active = 1
      LEFT JOIN subjects s ON c.subject_id = s.id
      LEFT JOIN countries co ON ud.country = co.country
      LEFT JOIN posts p ON c.qualification = p.id
      WHERE c.slug = ? AND c.university_id = ?`, [courseslug, university_id]
    )

    if(rows.length > 0)
    {
        const course = rows[0];

        const [relatedcourse] = await pool.query<RowDataPacket[]>(
          `SELECT 
            c.id AS id,
            c.name AS name,
            c.slug AS slug,
            c.university_id AS university_id,
            ud.logo as university_logo,
            ud.slug as university_slug,
            ud.name AS university_name,
            ud.address AS location,
            co.image as flag_image,
            co.country as country_name
          FROM courses c
          LEFT JOIN university_details ud ON c.university_id = ud.id AND is_active = 1
          LEFT JOIN countries co ON ud.country = co.country
          WHERE c.university_id = ? AND c.id != ? LIMIT 3`, [course.university_id, course.id])

        if(relatedcourse.length > 0)
        {
            return NextResponse.json(
              {
                success: true,
                data: course,
                relatedcourse: relatedcourse
              },
              {
                  status: 200
              }
            )
        }
        else{
          return NextResponse.json(
              {
                  success: true,
                  data: course
              },
              {
                  status: 200
              }
          )  
        }

         
    }
    else{
        return NextResponse.json(
            {
                success: false,
                message: "There is no course available for this university"
            },
            {
                status: 404
            }
        )
    }

  } catch (err: any) {

    console.log(err)
    return NextResponse.json(
      {
        success: false,
        message: err.message,
      },
      {
        status: 500,
      }
    );
  }
}