import { authMiddleware } from '../../../lib/authMiddleware';
import {
	Users,
	Templates,
	Campaigns,
	AutoIncreament,
	CampaignSection,
	TemplateSection
} from '../../../models/DB';
import Common from '../../../helpers/Common';
import { uuid } from 'uuidv4';
import md5 from 'md5';
import path from 'path';
import CommonAPI from '../../../helpers/CommonAPI';
import { parseForm } from '../../../lib/parseForm';
import axios from 'axios';

const routeHandler = {}

routeHandler.updateProfile = async (req, res) => {
	let user = req.vsuser;
	try {
		const { fields, files } = await parseForm(req)

		const profileName = fields?.name.toString()
		const profilePassword = fields?.password.toString()
		let signedurl
		if (profileName && user) {
			let where = {
				_id: user._id
			};

			let set = {
				name: profileName
			};

			let file = '';
			if (files && files?.profileImage) {
				file = files.profileImage[0];
			}
			if (file) {
				let extension = path.extname(file.originalFilename);
				if (!extension) {
					extension = '.png';
				}
				let fileName = uuid().concat(extension);
				const tempFilePath = file.filepath;
				let subPath = `${user._id}`;
				subPath = subPath.concat(`/profileImage`);
				let remoteFile = `usercontent/${subPath}/${fileName}`;

				// let r;
				// r = await CommonAPI.upload(tempFilePath, remoteFile, {
				// 	ContentType: file.mimetype,
				// }).catch((err) => {
				// 	console.log(err);
				// 	res.json({
				// 		status: 'erroe',
				// 		file: null,
				// 		messgae: err,
				// 	});
				// 	return;
				// });
				signedurl = await CommonAPI.getPutObjectSignedURL(remoteFile)
				if (signedurl != undefined) {
					if (user.profilePicture && user.profilePicture != "") {
						CommonAPI.deleteObjects([user.profilePicture.key]);
					}
					set.profilePicture = { key: remoteFile, file: remoteFile };
				}
			}

			if (profilePassword != "") {
				set.password = md5(profilePassword);
			}

			await Users.updateOne(where, {
				$set: set
			}).then(async (result) => {
				if (set.password) {
					delete set.password;
				}
				res.json({
					status: 'success',
					message: 'We have updated profile successfully.',
					data: set,
					signedurl: signedurl
				});
			});
		} else {
			res.json({
				status: 'error',
				message: 'Something went wrong.'
			});
		}
	} catch (err) {
		console.log(err);
		res.json({
			status: 'error',
			message: 'Server error'
		});
	}
}

routeHandler.uploadTemplateProfileImage = async (req, res) => {
	let user = req.vsuser;
	try {
		const { fields, files } = await parseForm(req)

		const templateId = fields?.template_id.toString()

		if (
			templateId != "" && user
		) {
			let templateTbl = Templates;
			if (user.role != 1) {
				templateTbl = Campaigns;
			}

			let file = '';
			if (files && files?.file) {
				file = files.file[0];
			}
			if (file && file.size) {
				let extension = path.extname(file.originalFilename);
				if (!extension) {
					extension = '.png';
				}
				let fileName = uuid().concat(extension);
				const tempFilePath = file.filepath;
				let subPath = `${user._id}`;
				subPath = subPath.concat(`/templates`);
				let remoteFile = `usercontent/${subPath}/${templateId}/${fileName}`;
				let r;
				// r = await CommonAPI.upload(tempFilePath, remoteFile, {
				// 	ContentType: file.mimetype,
				// }).catch((err) => {
				// 	res.json({
				// 		status: "error",
				// 		file: null,
				// 		messgae: err,
				// 	});
				// 	return;
				// });
				let signedurl = await CommonAPI.getPutObjectSignedURL(remoteFile)

				if (signedurl != undefined) {
					let where = { _id: templateId };
					let template = await templateTbl.findOne(where);
					if (template && typeof template._id !== "undefined") {
						let profile = {};
						if (
							template.profile.image &&
							template.profile.image.indexOf("profileImage") ===
							-1
						) {
							await CommonAPI.deleteObjects([template.profile.image]);
						}
						profile = {
							...template.profile,
							image: remoteFile,
						};
						//console.log(profile);
						let set = {
							profile: profile,
						};
						await templateTbl.updateOne(where, {
							$set: set,
						}).then(() => {
							res.json({
								status: "success",
								message:
									"The template Profile Image has been uploaded successfully.",
								profile,
								signedurl: signedurl
							});
						});
					}
				}
			}
		} else {
			res.json({
				status: "error",
				message: "Something went wrong.",
			});
		}
	} catch (err) {
		res.json({
			status: "error",
			message: "Server error",
		});
	}
}
async function getNextSequenceValue(sequenceName) {

	var sequenceDocument = await AutoIncreament.findOneAndUpdate(
		{ _id: sequenceName },
		{ $inc: { sequence_value: 1 } },
		{ upsert: true, new: true }
	);
	let sequence
	if (sequenceDocument != null) {
		sequence = sequenceDocument.sequence_value
	}
	else {
		let create = await AutoIncreament.create({ '_id': 'imageId', 'sequence_value': 1 })
		console.log('created sequence', create);
		sequence = create.sequence_value
	}
	// console.log(sequenceDocument);
	return sequence;
}


