const Students = require("../models/students.m")

exports.getStudent = async (req, res) => {
    try {
        const all_students = await Students.find()

        return res.status(200).json({ message: "All students gathered well", students: all_students })
    } catch (error) {
        return res.status(500).json({ message: "Internal Server Error", error: error.message })
    }
}

exports.createStudent = async (req, res) => {
    try {
        const { full_names, classes, parent_phone } = req.body
        if (!full_names || !classes || !parent_phone) return res.status(400).json({ message: "All fields are requried bro" })

        const newStudent = new Students({
            full_names,
            classes,
            parent_phone
        })

        await newStudent.save()

        return res.status(201).json({ message: "Student added to database Successfully", data: newStudent })

    } catch (error) {
        return res.status(500).json({ message: "Internal Server Error", error: error.message })
    }
}

exports.updateStudent = async (req, res) => {
    try {
        const { id } = req.params
        const { full_names, classes, parent_phone } = req.body
        const studentExists = await Students.findById(id)
        if (!studentExists) return res.status(404).json({ message: "Student not Found" })

        const updates = await Students.findByIdAndUpdate(id, req.body, { new: true })
        return res.status(200).json({ message: "Student Updated successfully", data: updates })
    } catch (error) {
        return res.status(500).json({ message: "Internal Server Error", error: error.message })
    }
}

exports.deleteStudent = async (req, res) => {
    try {
        const { id } = req.params
        const studentExists = await Students.findById(id)
        if(!studentExists) return res.status(404).json({message: "Student Not Found"})

        const deletes = await Students.findByIdAndDelete(id)
        return res.status(200).json({message: "Student Deleted Succefully"})
    } catch (error) {
        return res.status(500).json({ message: "Internal Server Error", error: error.message })
    }
}