"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const index_1 = require("../index");
const errorHandler_1 = require("../middleware/errorHandler");
const auth_1 = require("../middleware/auth");
const joi_1 = __importDefault(require("joi"));
const uuid_1 = require("uuid");
const router = (0, express_1.Router)();
const subscribeSchema = joi_1.default.object({
    email: joi_1.default.string().email().required(),
});
router.post('/subscribe', (0, errorHandler_1.asyncHandler)(async (req, res) => {
    const { error, value } = subscribeSchema.validate(req.body);
    if (error) {
        return res.status(400).json({
            success: false,
            message: error.details[0].message,
        });
    }
    const { email } = value;
    const existing = await index_1.prisma.subscription.findUnique({
        where: { email },
    });
    if (existing) {
        if (existing.isActive) {
            return res.status(400).json({
                success: false,
                message: 'Email is already subscribed',
            });
        }
        else {
            await index_1.prisma.subscription.update({
                where: { email },
                data: { isActive: true },
            });
        }
    }
    else {
        await index_1.prisma.subscription.create({
            data: {
                email,
                unsubscribeToken: (0, uuid_1.v4)(),
            },
        });
    }
    res.json({
        success: true,
        message: 'Successfully subscribed to notifications',
    });
}));
router.get('/', auth_1.authenticateToken, (0, errorHandler_1.asyncHandler)(async (req, res) => {
    const subscribers = await index_1.prisma.subscription.findMany({
        orderBy: { createdAt: 'desc' },
        select: {
            id: true,
            email: true,
            isActive: true,
            createdAt: true,
        },
    });
    const stats = {
        total: subscribers.length,
        active: subscribers.filter(sub => sub.isActive).length,
        inactive: subscribers.filter(sub => !sub.isActive).length,
    };
    res.json({
        success: true,
        subscribers,
        stats,
    });
}));
router.put('/:id/toggle', auth_1.authenticateToken, (0, errorHandler_1.asyncHandler)(async (req, res) => {
    const subscriberId = parseInt(req.params.id);
    if (isNaN(subscriberId)) {
        return res.status(400).json({
            success: false,
            message: 'Invalid subscriber ID',
        });
    }
    const subscriber = await index_1.prisma.subscription.findUnique({
        where: { id: subscriberId },
    });
    if (!subscriber) {
        return res.status(404).json({
            success: false,
            message: 'Subscriber not found',
        });
    }
    const updated = await index_1.prisma.subscription.update({
        where: { id: subscriberId },
        data: { isActive: !subscriber.isActive },
    });
    res.json({
        success: true,
        message: `Subscriber ${updated.isActive ? 'activated' : 'deactivated'} successfully`,
        subscriber: updated,
    });
}));
exports.default = router;
//# sourceMappingURL=subscriptions.js.map