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

export const getAllDepartments = async (req, res) => {
	try {
		const departments = await prisma.department.findMany({
			orderBy: {name: "asc"},
		});

		const response = departments.map((dep) => ({
			departmentId: dep.departmentId,
			name: dep.name,
			para1: dep.para1 ?? "",
			para2: dep.para2 ?? "",
			para3: dep.para3 ?? "",
			facilities: dep.facilities ?? "",
			services: dep.services ?? "",
		}));

		return res.status(200).json({
			success: true,
			data: response,
		});
	} catch (error) {
		console.error(error);
		return res.status(500).json({
			success: false,
			message: "Failed to fetch departments",
		});
	}
};

export const getDepartmentByName = async (req, res) => {
	try {
		const {name} = req.query;

		if (!name) {
			return res.status(400).json({
				success: false,
				message: "Department name is required",
			});
		}

		const department = await prisma.department.findFirst({
			where: {
				name: name,
			},
		});

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

		const response = {
			departmentId: department.departmentId,
			name: department.name,
			para1: department.para1 ?? "",
			para2: department.para2 ?? "",
			para3: department.para3 ?? "",
			facilities: department.facilities ?? "",
			services: department.services ?? "",
		};

		return res.status(200).json({
			success: true,
			data: [response],
		});
	} catch (error) {
		console.error(error);
		return res.status(500).json({
			success: false,
			message: "Failed to fetch department",
		});
	}
};

export async function createDepartment(req, res) {
	try {
		const {departmentId, name, para1, para2, para3, facilities, services} =
			req.body;

		if (!departmentId || !name) {
			return res
				.status(400)
				.json({error: "departmentId and name are required"});
		}

		const department = await prisma.department.create({
			data: {
				departmentId,
				name,
				para1,
				para2,
				para3,
				facilities,
				services,
			},
		});

		res.status(201).json({
			message: "Department created successfully",
			data: department,
		});
	} catch (error) {
		if (error.code === "P2002") {
			return res.status(409).json({error: "Department already exists"});
		}

		res.status(500).json({error: "Failed to create department"});
	}
}

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

		const {name, para1, para2, para3, facilities, services} = req.body;

		const department = await prisma.department.update({
			where: {departmentId},
			data: {
				name,
				para1,
				para2,
				para3,
				facilities,
				services,
			},
		});

		return res.status(200).json({
			success: true,
			message: "Department updated successfully",
			data: department,
		});
	} catch (error) {
		console.error(error);
		return res.status(500).json({
			success: false,
			message: "Failed to update department",
		});
	}
};

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

		await prisma.department.delete({
			where: {departmentId},
		});

		return res.status(200).json({
			success: true,
			message: "Department deleted successfully",
		});
	} catch (error) {
		console.error(error);
		return res.status(500).json({
			success: false,
			message: "Failed to delete department",
		});
	}
};
