import Student from "../models/student.model.js";
import express from 'express';
import { verifyToken } from "../auth/auth.js";
import mongoose from "mongoose";
import Payment from "../models/payment.model.js";
const studentRouter = express.Router()

// add
studentRouter.post("/add", verifyToken, async(req, res) => {
    try{
        const { name, email, parentName, course } = req.body;
        if(!name || !email || !parentName || !course) {
            return res.status(400).json({ message: "All fields are required" })
        }

        if(!["L4MMP", "L5SOD", "L4SOD", "L3NIT", "L4NIT", "L5NIT"].includes(course)) {
            return res.status(400).json({ message: "Invalid course" })
        }
        const doesExists = await Student.findOne({ email })
        if(doesExists){
            return res.status(400).json({ message: "student arleady exists"})
        }
        const student = new Student({ name, email, parentName, course })
        await student.save()
        res.status(201).json({ message: "Student added successfully", student })   

    }catch(error){
        console.log(error)
        return res.status(500).json({ message: "Server error" })
    }
})

// edit

studentRouter.put("/edit/:id", verifyToken, async(req, res) => {
    try{
        const { id } = req.params;
        if(!mongoose.Types.ObjectId.isValid(id)) {
            return res.status(400).json({ message: "Invalid student ID" })
        }
        const { name, email, parentName, course } = req.body;
        
        if(!["L4MMP", "L5SOD", "L4SOD", "L3NIT", "L4NIT", "L5NIT"].includes(course)) {
            return res.status(400).json({ message: "Invalid course" })
        }

        const fieldsToUpdate = {}
        if(name) fieldsToUpdate.name = name;
        if(email) fieldsToUpdate.email = email;
        if(parentName) fieldsToUpdate.parentName = parentName;
        if(course) fieldsToUpdate.course = course;
        const updatedStudent = await Student.findByIdAndUpdate(id, fieldsToUpdate, { returnDocument: 'after' })
        return res.status(200).json({ message: "Student updated successfully", updatedStudent })
    }catch(error){
        console.log(error)
        return res.status(500).json({ message: "Server error" })
    }
})

// delete
studentRouter.delete("/delete/:id", verifyToken, async(req, res) => {
    try{
        const { id } = req.params;
        if(!mongoose.Types.ObjectId.isValid(id)) {
            return res.status(400).json({ message: "Invalid student ID" })
        }
        const student = await Student.findByIdAndDelete(id)
        if(!student) {
            return res.status(404).json({ message: "Student not found" })
        }
        const cascadeDelete = await Payment.deleteMany({ student: id })
        return res.status(200).json({ message: "Student deleted successfully with all associated payments deleted", cascadeDelete })
    }catch(error){
        console.log(error)
        return res.status(500).json({ message: "Server error" })
    }
})

// get all 
studentRouter.get("/all", verifyToken, async(req, res) => {
    try{
        const students = await Student.find().sort({ createdAt: -1 })
        return res.status(200).json({ students })
    }catch(error){
        console.log(error)
        return res.status(500).json({ message: "Server error" })
    }
})

//get by id

studentRouter.get("/:id", verifyToken, async(req, res) => {
    try{
        const { id } = req.params;
        if(!mongoose.Types.ObjectId.isValid(id)) {
            return res.status(400).json({ message: "Invalid student ID" })
        }
        const student = await Student.findById(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: "Server error" })
    }
})
export default studentRouter;