"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteDataset = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const database_1 = require("../../../database");
const tables_1 = require("../../../database/tables");
const logger_1 = __importDefault(require("../../../logger"));
const constants_1 = require("../../../constants");
const activityLogService_1 = require("../../services/activityLogService");
const deleteDataset = async (req, res) => {
    try {
        const { datasetId } = req.params;
        const db = await (0, database_1.getDb)();
        // Get dataset details before deleting for activity log
        const [dataset] = await db
            .select()
            .from(tables_1.trusysDatasetsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.trusysDatasetsTable.id, datasetId));
        if (!dataset) {
            res.status(404).json({
                success: false,
                message: 'Dataset not found',
            });
            return;
        }
        const [result] = await db
            .delete(tables_1.trusysDatasetsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.trusysDatasetsTable.id, datasetId))
            .returning({
            id: tables_1.trusysDatasetsTable.id,
            name: tables_1.trusysDatasetsTable.name,
        });
        logger_1.default.info(`Deleted dataset ${datasetId}`);
        // Log activity
        try {
            const token = req.headers['x-access-token'] || req.headers.authorization;
            const { userId, userName } = (0, activityLogService_1.getUserInfo)(req);
            if (token && dataset.workspaceId && typeof dataset.workspaceId === 'string') {
                const orgId = dataset.workspaceId;
                const projectId = (dataset.projectId && typeof dataset.projectId === 'string') ? dataset.projectId : '';
                const [orgName, projectName] = await Promise.all([
                    (0, activityLogService_1.fetchOrgName)(token, orgId),
                    projectId ? (0, activityLogService_1.fetchProjectName)(token, orgId, projectId) : Promise.resolve(''),
                ]);
                await (0, activityLogService_1.logActivity)(token, {
                    type: constants_1.ActivityType.DELETE_DATASET,
                    params: (0, activityLogService_1.buildActivityParams)({
                        dataset_name: { value: dataset.name, _id: datasetId },
                        user_name: { value: userName, _id: userId },
                        org_name: { value: orgName || orgId, _id: orgId },
                        project_name: { value: projectName || projectId, _id: projectId || undefined },
                    }),
                    project_id: projectId || undefined,
                    org_id: orgId,
                    created_by: userId,
                    updated_by: userId,
                });
            }
        }
        catch (activityError) {
            logger_1.default.error(`Error logging activity for delete dataset: ${activityError}`);
        }
        res.json({
            success: true,
            message: 'Dataset deleted successfully',
            data: result,
        });
    }
    catch (error) {
        logger_1.default.error(`Error deleting dataset: ${error}`);
        res.status(500).json({
            success: false,
            message: 'Failed to delete dataset',
        });
    }
};
exports.deleteDataset = deleteDataset;
//# sourceMappingURL=delete-dataset.js.map