import UserMiddleware from "../Middleware/UserAuthorization";
import GlobalConfigService from "../Services/GlobalConfigService";
import UserTelegramService from "../Services/UserTelegramService";
import Response from "../Utils/Response";
import BaseAPI from "./BaseAPI";
import BadDataException from "../../Types/Exception/BadDataException";
import ObjectID from "../../Types/ObjectID";
import UserTelegram from "../../Models/DatabaseModels/UserTelegram";
export default class UserTelegramAPI extends BaseAPI {
    constructor() {
        var _a, _b;
        super(UserTelegram, UserTelegramService);
        this.router.post(`${(_a = new this.entityType().getCrudApiPath()) === null || _a === void 0 ? void 0 : _a.toString()}/verification-info`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            var _a, _b, _c, _d;
            try {
                req = req;
                if (!req.body.itemId) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Invalid item ID"));
                }
                const item = await this.service.findOneById({
                    id: req.body["itemId"],
                    props: {
                        isRoot: true,
                    },
                    select: {
                        userId: true,
                        projectId: true,
                        verificationCode: true,
                        isVerified: true,
                    },
                });
                if (!item) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Item not found"));
                }
                if (((_a = item.userId) === null || _a === void 0 ? void 0 : _a.toString()) !==
                    ((_c = (_b = req === null || req === void 0 ? void 0 : req.userAuthorization) === null || _b === void 0 ? void 0 : _b.userId) === null || _c === void 0 ? void 0 : _c.toString())) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Invalid user ID"));
                }
                const globalConfig = await GlobalConfigService.findOneBy({
                    query: {
                        _id: ObjectID.getZeroObjectID().toString(),
                    },
                    props: {
                        isRoot: true,
                    },
                    select: {
                        telegramBotUsername: true,
                    },
                });
                const botUsername = ((_d = globalConfig === null || globalConfig === void 0 ? void 0 : globalConfig.telegramBotUsername) === null || _d === void 0 ? void 0 : _d.trim()) || undefined;
                if (!botUsername) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Telegram bot is not configured. Please contact your OneUptime administrator."));
                }
                return Response.sendJsonObjectResponse(req, res, {
                    verificationCode: item.verificationCode,
                    telegramBotUsername: botUsername,
                    isVerified: Boolean(item.isVerified),
                    deepLinkUrl: `https://t.me/${botUsername}?start=${item.verificationCode}`,
                    startCommand: `/start ${item.verificationCode}`,
                });
            }
            catch (err) {
                return next(err);
            }
        });
        this.router.post(`${(_b = new this.entityType()
            .getCrudApiPath()) === null || _b === void 0 ? void 0 : _b.toString()}/resend-verification-code`, UserMiddleware.getUserMiddleware, async (req, res, next) => {
            var _a, _b, _c;
            try {
                req = req;
                if (!req.body.itemId) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Invalid item ID"));
                }
                const item = await this.service.findOneById({
                    id: req.body["itemId"],
                    props: {
                        isRoot: true,
                    },
                    select: {
                        userId: true,
                    },
                });
                if (!item) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Item not found"));
                }
                if (((_a = item.userId) === null || _a === void 0 ? void 0 : _a.toString()) !==
                    ((_c = (_b = req === null || req === void 0 ? void 0 : req.userAuthorization) === null || _b === void 0 ? void 0 : _b.userId) === null || _c === void 0 ? void 0 : _c.toString())) {
                    return Response.sendErrorResponse(req, res, new BadDataException("Invalid user ID"));
                }
                await this.service.regenerateVerificationCode(req.body.itemId);
                return Response.sendEmptySuccessResponse(req, res);
            }
            catch (err) {
                return next(err);
            }
        });
    }
}
//# sourceMappingURL=UserTelegramAPI.js.map