"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.OfflineService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
const estate_scope_1 = require("../common/estate-scope");
const prisma_service_1 = require("../database/prisma.service");
let OfflineService = class OfflineService {
    prisma;
    constructor(prisma) {
        this.prisma = prisma;
    }
    async bootstrap(user, estateId) {
        const scopedEstateId = (0, estate_scope_1.resolveEstateScope)(user, estateId);
        const now = new Date();
        const endOfToday = new Date(now);
        endOfToday.setHours(23, 59, 59, 999);
        const [activePasses, residents, recentLogs] = await Promise.all([
            this.prisma.visitorPass.findMany({
                where: {
                    estateId: scopedEstateId,
                    status: 'ACTIVE',
                    validFrom: { lte: endOfToday },
                    validUntil: { gte: now }
                },
                include: { visitor: true, hostResident: true },
                take: 500
            }),
            this.prisma.resident.findMany({
                where: { estateId: scopedEstateId, status: 'ACTIVE' },
                select: { id: true, name: true, unit: true, phone: true },
                take: 1000
            }),
            this.prisma.auditLog.findMany({
                where: { estateId: scopedEstateId },
                orderBy: { timestamp: 'desc' },
                take: 100
            })
        ]);
        return {
            serverTime: now,
            activePasses,
            residents,
            recentLogs
        };
    }
    async sync(dto, user) {
        const estateId = (0, estate_scope_1.resolveEstateScope)(user, dto.estateId);
        const results = [];
        for (const item of dto.items) {
            const record = await this.prisma.offlineSyncItem.upsert({
                where: {
                    deviceId_clientEventId: {
                        deviceId: dto.deviceId,
                        clientEventId: item.clientEventId
                    }
                },
                create: {
                    estateId,
                    deviceId: dto.deviceId,
                    clientEventId: item.clientEventId,
                    eventType: item.eventType,
                    payload: item.payload,
                    status: client_1.SyncItemStatus.APPLIED,
                    appliedAt: new Date()
                },
                update: {}
            });
            results.push(record);
        }
        return {
            accepted: results.length,
            items: results
        };
    }
};
exports.OfflineService = OfflineService;
exports.OfflineService = OfflineService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], OfflineService);
//# sourceMappingURL=offline.service.js.map