"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupportTicketService = void 0;
const crypto_1 = require("crypto");
const logger_1 = require("../utils/logger");
const proxy_1 = require("../config/proxy");
const log = (0, logger_1.createLogger)('SupportTicketService');
/**
 * Sends support tickets via the configured webhook endpoint (e.g. a Supabase
 * Edge Function), which handles Resend delivery in a secure environment.
 */
class SupportTicketService {
    fetchFn;
    constructor(options = {}) {
        this.fetchFn = options.fetchFn ?? fetch;
    }
    async submitTicket(input) {
        const ticketId = (0, crypto_1.randomUUID)();
        const response = await this.fetchFn(proxy_1.SUPPORT_TICKET_WEBHOOK_URL, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                ticketId,
                type: input.type,
                message: input.message,
                contactEmail: input.contactEmail,
                metadata: input.metadata,
            }),
        });
        if (!response.ok) {
            const responseText = await response.text();
            throw new Error(`Support ticket submission failed: ${response.status} ${responseText}`);
        }
        const data = await response.json();
        const providerMessageId = typeof data === 'object' &&
            data !== null &&
            'providerMessageId' in data &&
            typeof data.providerMessageId ===
                'string'
            ? data.providerMessageId
            : undefined;
        log.info(`Submitted support ticket ${ticketId}${providerMessageId ? ` (provider id: ${providerMessageId})` : ''}`);
        return { ticketId, providerMessageId };
    }
}
exports.SupportTicketService = SupportTicketService;
//# sourceMappingURL=SupportTicketService.js.map