"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteDatasetRow = 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 deleteDatasetRow = async (req, res) => {
    try {
        const { id: datasetId, recordId } = req.params;
        const token = (req.headers['x-access-token'] || req.headers.authorization);
        const projectId = (req.query.projectId || req.body?.projectId);
        const orgId = (req.query.orgId || req.body?.orgId);
        if (!datasetId || !recordId || !projectId || !orgId) {
            res.status(400).json({ error: 'datasetId, recordId, projectId, orgId are required' });
            return;
        }
        await datasetRowsService_1.datasetRowsService.deleteRow({ datasetId, recordId });
        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 delete dataset row: ${activityError}`);
        }
        res.json({ success: true });
    }
    catch (error) {
        const status = typeof error?.status === 'number' ? error.status : 500;
        logger_1.default.error(`Error deleting dataset row: ${error}`);
        res.status(status).json({ error: error?.message || 'Failed to delete dataset row' });
    }
};
exports.deleteDatasetRow = deleteDatasetRow;
//# sourceMappingURL=delete-dataset-row.js.map