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

/* CREATE INQUIRY */
export const createInquiry = async (req, res) => {
	try {
		const {fullName, number, emailId, subject, message} = req.body;

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

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

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

/* GET ALL */
export const getInquiries = async (req, res) => {
	try {
		const inquiries = await prisma.inquiry.findMany({
			orderBy: {
				createdAt: "desc",
			},
		});

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

/* GET SINGLE */
export const getInquiry = async (req, res) => {
	try {
		const {id} = req.params;

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

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

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

/* DELETE */
export const deleteInquiry = async (req, res) => {
	try {
		const {id} = req.params;

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

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