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);
};
import DatabaseService from "./DatabaseService";
import BadDataException from "../../Types/Exception/BadDataException";
import PushDeviceType from "../../Types/PushNotification/PushDeviceType";
import UserPush from "../../Models/DatabaseModels/UserPush";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(UserPush);
    }
    async onBeforeCreate(createBy) {
        if (!createBy.data.deviceToken) {
            throw new BadDataException("Device token is required");
        }
        if (!createBy.data.deviceType) {
            throw new BadDataException("Device type is required");
        }
        // Validate device type
        const validDeviceTypes = Object.values(PushDeviceType);
        if (!validDeviceTypes.includes(createBy.data.deviceType)) {
            throw new BadDataException("Device type must be one of: " + validDeviceTypes.join(", "));
        }
        // Check if this device token already exists for this user and project
        const existingCount = await this.countBy({
            query: {
                deviceToken: createBy.data.deviceToken,
                userId: createBy.data.userId,
                projectId: createBy.data.projectId,
            },
            props: {
                isRoot: true,
            },
        });
        if (existingCount.toNumber() > 0) {
            throw new BadDataException("This device is already registered for push notifications");
        }
        return { carryForward: null, createBy };
    }
    async onBeforeDelete(deleteBy) {
        // Add any cleanup logic here if needed
        return { carryForward: null, deleteBy };
    }
    async verifyDevice(deviceId) {
        await this.updateOneBy({
            query: {
                _id: deviceId,
            },
            data: {
                isVerified: true,
            },
            props: {
                isRoot: true,
            },
        });
    }
    async unverifyDevice(deviceId) {
        await this.updateOneBy({
            query: {
                _id: deviceId,
            },
            data: {
                isVerified: false,
            },
            props: {
                isRoot: true,
            },
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeCreate", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "onBeforeDelete", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], Service.prototype, "verifyDevice", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], Service.prototype, "unverifyDevice", null);
export default new Service();
//# sourceMappingURL=UserPushService.js.map