routeHandler.addUpdateImageSection = async (req, res) => {
	const user = req.vsuser;

	try {
		const { fields, files } = await parseForm(req);

		const section_id = fields?.section_id?.toString();
		const SectionData = fields?.SectionData?.toString();
		const imageurl = fields?.url?.toString();
		const imageID = fields?.imgId ? JSON.parse(fields.imgId) : '';

		if (!section_id || !user) {
			return res.json({
				status: "error",
				message: "Missing section ID or user.",
			});
		}

		let sectionTbl = user.role === 1 ? TemplateSection : CampaignSection;
		let file = files?.file?.[0];

		// If uploading a new image
		if (file && file.size) {
			const extension = path.extname(file.originalFilename) || '.png';
			const fileName = uuid().concat(extension);
			const remoteFile = `usercontent/${user._id}/templates/${section_id}/${fileName}`;
			const signedurl = await CommonAPI.getPutObjectSignedURL(remoteFile);

			if (!signedurl) {
				return res.json({
					status: "error",
					message: "Failed to generate signed URL.",
				});
			}

			const section = await sectionTbl.findOne({ _id: section_id });

			if (!section) {
				return res.json({
					status: "error",
					message: "Section not found.",
				});
			}

			// If replacing an existing image
			if (imageID && section.otherBusiness.length > 0) {
				for (const item of section.otherBusiness) {
					if (imageID === item.imageID) {
						await CommonAPI.deleteObjects([item.image]);
					}
				}

				const updatedBusiness = {
					image: remoteFile,
					url: imageurl,
					imageID: imageID,
				};

				const updateResult = await sectionTbl.updateOne(
					{ _id: section_id, "otherBusiness.imageID": imageID },
					{ $set: { "otherBusiness.$": updatedBusiness } }
				);
				if (updateResult.modifiedCount > 0) {
					return res.json({
						status: "success",
						message: "The image has been replaced successfully.",
						otherBusiness: updatedBusiness,
						signedurl,
					});
				} else {
					return res.json({
						status: "error",
						message: "ImageID not found in section.",
					});
				}
			}

			// If adding a new image
			const newImageID = await getNextSequenceValue('imageId');
			const otherBusiness = {
				image: remoteFile,
				url: imageurl,
				imageID: newImageID,
			};

			await sectionTbl.updateOne(
				{ _id: section_id },
				{
					$push: { otherBusiness },
					...(SectionData ? { $set: { SectionData } } : {}),
				}
			);

			return res.json({
				status: "success",
				message: "The template profile image has been uploaded successfully.",
				otherBusiness,
				signedurl,
			});
		}

		// If only updating the URL of an existing image
		if (imageID !== '' && imageurl !== '') {
			const result = await sectionTbl.updateOne(
				{ _id: section_id, "otherBusiness.imageID": imageID },
				{ $set: { "otherBusiness.$.url": imageurl } }
			);

			if (result.modifiedCount > 0) {
				return res.json({
					status: "success",
					message: "The URL has been updated successfully.",
				});
			} else {
				return res.json({
					status: "error",
					message: "Image not found or URL already up to date.",
				});
			}
		}

		return res.json({
			status: "error",
			message: "Invalid request. No file or valid image ID provided.",
		});
	} catch (err) {
		console.error("Server error:", err);
		return res.json({
			status: "error",
			message: "Server error",
		});
	}
};




