"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteApplication = 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 deleteApplication = async (req, res) => {
    try {
        const { applicationId } = req.params;
        const db = await (0, database_1.getDb)();
        // Get application details before deleting for activity log
        const [application] = await db
            .select()
            .from(tables_1.applicationsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, applicationId));
        if (!application) {
            res.status(404).json({
                success: false,
                message: 'Application not found',
            });
            return;
        }
        await db
            .delete(tables_1.applicationsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.applicationsTable.id, applicationId));
        logger_1.default.info(`Deleted application ${applicationId}`);
        // Log activity
        try {
            const token = req.headers['x-access-token'] || req.headers.authorization;
            const { userId, userName } = (0, activityLogService_1.getUserInfo)(req);
            if (token && application.workspaceId && typeof application.workspaceId === 'string') {
                const orgId = application.workspaceId;
                const projectId = (application.projectId && typeof application.projectId === 'string') ? application.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_APPLICATION,
                    params: (0, activityLogService_1.buildActivityParams)({
                        application_name: { value: application.name || '', _id: applicationId },
                        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 application: ${activityError}`);
        }
        res.json({
            success: true,
            message: 'Application deleted successfully',
        });
    }
    catch (error) {
        logger_1.default.error(`Error deleting application: ${error}`);
        res.status(500).json({
            success: false,
            message: 'Failed to delete application',
        });
    }
};
exports.deleteApplication = deleteApplication;
//# sourceMappingURL=delete-application.js.map