const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_logger_index = require('../logger/index.cjs');
const require_utils_responseData = require('../utils/responseData.cjs');
const require_utils_errors_ErrorHandler = require('../utils/errors/ErrorHandler.cjs');
const require_services_tag_service = require('../services/tag.service.cjs');
const require_utils_permissions = require('../utils/permissions.cjs');
const require_utils_filtersAndPagination_getTagFiltersAndPagination = require('../utils/filtersAndPagination/getTagFiltersAndPagination.cjs');
const require_utils_mapper_tag = require('../utils/mapper/tag.cjs');
let express_intlayer = require("express-intlayer");
express_intlayer = require_rolldown_runtime.__toESM(express_intlayer);

//#region src/controllers/tag.controller.ts
/**
* Retrieves a list of tags based on filters and pagination.
*/
const getTags = async (req, res, _next) => {
	const { user, organization, roles } = res.locals;
	const { filters, sortOptions, pageSize, skip, page, getNumberOfPages } = require_utils_filtersAndPagination_getTagFiltersAndPagination.getTagFiltersAndPagination(req, res);
	if (!user) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
		return;
	}
	if (!organization) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_DEFINED");
		return;
	}
	try {
		const tags = await require_services_tag_service.findTags(filters, skip, pageSize, sortOptions);
		if (!require_utils_permissions.hasPermission(roles, "tag:read")({
			...res.locals,
			targetTags: tags
		})) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
			return;
		}
		const totalItems = await require_services_tag_service.countTags(filters);
		const responseData = require_utils_responseData.formatPaginatedResponse({
			data: require_utils_mapper_tag.mapTagsToAPI(tags),
			page,
			pageSize,
			totalPages: getNumberOfPages(totalItems),
			totalItems
		});
		res.json(responseData);
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};
/**
* Adds a new tag to the database.
*/
const addTag = async (req, res, _next) => {
	const { organization, project, user, roles } = res.locals;
	const tagData = req.body;
	if (!user) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
		return;
	}
	if (!organization) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_DEFINED");
		return;
	}
	if (!project) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_NOT_DEFINED");
		return;
	}
	if (!tagData) require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PROJECT_DATA_NOT_FOUND");
	const tag = {
		creatorId: user.id,
		organizationId: organization.id,
		projectId: project.id,
		...tagData
	};
	if (!require_utils_permissions.hasPermission(roles, "tag:admin")({
		...res.locals,
		targetTags: [tag]
	})) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
		return;
	}
	try {
		const formattedTag = require_utils_mapper_tag.mapTagToAPI(await require_services_tag_service.createTag(tag));
		const responseData = require_utils_responseData.formatResponse({
			message: (0, express_intlayer.t)({
				en: "Tag created successfully",
				fr: "Tag créé avec succès",
				es: "Tag creado con éxito"
			}),
			description: (0, express_intlayer.t)({
				en: "Your tag has been created successfully",
				fr: "Votre tag a été créé avec succès",
				es: "Su tag ha sido creado con éxito"
			}),
			data: formattedTag
		});
		res.json(responseData);
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};
/**
* Updates an existing tag in the database.
*/
const updateTag = async (req, res, _next) => {
	const { tagId } = req.params;
	const { organization, user, roles } = res.locals;
	if (!user) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
		return;
	}
	if (!organization) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_DEFINED");
		return;
	}
	try {
		const tag = {
			_id: tagId,
			name: req.body.name,
			key: req.body.key,
			description: req.body.description,
			instructions: req.body.instructions
		};
		const tagToDelete = await require_services_tag_service.getTagById(tagId);
		if (!require_utils_permissions.hasPermission(roles, "tag:write")({
			...res.locals,
			targetTags: [tagToDelete]
		})) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
			return;
		}
		if (String(tagToDelete.organizationId) !== String(organization.id)) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "TAG_NOT_IN_ORGANIZATION");
			return;
		}
		const formattedTag = require_utils_mapper_tag.mapTagToAPI(await require_services_tag_service.updateTagById(tag._id, tag));
		const responseData = require_utils_responseData.formatResponse({
			message: (0, express_intlayer.t)({
				en: "Tag updated successfully",
				fr: "Tag mis à jour avec succès",
				es: "Tag actualizado con éxito"
			}),
			description: (0, express_intlayer.t)({
				en: "Your tag has been updated successfully",
				fr: "Votre tag a été mis à jour avec succès",
				es: "Su tag ha sido actualizado con éxito"
			}),
			data: formattedTag
		});
		res.json(responseData);
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};
/**
* Deletes a tag from the database by its ID.
* @param req - Express request object.
* @param  res - Express response object.
* @returns Response confirming the deletion.
*/
const deleteTag = async (req, res, _next) => {
	const { user, organization, roles } = res.locals;
	const { tagId } = req.params;
	if (!user) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "USER_NOT_DEFINED");
		return;
	}
	if (!organization) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "ORGANIZATION_NOT_DEFINED");
		return;
	}
	if (!tagId) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "TAG_ID_NOT_FOUND");
		return;
	}
	try {
		const tagToDelete = await require_services_tag_service.getTagById(tagId);
		if (!require_utils_permissions.hasPermission(roles, "tag:admin")({
			...res.locals,
			targetTags: [tagToDelete]
		})) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "PERMISSION_DENIED");
			return;
		}
		if (String(tagToDelete.organizationId) !== String(organization.id)) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "TAG_NOT_IN_ORGANIZATION");
			return;
		}
		const deletedTag = await require_services_tag_service.deleteTagById(tagId);
		if (!deletedTag) {
			require_utils_errors_ErrorHandler.ErrorHandler.handleGenericErrorResponse(res, "TAG_NOT_FOUND", { tagId });
			return;
		}
		require_logger_index.logger.info(`Tag deleted: ${String(deletedTag.id)}`);
		const formattedTag = require_utils_mapper_tag.mapTagToAPI(deletedTag);
		const responseData = require_utils_responseData.formatResponse({
			message: (0, express_intlayer.t)({
				en: "Tag deleted successfully",
				fr: "Tag supprimé avec succès",
				es: "Tag eliminado con éxito"
			}),
			description: (0, express_intlayer.t)({
				en: "Your tag has been deleted successfully",
				fr: "Votre tag a été supprimé avec succès",
				es: "Su tag ha sido eliminado con éxito"
			}),
			data: formattedTag
		});
		res.json(responseData);
		return;
	} catch (error) {
		require_utils_errors_ErrorHandler.ErrorHandler.handleAppErrorResponse(res, error);
		return;
	}
};

//#endregion
exports.addTag = addTag;
exports.deleteTag = deleteTag;
exports.getTags = getTags;
exports.updateTag = updateTag;
//# sourceMappingURL=tag.controller.cjs.map