routeHandler.updateTemplateThumb = async (req, res) => {
	let user = req.vsuser;
	try {
		const { fields, files } = await parseForm(req)

		const templateId = fields?.template_id.toString()

		if (
			templateId != "" && user
		) {
			let templateTbl = Templates;

			let where = { _id: templateId };
			let templateData = await templateTbl.findOne(where);
			if (templateData && typeof templateData._id != 'undefined') {
				let file = '';
				if (files && files?.thumb) {
					file = files.thumb[0];
				}
				if (file && file.size) {
					let extension = path.extname(file.originalFilename);
					if (!extension) {
						extension = '.png';
					}
					let fileName = uuid().concat(extension);
					const tempFilePath = file.filepath;
					let subPath = `${user._id}`;
					subPath = subPath.concat(`/templates`);
					let remoteFile = `usercontent/${subPath}/${templateId}/${fileName}`;
					let r;
					// r = await CommonAPI.upload(tempFilePath, remoteFile, {
					// 	ContentType: file.mimetype,
					// }).catch((err) => {
					// 	res.json({
					// 		status: "error",
					// 		file: null,
					// 		messgae: err,
					// 	});
					// 	return;
					// });
					let signedurl = await CommonAPI.getPutObjectSignedURL(remoteFile)
					if (signedurl != undefined) {
						let thumb = '';
						if (templateData.thumb?.key) {
							await CommonAPI.deleteObjects([templateData.thumb.key]);
						}
						thumb = {
							key: remoteFile,
							url: remoteFile
						};
						let set = {
							thumb: thumb,
						};
						await templateTbl.updateOne(where, {
							$set: set,
						}).then(() => {
							res.json({
								status: "success",
								message: "The template Thumb Image has been uploaded successfully.",
								data: { thumb: thumb },
								signedurl: signedurl
							});
						});
					} else {
						res.json({
							status: "error",
							message: "Something went wrong while uploading thumb.",
						});
					}
				} else {
					res.json({
						status: "error",
						message: "Something went wrong.",
					});
				}
			} else {
				res.json({
					status: "error",
					message: "Template that you requested is not exist.",
				});
			}
		} else {
			res.json({
				status: "error",
				message: "Something went wrong.",
			});
		}
	} catch (err) {
		console.log(err);
		res.json({
			status: "error",
			message: "Server error",
		});
	}
}

async function handler(req, res) {
	const { uploadSlug } = req.query
	let routeFlag = true

	if (req.method === 'POST') {
		switch (uploadSlug) {
			case 'updateProfile':
				await routeHandler.updateProfile(req, res)
				break;
			case 'uploadTemplateProfileImage':
				await routeHandler.uploadTemplateProfileImage(req, res)
				break;
			case 'updateTemplateThumb':
				await routeHandler.updateTemplateThumb(req, res)
				break;
			case 'addUpdateImageSection':
				await routeHandler.addUpdateImageSection(req, res)
				break;
			default:
				routeFlag = false
		}
	} else {
		routeFlag = false
	}

	if (!routeFlag) {
		res.status(404).send('No route found.')
	}
}

export default authMiddleware(handler)

export const config = {
	api: {
		bodyParser: false,
	},
}
