"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.onGameSessionUpdate = void 0;
const functions = __importStar(require("firebase-functions"));
const admin = __importStar(require("firebase-admin"));
/**
 * Firestore trigger that notifies partners when a game session is created or completed.
 *
 * Path: couples/{coupleId}/sessions/{sessionId}
 * Condition: onWrite (create, update, delete)
 */
exports.onGameSessionUpdate = functions.firestore
    .document('couples/{coupleId}/sessions/{sessionId}')
    .onWrite(async (change, context) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
    const { coupleId, sessionId } = context.params;
    const db = admin.firestore();
    const messaging = admin.messaging();
    // Get the session document
    const sessionDoc = await db.collection('couples').doc(coupleId).collection('sessions').doc(sessionId).get();
    const session = sessionDoc.data();
    if (!session) {
        console.log(`[onGameSessionUpdate] session ${sessionId} not found, skipping`);
        return;
    }
    // Get couple info
    const coupleDoc = await db.collection('couples').doc(coupleId).get();
    if (!coupleDoc.exists) {
        console.warn(`[onGameSessionUpdate] couple ${coupleId} not found`);
        return;
    }
    const coupleData = (_a = coupleDoc.data()) !== null && _a !== void 0 ? _a : {};
    const userIds = ((_b = coupleData.userIds) !== null && _b !== void 0 ? _b : []);
    if (userIds.length !== 2) {
        console.warn(`[onGameSessionUpdate] invalid couple ${coupleId}: expected 2 users, got ${userIds.length}`);
        return;
    }
    const partnerA = userIds[0];
    const partnerB = userIds[1];
    // Get user display names for notifications
    const userA = await db.collection('users').doc(partnerA).get();
    const userB = await db.collection('users').doc(partnerB).get();
    const partnerAName = (_d = (_c = userA.data()) === null || _c === void 0 ? void 0 : _c.displayName) !== null && _d !== void 0 ? _d : 'Partner A';
    const partnerBName = (_f = (_e = userB.data()) === null || _e === void 0 ? void 0 : _e.displayName) !== null && _f !== void 0 ? _f : 'Partner B';
    const avatarA = (_g = userA.data()) === null || _g === void 0 ? void 0 : _g.photoUrl;
    const avatarB = (_h = userB.data()) === null || _h === void 0 ? void 0 : _h.photoUrl;
    // Check if session was just created (status = "active")
    const previousData = (_j = change.before.data()) !== null && _j !== void 0 ? _j : {};
    const currentData = (_k = change.after.data()) !== null && _k !== void 0 ? _k : {};
    const wasInactive = ((_l = previousData.status) !== null && _l !== void 0 ? _l : '') !== 'active';
    const isActiveNow = currentData.status === 'active';
    if (wasInactive && isActiveNow) {
        // New session started — notify the OTHER partner, naming the person who started it.
        const startedBy = currentData.startedByUserId;
        const gameType = (_m = currentData.gameType) !== null && _m !== void 0 ? _m : 'wheel';
        const recipientId = startedBy === partnerA ? partnerB : partnerA;
        const starterName = startedBy === partnerA ? partnerAName : partnerBName;
        const starterAvatar = startedBy === partnerA ? avatarA : avatarB;
        await notifyPartner(db, messaging, recipientId, starterName, gameType, 'partner_started_game', `${starterName} has started a game. Tap to join!`, coupleId, starterAvatar);
        return;
    }
    // Check if session was completed
    const wasActive = ((_o = previousData.status) !== null && _o !== void 0 ? _o : '') === 'active';
    const isCompletedNow = currentData.status === 'completed';
    if (wasActive && isCompletedNow) {
        // The session is complete (both partners have answered) — the reveal is ready for each of
        // them, so notify BOTH, each naming the OTHER partner.
        const gt = (_p = currentData.gameType) !== null && _p !== void 0 ? _p : 'wheel';
        await notifyPartner(db, messaging, partnerA, partnerBName, gt, 'partner_finished_game', `${partnerBName} finished — tap to see your results!`, coupleId, avatarB);
        await notifyPartner(db, messaging, partnerB, partnerAName, gt, 'partner_finished_game', `${partnerAName} finished — tap to see your results!`, coupleId, avatarA);
        return;
    }
});
/**
 * Send notification to partner via FCM and write to notification_queue.
 */
async function notifyPartner(db, messaging, partnerId, partnerName, gameType, notificationType, body, coupleId, senderAvatarUrl) {
    var _a;
    const title = notificationType === 'partner_finished_game'
        ? `${partnerName} finished the game`
        : `${partnerName} is playing`;
    const notificationPayload = {
        type: notificationType,
        title,
        body: body,
    };
    // Write an in-app notification record for the partner
    await db
        .collection('users')
        .doc(partnerId)
        .collection('notification_queue')
        .add(Object.assign(Object.assign({}, notificationPayload), { read: false, createdAt: admin.firestore.FieldValue.serverTimestamp() }));
    // Collect the partner's FCM tokens
    const tokens = [];
    const partnerUserDoc = await db.collection('users').doc(partnerId).get();
    if (partnerUserDoc.exists) {
        const legacyToken = (_a = partnerUserDoc.data()) === null || _a === void 0 ? void 0 : _a.fcmToken;
        if (typeof legacyToken === 'string' && legacyToken.length > 0) {
            tokens.push(legacyToken);
        }
    }
    const tokenSnapshot = await db
        .collection('users')
        .doc(partnerId)
        .collection('fcmTokens')
        .get();
    tokenSnapshot.docs.forEach((doc) => {
        var _a;
        const t = (_a = doc.data()) === null || _a === void 0 ? void 0 : _a.token;
        if (typeof t === 'string' && t.length > 0 && !tokens.includes(t)) {
            tokens.push(t);
        }
    });
    if (tokens.length === 0) {
        console.log(`[notifyPartner] no FCM tokens for ${partnerId}`);
        return;
    }
    const fcmMessage = {
        token: tokens[0],
        notification: {
            title: notificationPayload.title,
            body: notificationPayload.body,
        },
        data: Object.assign({ type: notificationPayload.type, couple_id: coupleId, game_type: gameType }, (senderAvatarUrl && senderAvatarUrl.length > 0
            ? { sender_avatar_url: senderAvatarUrl }
            : {})),
    };
    const sendResults = await Promise.allSettled(tokens.map((token) => messaging.send(Object.assign(Object.assign({}, fcmMessage), { token }))));
    const failures = [];
    sendResults.forEach((result, index) => {
        if (result.status === 'rejected') {
            failures.push(`${tokens[index]}: ${String(result.reason)}`);
        }
    });
    if (failures.length > 0) {
        console.error(`[notifyPartner] some notifications failed:`, failures);
    }
    else {
        console.log(`[notifyPartner] notified ${partnerId} (${notificationType})`);
    }
}
//# sourceMappingURL=onGameSessionUpdate.js.map