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

const studentRoute = express.Router()

// Get all students route

studentRoute.get("/all", verifyToken, async (req, res) => {
    try {

        const [students] = await connection.query("SELECT * FROM students")
        return res.status(200).json(students)
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})

// Get student by id route

studentRoute.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 [student] = await connection.query("SELECT * FROM students WHERE id = ?", [id])
        if(!student) return res.status(404).json({ message: "Student not found" })
        return res.status(200).json(student)
    
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "Internal server error" })
    }
})


// Create student route

studentRoute.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 { first_name, last_name, gender, email, phone, course_id } = req.body
        if(!first_name || !last_name || !gender || !email || !phone || !course_id) return res.status(400).json({ message: "Please provide all the required fields" })
        const [course] = await connection.query("SELECT * FROM courses WHERE id = ?", [course_id])
        if(!course) return res.status(404).json({ message: "Course not found" })
        const sql = `INSERT INTO students(first_name, last_name, gender, email, phone, course_id) VALUES(?, ?, ?, ?, ?, ?)`
        const [result] = await connection.query(sql, [first_name, last_name, gender, email, phone, course_id])
        if(result.affectedRows == 1) return res.status(201).json({ message: "Student 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 student route

studentRoute.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 { first_name, last_name, gender, email, phone, course_id } = req.body
        if(!id || !first_name || !last_name || !gender || !email || !phone || !course_id) return res.status(400).json({ message: "Please provide all the required fields" })
        const [course] = await connection.query("SELECT * FROM courses WHERE id = ?", [course_id])
        if(!course) return res.status(404).json({ message: "Course not found" })
        
        const sql = `UPDATE students SET first_name = ?, last_name = ?, gender = ?, email = ?, phone = ?, course_id = ? WHERE id = ?`
        const [result] = await connection.query(sql, [first_name, last_name, gender, email, phone, course_id, id])
        if(result.affectedRows == 1) return res.status(200).json({ message: "Student 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 student route

studentRoute.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 [student] = await connection.query("SELECT * FROM students WHERE id = ?", [id])
        if(!student) return res.status(404).json({ message: "Student not found" })
        const sql = `DELETE FROM students WHERE id = ?`
        const [result] = await connection.query(sql, [id])
        if(result.affectedRows == 1) return res.status(200).json({ message: "Student 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 studentRoute