"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bulkDeleteDatasetRows = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const datasetRowsService_1 = require("../../services/datasetRows/datasetRowsService");
const constants_1 = require("../../../constants");
const activityLogService_1 = require("../../services/activityLogService");
const database_1 = require("../../../database");
const tables_1 = require("../../../database/tables");
const drizzle_orm_1 = require("drizzle-orm");
const bulkDeleteDatasetRows = async (req, res) => {
    try {
        const { id: datasetId } = req.params;
        const token = (req.headers['x-access-token'] || req.headers.authorization);
        const { projectId, orgId, recordIds } = (req.body ?? {});
        if (!datasetId || !projectId || !orgId || !Array.isArray(recordIds)) {
            res.status(400).json({ error: 'datasetId, projectId, orgId and recordIds[] are required' });
            return;
        }
        const result = await datasetRowsService_1.datasetRowsService.bulkDelete({ datasetId, recordIds });
        try {
            const db = await (0, database_1.getDb)();
            const [dataset] = await db
                .select({ name: tables_1.trusysDatasetsTable.name })
                .from(tables_1.trusysDatasetsTable)
                .where((0, drizzle_orm_1.eq)(tables_1.trusysDatasetsTable.id, datasetId))
                .limit(1);
            const datasetName = dataset?.name || datasetId;
            const { userId, userName } = (0, activityLogService_1.getUserInfo)(req);
            const [orgName, projectName] = await Promise.all([
                (0, activityLogService_1.fetchOrgName)(token, orgId),
                (0, activityLogService_1.fetchProjectName)(token, orgId, projectId),
            ]);
            await (0, activityLogService_1.logActivity)(token, {
                type: constants_1.ActivityType.DELETE_DATASET_ROW,
                params: (0, activityLogService_1.buildActivityParams)({
                    dataset_name: { value: datasetName, _id: datasetId },
                    user_name: { value: userName, _id: userId },
                    org_name: { value: orgName || orgId, _id: orgId },
                    project_name: { value: projectName || projectId, _id: projectId },
                    message: { value: `Row(s) of dataset-${datasetName} deleted by ${userName}` },
                }),
                project_id: projectId,
                org_id: orgId,
                created_by: userId,
                updated_by: userId,
            });
        }
        catch (activityError) {
            logger_1.default.error(`Error logging activity for bulk delete dataset rows: ${activityError}`);
        }
        res.json({ success: true, ...result });
    }
    catch (error) {
        const status = typeof error?.status === 'number' ? error.status : 500;
        logger_1.default.error(`Error bulk deleting dataset rows: ${error}`);
        res.status(status).json({ error: error?.message || 'Failed to bulk delete dataset rows' });
    }
};
exports.bulkDeleteDatasetRows = bulkDeleteDatasetRows;
//# sourceMappingURL=bulk-delete-dataset-rows.js.map