import prisma from "../prisma/client.js";

// CREATE ACADEMICS & RESEARCH

export const createAcademicsResearch = async (req, res) => {
	try {
		const {fullName, number, emailId, subject, courseName, message} = req.body;

		if (!fullName || !number) {
			return res.status(400).json({
				success: false,
				message: "Full name and number are required",
			});
		}

		const data = await prisma.academicsResearch.create({
			data: {
				fullName,
				number,
				emailId,
				subject,
				courseName,
				message,
			},
		});

		res.status(200).json({
			success: true,
			status: 200,
			data,
			message: "Academics & Research added successfully",
		});
	} catch (error) {
		console.error(error);
		res.status(500).json({
			success: false,
			message: "Failed to add Academics & Research inquiry",
		});
	}
};

// GET ALL

export const getAcademicsResearch = async (req, res) => {
	try {
		const data = await prisma.academicsResearch.findMany({
			orderBy: {
				createdAt: "desc",
			},
		});

		res.json({
			success: true,
			data,
		});
	} catch (error) {
		res.status(500).json({
			success: false,
			message: "Failed to fetch records",
		});
	}
};

// GET SINGLE

export const getSingleAcademicsResearch = async (req, res) => {
	try {
		const {id} = req.params;

		const data = await prisma.academicsResearch.findUnique({
			where: {
				id: Number(id),
			},
		});

		if (!data) {
			return res.status(404).json({
				success: false,
				message: "Record not found",
			});
		}

		res.json({
			success: true,
			data,
		});
	} catch (error) {
		res.status(500).json({
			success: false,
			message: "Failed to fetch record",
		});
	}
};

// DELETE

export const deleteAcademicsResearch = async (req, res) => {
	try {
		const {id} = req.params;

		await prisma.academicsResearch.delete({
			where: {
				id: Number(id),
			},
		});

		res.json({
			success: true,
			message: "Record deleted successfully",
		});
	} catch (error) {
		res.status(500).json({
			success: false,
			message: "Failed to delete record",
		});
	}
};
