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

// CREATE CANDIDATE

export const createCandidate = async (req, res) => {
	try {
		const {fullName, mobile, email, subject, coverLetter, careerId} = req.body;

		if (!fullName || !mobile || !email || !careerId) {
			return res.status(400).json({
				success: false,
				message: "Required fields missing",
			});
		}

		const candidate = await prisma.candidate.create({
			data: {
				fullName,
				mobile,
				email,
				subject,
				coverLetter,
				careerId: Number(careerId),
			},
		});

		res.status(201).json({
			success: true,
			message: "Application submitted successfully",
			data: candidate,
		});
	} catch (error) {
		console.error(error);
		res.status(500).json({
			success: false,
			message: "Failed to create candidate",
		});
	}
};

// GET ALL CANDIDATES

export const getCandidates = async (req, res) => {
	try {
		const candidates = await prisma.candidate.findMany({
			include: {
				career: true,
			},
			orderBy: {
				createdAt: "desc",
			},
		});

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

// GET SINGLE CANDIDATE

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

		const candidate = await prisma.candidate.findUnique({
			where: {
				id: Number(id),
			},
			include: {
				career: true,
			},
		});

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

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

// GET CANDIDATES BY CAREER

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

		const candidates = await prisma.candidate.findMany({
			where: {
				careerId: Number(careerId),
			},
			include: {
				career: true,
			},
			orderBy: {
				createdAt: "desc",
			},
		});

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

// UPDATE CANDIDATE

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

		const candidate = await prisma.candidate.update({
			where: {
				id: Number(id),
			},
			data: req.body,
		});

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

// DELETE CANDIDATE

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

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

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