"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);
};
var FirebaseService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirebaseService = void 0;
const common_1 = require("@nestjs/common");
const admin = require("firebase-admin");
const googleapis_1 = require("googleapis");
const path = require("path");
const fs = require("fs");
let FirebaseService = FirebaseService_1 = class FirebaseService {
    constructor() {
        this.logger = new common_1.Logger(FirebaseService_1.name);
        const devPath = path.join(process.cwd(), 'src/firebaseserviceaccount.json');
        const prodPath = path.join(process.cwd(), 'firebaseserviceaccount.json');
        const firebaseConfigPath = fs.existsSync(prodPath) ? prodPath : devPath;
        if (!admin.apps.length) {
            admin.initializeApp({
                credential: admin.credential.cert(firebaseConfigPath),
            });
            this.logger.log('✅ Firebase Admin initialized');
        }
    }
    async sendNotification(token, title, body, screen, data = { screen: screen }) {
        const message = {
            token,
            notification: {
                title,
                body,
            },
            data,
            android: {
                priority: 'high',
                notification: {
                    sound: 'default',
                    channelId: 'default',
                },
            },
            apns: {
                payload: {
                    aps: {
                        alert: {
                            title,
                            body,
                        },
                        sound: 'default',
                        'content-available': 1,
                    },
                },
                headers: {
                    'apns-priority': '10',
                },
            },
        };
        try {
            const response = await admin.messaging().send(message);
            this.logger.log(`✅ Notification sent successfully: ${response}`);
            return { success: true, response };
        }
        catch (error) {
            this.logger.error('❌ Error sending notification', error);
            throw error;
        }
    }
    async getAccessToken() {
        const devPath = path.join(process.cwd(), 'src/firebaseserviceaccount.json');
        const prodPath = path.join(process.cwd(), 'firebaseserviceaccount.json');
        const keyFile = fs.existsSync(prodPath) ? prodPath : devPath;
        const auth = new googleapis_1.google.auth.GoogleAuth({
            keyFile,
            scopes: ['https://www.googleapis.com/auth/firebase.messaging'],
        });
        try {
            const accessToken = await auth.getAccessToken();
            this.logger.log('🔑 Access Token fetched successfully');
            return accessToken || '';
        }
        catch (error) {
            this.logger.error('❌ Error fetching access token', error);
            throw error;
        }
    }
};
exports.FirebaseService = FirebaseService;
exports.FirebaseService = FirebaseService = FirebaseService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [])
], FirebaseService);
//# sourceMappingURL=firebase.service.js.map