const Payment = require("../models/payment.m")

exports.getPayment = async (req, res) => {
    try {
        const all_payment = await Payment.find()

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

exports.createPayment = async (req, res) => {
    try {
        const { student_id, amount } = req.body
        if (!student_id || !amount) return res.status(401).json({ message: "All fields are requried bro" })

        const newPayment = new Payment({
            student_id,
            amount
        })

        await newPayment.save()

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

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

exports.updatePayment = async (req, res) => {
    try {
        const { id } = req.params
        const { student_id, amount } = req.body
        const paymenyExists = await Payment.findById(id)
        if (!paymenyExists) return res.status(404).json({ message: "Payment not Found" })

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

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

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