import NotificationService from "../services/NotificationService.js";
import settingsService from "../services/SettingsService.js";
import { parseQueryParams } from "../utils/router.js";
import { adminOnly } from "../utils/auth.js";
import { requireAuth } from "../utils/common.js";
const registerEndpoint = (app) => {
    // Get user's notifications with pagination and filtering
    app.get("/notifications", async (req, res, next) => {
        try {
            requireAuth(req);
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const query = parseQueryParams(req.query);
            let filter = {
                userId: req.accountability.user.id,
            };
            if (query.filter) {
                filter = { AND: [{ ...query.filter }, { userId: req.accountability.user.id }] };
            }
            query.filter = filter;
            const results = await notificationService.getUserNotifications(query);
            res.json(results);
        }
        catch (error) {
            next(error);
        }
    });
    // Get unread notifications count
    app.get("/notifications/unread/count", async (req, res, next) => {
        try {
            requireAuth(req);
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const count = await notificationService.getUnreadCount(req.accountability.user.id);
            res.json({ count });
        }
        catch (error) {
            next(error);
        }
    });
    // Mark notifications as seen
    app.post("/notifications/mark-seen", async (req, res, next) => {
        try {
            requireAuth(req);
            const { notificationIds } = req.body; // Optional array of specific notification IDs
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const updatedCount = await notificationService.markAsSeen(req.accountability.user.id, notificationIds);
            res.json({
                message: "Notifications marked as seen",
                count: updatedCount,
            });
        }
        catch (error) {
            next(error);
        }
    });
    // Mark notifications as unseen (unread) — mirrors mark-seen
    app.post("/notifications/mark-unseen", async (req, res, next) => {
        try {
            requireAuth(req);
            const { notificationIds } = req.body; // Optional array of specific notification IDs
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const updatedCount = await notificationService.markAsUnseen(req.accountability.user.id, notificationIds);
            res.json({
                message: "Notifications marked as unseen",
                count: updatedCount,
            });
        }
        catch (error) {
            next(error);
        }
    });
    // Per-type notification link templates (settings metadata.notificationLinks).
    // Available to ANY authenticated user: the public GET /settings allow-list
    // deliberately hides `metadata`, so this exposes only the link mapping.
    app.get("/notifications/links", async (req, res, next) => {
        try {
            requireAuth(req);
            const tenantId = req.accountability?.tenant;
            const settings = tenantId
                ? await settingsService.getTenantSettings(tenantId)
                : settingsService.getGlobalSettings();
            const raw = settings?.metadata?.notificationLinks;
            const links = {};
            if (raw && typeof raw === "object" && !Array.isArray(raw)) {
                for (const [type, template] of Object.entries(raw)) {
                    if (typeof template === "string" && template.length > 0) {
                        links[type] = template;
                    }
                }
            }
            res.json({ data: links });
        }
        catch (error) {
            next(error);
        }
    });
    // Delete notifications
    app.delete("/notifications", async (req, res, next) => {
        try {
            requireAuth(req);
            const { notificationIds } = req.body; // Optional array of specific notification IDs
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const deletedCount = await notificationService.deleteForUser(req.accountability.user.id, notificationIds);
            res.json({
                message: "Notifications deleted",
                count: deletedCount,
            });
        }
        catch (error) {
            next(error);
        }
    });
    // Send notifications (admin only)
    app.post("/notifications/send", adminOnly, async (req, res, next) => {
        try {
            const { type, title, message, data, userIds, tenant_Id } = req.body;
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const notificationIds = await notificationService.send({
                type,
                title,
                message,
                data,
                userIds,
                tenant_Id,
            });
            res.json({
                message: "Notifications sent successfully",
                notificationIds,
            });
        }
        catch (error) {
            next(error);
        }
    });
    // Cleanup old notifications (admin only)
    app.post("/notifications/cleanup", adminOnly, async (req, res, next) => {
        try {
            const { days = 30 } = req.body;
            const notificationService = new NotificationService({
                accountability: req.accountability,
            });
            const deletedCount = await notificationService.cleanupOldNotifications(days);
            res.json({
                message: "Old notifications cleaned up",
                count: deletedCount,
            });
        }
        catch (error) {
            next(error);
        }
    });
};
export default {
    id: "notifications",
    handler: registerEndpoint,
};
//# sourceMappingURL=notification.route.js.map