"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSupportRouter = void 0;
const express_1 = require("express");
const zod_1 = require("zod");
const index_core_1 = require("../index-core");
const SupportTicketService_1 = require("../services/SupportTicketService");
const log = (0, index_core_1.createLogger)('SupportRoutes');
const SUPPORT_TICKET_FAILURE_DETAILS = 'Please try again in a few minutes.';
const SupportTicketMetadataSchema = zod_1.z
    .object({
    posthogSessionId: zod_1.z.string().trim().min(1).max(200).optional(),
    posthogDistinctId: zod_1.z.string().trim().min(1).max(200).optional(),
    appVersion: zod_1.z.string().trim().min(1).max(100).optional(),
    appEnvironment: zod_1.z.string().trim().min(1).max(100).optional(),
    pageUrl: zod_1.z.string().trim().min(1).max(2000).optional(),
    userAgent: zod_1.z.string().trim().min(1).max(1000).optional(),
    submittedAt: zod_1.z.string().trim().min(1).max(100).optional(),
})
    .strict()
    .optional();
const SupportTicketRequestSchema = zod_1.z
    .object({
    type: zod_1.z.enum(['issue', 'feedback']),
    message: zod_1.z.string().trim().min(1).max(5000),
    metadata: SupportTicketMetadataSchema,
    contactEmail: zod_1.z.string().trim().email().max(320).optional(),
})
    .strict();
/**
 * Creates support ticket routes for issue and feedback submissions.
 */
const createSupportRouter = (telemetry, deliveryService = new SupportTicketService_1.SupportTicketService()) => {
    const router = (0, express_1.Router)();
    router.post('/support/tickets', async (req, res) => {
        const parseResult = SupportTicketRequestSchema.safeParse(req.body);
        if (!parseResult.success) {
            return res.status(400).json({
                error: 'Invalid support ticket payload',
                details: parseResult.error.flatten(),
            });
        }
        const request = parseResult.data;
        try {
            const result = await deliveryService.submitTicket(request);
            telemetry?.track('support_ticket_submitted', {
                ticket_type: request.type,
                has_contact_email: Boolean(request.contactEmail),
                has_posthog_session_id: Boolean(request.metadata?.posthogSessionId),
            });
            return res.json({
                success: true,
                ticketId: result.ticketId,
            });
        }
        catch (error) {
            const message = error instanceof Error ? error.message : 'Unknown submission error';
            log.error('Failed to submit support ticket:', message);
            telemetry?.track('support_ticket_submit_failed', {
                ticket_type: request.type,
                has_contact_email: Boolean(request.contactEmail),
                error: message,
            });
            return res.status(500).json({
                error: 'Failed to submit support ticket',
                details: SUPPORT_TICKET_FAILURE_DETAILS,
            });
        }
    });
    return router;
};
exports.createSupportRouter = createSupportRouter;
//# sourceMappingURL=support.js.map