var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Blue500 } from "../../Types/BrandColors";
import OneUptimeDate from "../../Types/Date";
import BadDataException from "../../Types/Exception/BadDataException";
import { IsBillingEnabled } from "../EnvironmentConfig";
import logger from "../Utils/Logger";
import DatabaseService from "./DatabaseService";
import Model from "../../Models/DatabaseModels/ScheduledMaintenanceFeed";
import WorkspaceNotificationRuleService from "./WorkspaceNotificationRuleService";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
        if (IsBillingEnabled) {
            this.hardDeleteItemsOlderThanInDays("createdAt", 3 * 365); // 3 years
        }
    }
    async createScheduledMaintenanceFeedItem(data) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        try {
            if (!data.scheduledMaintenanceId) {
                throw new BadDataException("Scheduled Maintenance ID is required");
            }
            if (!data.feedInfoInMarkdown) {
                throw new BadDataException("Log in markdown is required");
            }
            if (!data.scheduledMaintenanceFeedEventType) {
                throw new BadDataException("Scheduled Maintenance log event is required");
            }
            if (!data.projectId) {
                throw new BadDataException("Project ID is required");
            }
            if (!data.displayColor) {
                data.displayColor = Blue500;
            }
            const scheduledMaintenanceFeed = new Model();
            scheduledMaintenanceFeed.displayColor = data.displayColor;
            scheduledMaintenanceFeed.scheduledMaintenanceId =
                data.scheduledMaintenanceId;
            scheduledMaintenanceFeed.feedInfoInMarkdown = data.feedInfoInMarkdown;
            scheduledMaintenanceFeed.scheduledMaintenanceFeedEventType =
                data.scheduledMaintenanceFeedEventType;
            scheduledMaintenanceFeed.projectId = data.projectId;
            if (data.userId) {
                scheduledMaintenanceFeed.userId = data.userId;
            }
            if (data.moreInformationInMarkdown) {
                scheduledMaintenanceFeed.moreInformationInMarkdown =
                    data.moreInformationInMarkdown;
            }
            if (!data.postedAt) {
                scheduledMaintenanceFeed.postedAt = OneUptimeDate.getCurrentDate();
            }
            await this.create({
                data: scheduledMaintenanceFeed,
                props: {
                    isRoot: true,
                },
            });
            try {
                // send notification to slack and teams
                if ((_a = data.workspaceNotification) === null || _a === void 0 ? void 0 : _a.sendWorkspaceNotification) {
                    await this.sendWorkspaceNotification({
                        projectId: data.projectId,
                        scheduledMaintenanceId: data.scheduledMaintenanceId,
                        feedInfoInMarkdown: data.feedInfoInMarkdown,
                        workspaceNotification: data.workspaceNotification,
                    });
                }
            }
            catch (e) {
                logger.error("Error in sending notification to slack and teams", {
                    projectId: (_b = data.projectId) === null || _b === void 0 ? void 0 : _b.toString(),
                    scheduledMaintenanceId: (_c = data.scheduledMaintenanceId) === null || _c === void 0 ? void 0 : _c.toString(),
                });
                logger.error(e, {
                    projectId: (_d = data.projectId) === null || _d === void 0 ? void 0 : _d.toString(),
                    scheduledMaintenanceId: (_e = data.scheduledMaintenanceId) === null || _e === void 0 ? void 0 : _e.toString(),
                });
                // we dont throw this error as it is not a critical error
            }
        }
        catch (error) {
            logger.error("ScheduledMaintenanceFeedService.createScheduledMaintenanceFeedItem", {
                projectId: (_f = data.projectId) === null || _f === void 0 ? void 0 : _f.toString(),
                scheduledMaintenanceId: (_g = data.scheduledMaintenanceId) === null || _g === void 0 ? void 0 : _g.toString(),
            });
            logger.error(error, {
                projectId: (_h = data.projectId) === null || _h === void 0 ? void 0 : _h.toString(),
                scheduledMaintenanceId: (_j = data.scheduledMaintenanceId) === null || _j === void 0 ? void 0 : _j.toString(),
            });
            // we dont want to throw the error here, as this is not critical but we still log it.
        }
    }
    async sendWorkspaceNotification(data) {
        return await WorkspaceNotificationRuleService.sendWorkspaceMarkdownNotification({
            projectId: data.projectId,
            notificationFor: {
                scheduledMaintenanceId: data.scheduledMaintenanceId,
            },
            feedInfoInMarkdown: data.feedInfoInMarkdown,
            workspaceNotification: data.workspaceNotification,
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "createScheduledMaintenanceFeedItem", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "sendWorkspaceNotification", null);
export default new Service();
//# sourceMappingURL=ScheduledMaintenanceFeedService.js.map