"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PulseAlertsController = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const pulse_1 = require("../../../validators/pulse");
const pulseMonitoringService_1 = require("../../services/pulseMonitoringService");
const get_upgrade_meta_1 = require("../../../util/get-upgrade-meta");
const restriction_message_1 = require("../../constants/restriction.message");
const PulseAlertsController = {
    async createAlert(req, res) {
        try {
            logger_1.default.info('Creating alert');
            const { applicationId, projectId, orgId } = req?.body ?? {};
            const { isStarter, } = await (0, get_upgrade_meta_1.getUpgradeMetaByOrgId)(orgId);
            if (isStarter) {
                res.status(400).json({
                    success: false,
                    message: restriction_message_1.ERROR_RESTRICTION_MESSAGE.upgrade.title,
                    toastMetadata: {
                        text: restriction_message_1.ERROR_RESTRICTION_MESSAGE.upgrade.message,
                        type: 'error',
                    }
                });
                return;
            }
            const validationResult = pulse_1.PulseMonitoringAlertsSchema.validate({
                applicationId,
            });
            if (validationResult?.error) {
                res.status(400).json({
                    success: false,
                    message: validationResult?.error?.message,
                    error: validationResult?.error,
                });
                return;
            }
            const pulseData = await pulseMonitoringService_1.pulseMonitoringService.createAlert({
                applicationId: applicationId,
                projectId: projectId,
                workspaceId: orgId,
            });
            res.json({
                success: true,
                data: pulseData,
            });
        }
        catch (error) {
            logger_1.default.error(`Error in alert controller: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getApplicationsWithMonitoring(req, res) {
        try {
            logger_1.default.info('Getting applications with monitoring');
            const { projectId, limit = 10, page = 1 } = req?.query ?? {};
            const applications = await pulseMonitoringService_1.pulseMonitoringService.getApplicationsWithForAlerts({
                projectId: projectId,
                limit: Number(limit),
                page: Number(page),
            });
            res.json({
                success: true,
                data: applications,
            });
        }
        catch (error) {
            logger_1.default.error(`Error in get applications with monitoring controller: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async updateAlert(req, res) {
        try {
            logger_1.default.info('Updating alert');
            const { applicationId, alerts, emails, webhook } = req?.body ?? {};
            const validationResult = pulse_1.PulseMonitoringAlertsSchema.validate({
                applicationId,
                alerts,
                emails,
                webhook,
            });
            if (validationResult?.error) {
                res.status(400).json({
                    success: false,
                    message: validationResult?.error?.message,
                    error: validationResult?.error,
                });
                return;
            }
            const pulseData = await pulseMonitoringService_1.pulseMonitoringService.updateAlert({
                applicationId: applicationId,
                alerts,
                emails,
                webhook,
            });
            res.json({
                success: true,
                data: pulseData,
            });
        }
        catch (error) {
            logger_1.default.error(`Error in update alert controller: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getAlerts(req, res) {
        try {
            logger_1.default.info('Getting alerts');
            const { projectId, limit = 10, page = 1 } = req?.query ?? {};
            if (!projectId) {
                res.status(400).json({
                    success: false,
                    message: 'projectId is required',
                });
                return;
            }
            const alerts = await pulseMonitoringService_1.pulseMonitoringService.getAlerts({
                projectId: projectId,
                limit: Number(limit),
                page: Number(page),
            });
            res.json({
                success: true,
                data: alerts,
            });
        }
        catch (error) {
            logger_1.default.error(`Error in get alerts controller: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getAlertDetails(req, res) {
        try {
            logger_1.default.info('Getting alert details');
            const { alertId } = req?.params ?? {};
            if (!alertId) {
                res.status(400).json({
                    success: false,
                    message: 'alertId is required',
                });
                return;
            }
            const alertDetails = await pulseMonitoringService_1.pulseMonitoringService.getAlertDetails({
                alertId: alertId,
            });
            res.json({
                success: true,
                data: alertDetails,
            });
        }
        catch (error) {
            logger_1.default.error(`Error in get alert details controller: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getAlertLogs(req, res) {
        try {
            logger_1.default.info('Getting alert logs');
            const { alertId } = req?.params ?? {};
            const { limit = 5, page = 1 } = req?.query ?? {};
            if (!alertId) {
                res.status(400).json({
                    success: false,
                    message: 'alertId is required',
                });
                return;
            }
            const alertLogs = await pulseMonitoringService_1.pulseMonitoringService.getAlertLogs({
                alertId: alertId,
                limit: Number(limit),
                page: Number(page),
            });
            res.json({
                success: true,
                data: alertLogs,
            });
        }
        catch (error) {
            logger_1.default.error(`Error in get alert logs controller: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
};
exports.PulseAlertsController = PulseAlertsController;
//# sourceMappingURL=pulse-alerts.js.map