"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationsService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
const audit_service_1 = require("../audit/audit.service");
const estate_scope_1 = require("../common/estate-scope");
const prisma_service_1 = require("../database/prisma.service");
let NotificationsService = class NotificationsService {
    audit;
    prisma;
    constructor(audit, prisma) {
        this.audit = audit;
        this.prisma = prisma;
    }
    async send(dto, user) {
        const estateId = (0, estate_scope_1.resolveEstateScope)(user, dto.estateId);
        const notification = await this.prisma.appNotification.create({
            data: {
                estateId,
                channel: this.normalizeChannel(dto.channel),
                status: client_1.NotificationStatus.PENDING,
                title: String(dto.metadata?.title ?? 'GateWise notification'),
                body: dto.message,
                data: {
                    recipient: dto.recipient,
                    requestedBy: user.id,
                    ...dto.metadata
                }
            }
        });
        await this.audit.record({
            estateId,
            actorType: 'SYSTEM',
            actorId: 'notification-service',
            action: 'NOTIFICATION_QUEUED',
            metadata: {
                channel: dto.channel,
                recipient: dto.recipient,
                message: dto.message,
                requestedBy: user.id,
                ...dto.metadata
            }
        });
        return {
            status: 'QUEUED',
            notificationId: notification.id,
            channel: dto.channel,
            criticalPath: false
        };
    }
    listMine(user, unreadOnly) {
        return this.prisma.appNotification.findMany({
            where: {
                estateId: user.estateId ?? undefined,
                userId: user.id,
                readAt: unreadOnly === 'true' ? null : undefined
            },
            orderBy: { createdAt: 'desc' },
            take: 100
        });
    }
    async markRead(id, user) {
        const notification = await this.prisma.appNotification.updateMany({
            where: { id, userId: user.id, estateId: user.estateId ?? undefined },
            data: { status: client_1.NotificationStatus.READ, readAt: new Date() }
        });
        return { updated: notification.count };
    }
    normalizeChannel(channel) {
        if (channel === 'SMS') {
            return client_1.NotificationChannel.SMS;
        }
        if (channel === 'PUSH') {
            return client_1.NotificationChannel.PUSH;
        }
        return client_1.NotificationChannel.IN_APP;
    }
};
exports.NotificationsService = NotificationsService;
exports.NotificationsService = NotificationsService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [audit_service_1.AuditService,
        prisma_service_1.PrismaService])
], NotificationsService);
//# sourceMappingURL=notifications.service.js.map