import { verifyToken } from "../auth/auth.js"
import { connection } from "../configs/db.js"
import express from "express"

const courseRoute = express.Router()

// Get all courses route

courseRoute.get("/all", verifyToken, async (req, res) => {
    try {
        const [courses] = await connection.query("SELECT * FROM courses")
        return res.status(200).json(courses)
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})

// Get course by id route

courseRoute.get("/:id", verifyToken, async (req, res) => {
    try {
        const { id } = req.params
        if(!id) return res.status(400).json({ message: "Please provide a valid id" })
        const [course] = await connection.query("SELECT * FROM courses WHERE id = ?", [id])
        if(!course) return res.status(404).json({ message: "Course not found" })
        return res.status(200).json(course)
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})

// Create course route

courseRoute.post("/create", verifyToken, async (req, res) => {
    try {
        if(req.user.role != "admin") return res.status(401).json({ message: "You are not authorized to perform this action" })
        const { course_name, duration } = req.body
        if(!course_name || !duration) return res.status(400).json({ message: "Please provide all the required fields" })
        const sql = `INSERT INTO courses(course_name, duration) VALUES(?, ?)`
        const [result] = await connection.query(sql, [course_name, duration])
        if(result.affectedRows == 1) return res.status(201).json({ message: "Course created successfully" })
        return res.status(500).json({ message: "Internal server error" })
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})

// Update course route

courseRoute.put("/update/:id", verifyToken, async (req, res) => {
    try {
        if(req.user.role != "admin") return res.status(401).json({ message: "You are not authorized to perform this action" })
        const { id } = req.params
        const { course_name, duration } = req.body
        if(!id || !course_name || !duration) return res.status(400).json({ message: "Please provide all the required fields" })
        const [course] = await connection.query("SELECT * FROM courses WHERE id = ?", [id])
        if(!course) return res.status(404).json({ message: "Course not found" })
        const sql = `UPDATE courses SET course_name = ?, duration = ? WHERE id = ?`
        const [result] = await connection.query(sql, [course_name, duration, id])
        if(result.affectedRows == 1) return res.status(200).json({ message: "Course updated successfully" })
        return res.status(500).json({ message: "Internal server error" })
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})


// delete course route

courseRoute.delete("/delete/:id", verifyToken, async (req, res) => {
    try {
        if(req.user.role != "admin") return res.status(401).json({ message: "You are not authorized to perform this action" })
        const { id } = req.params
        if(!id) return res.status(400).json({ message: "Please provide a valid id" })
        const [course] = await connection.query("SELECT * FROM courses WHERE id = ?", [id])
        if(!course) return res.status(404).json({ message: "Course not found" })
        const sql = `DELETE FROM courses WHERE id = ?`
        const [result] = await connection.query(sql, [id])
        if(result.affectedRows == 1) return res.status(200).json({ message: "Course deleted successfully" })
        return res.status(500).json({ message: "Internal server error" })
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})

export default courseRoute