"use strict";
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var GupshupWebhookController_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GupshupWebhookController = void 0;
const openapi = require("@nestjs/swagger");
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const auth_decorator_1 = require("../decorators/auth.decorator");
const public_decorator_1 = require("../decorators/public.decorator");
const auth_type_enum_1 = require("../enums/auth-type.enum");
let GupshupWebhookController = GupshupWebhookController_1 = class GupshupWebhookController {
    constructor() {
        this.logger = new common_1.Logger(GupshupWebhookController_1.name);
    }
    async receiveWebhook(req, body) {
        const userAgent = req.headers["user-agent"] ?? null;
        this.logger.log(`Received Gupshup WhatsApp webhook${userAgent ? ` from ${userAgent}` : ""}`);
        this.logger.debug(`Gupshup webhook payload: ${JSON.stringify(body)}`);
        const statusInfo = this.extractStatusInfo(body);
        if (statusInfo) {
            this.logger.log(`Gupshup delivery update: status=${statusInfo.status ?? "unknown"}, messageId=${statusInfo.messageId ?? "n/a"}, destination=${statusInfo.destination ?? "n/a"}, reason=${statusInfo.reason ?? "n/a"}`);
        }
        return {
            success: true,
            message: "Webhook received",
        };
    }
    extractStatusInfo(body) {
        if (!body || typeof body !== "object") {
            return null;
        }
        const payload = body;
        const status = this.asString(payload.status) ||
            this.asString(payload.messageStatus) ||
            this.asString(payload.payload?.status) ||
            this.asString(payload.payload?.type);
        const messageId = this.asString(payload.messageId) ||
            this.asString(payload.id) ||
            this.asString(payload.payload?.id) ||
            this.asString(payload.payload?.messageId);
        const destination = this.asString(payload.destination) ||
            this.asString(payload.phone) ||
            this.asString(payload.payload?.destination) ||
            this.asString(payload.payload?.phone);
        const reason = this.asString(payload.reason) ||
            this.asString(payload.error) ||
            this.asString(payload.payload?.reason) ||
            this.asString(payload.payload?.error);
        if (!status && !messageId && !destination && !reason) {
            return null;
        }
        return { status, messageId, destination, reason };
    }
    asString(value) {
        return typeof value === "string" && value.trim() ? value.trim() : undefined;
    }
};
exports.GupshupWebhookController = GupshupWebhookController;
__decorate([
    (0, public_decorator_1.Public)(),
    (0, common_1.Post)(),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    openapi.ApiResponse({ status: common_1.HttpStatus.OK }),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], GupshupWebhookController.prototype, "receiveWebhook", null);
exports.GupshupWebhookController = GupshupWebhookController = GupshupWebhookController_1 = __decorate([
    (0, auth_decorator_1.Auth)(auth_type_enum_1.AuthType.None),
    (0, common_1.Controller)("webhook/whatsapp/gupshup"),
    (0, swagger_1.ApiTags)("Solid Core")
], GupshupWebhookController);
//# sourceMappingURL=gupshup-webhook.controller.js.map