"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const core_1 = require("@nestjs/core");
const common_1 = require("@nestjs/common");
const app_module_1 = require("../app.module");
const user_services_1 = require("../appapi/user/user.services");
const firebase_service_1 = require("../appapi/fcm/firebase.service");
function readArg(name) {
    const idx = process.argv.indexOf(`--${name}`);
    if (idx === -1)
        return undefined;
    return process.argv[idx + 1];
}
async function main() {
    const logger = new common_1.Logger('test-fcm-user');
    const userIdRaw = readArg('userId') ?? readArg('user_id') ?? readArg('id');
    const userId = userIdRaw ? Number(userIdRaw) : NaN;
    if (!userIdRaw || Number.isNaN(userId)) {
        throw new Error('Missing/invalid --userId (example: --userId 18)');
    }
    const title = readArg('title') ?? 'FCM Test';
    const body = readArg('body') ?? `Test notification for userId=${userId}`;
    const screen = readArg('screen') ?? 'Notifications';
    const app = await core_1.NestFactory.createApplicationContext(app_module_1.AppModule, {
        logger: ['log', 'error', 'warn'],
    });
    try {
        const userService = app.get(user_services_1.UserService);
        const firebaseService = app.get(firebase_service_1.FirebaseService);
        const user = await userService.findById(userId);
        if (!user) {
            throw new Error(`User not found for id=${userId}`);
        }
        const token = String(user.deviceId ?? '').trim();
        if (!token || token.toLowerCase() === 'null' || token.toLowerCase() === 'undefined') {
            throw new Error(`User id=${userId} has no valid deviceId/FCM token`);
        }
        logger.log(`Sending FCM to user id=${userId} (${user.name}) token=${token.slice(0, 12)}…`);
        const result = await firebaseService.sendNotification(token, title, body, screen);
        logger.log(`Done: ${JSON.stringify(result)}`);
    }
    finally {
        await app.close();
    }
}
main().catch((err) => {
    console.error(err);
    process.exitCode = 1;
});
//# sourceMappingURL=test-fcm-user.js.map