"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.sendPartnerAnsweredNotification = exports.sendDailyQuestionReminder = void 0;
const functions = __importStar(require("firebase-functions"));
const admin = __importStar(require("firebase-admin"));
/**
 * Callable function that queues a daily-question reminder.
 *
 * Expected body: { userId: string }
 * Auth context supplies the caller uid; the request `userId` must match
 * the caller to prevent cross-user notification spam.
 *
 * This is a placeholder scheduler. The actual daily scheduling will be
 * handled by a Firestore trigger / Cloud Scheduler integration later.
 */
exports.sendDailyQuestionReminder = functions.https.onCall(async (data, context) => {
    var _a, _b, _c;
    const callerId = (_a = context.auth) === null || _a === void 0 ? void 0 : _a.uid;
    if (!callerId) {
        throw new functions.https.HttpsError('unauthenticated', 'Caller must be authenticated.');
    }
    const userId = data.userId;
    if (!userId || typeof userId !== 'string') {
        throw new functions.https.HttpsError('invalid-argument', 'userId is required.');
    }
    // Users may only request reminders for themselves.
    if (userId !== callerId) {
        throw new functions.https.HttpsError('permission-denied', 'Cannot send notifications to another user.');
    }
    await writeNotificationRecord(userId, 'daily_question', {
        title: (_b = data.title) !== null && _b !== void 0 ? _b : 'Your daily question is waiting!',
        body: (_c = data.body) !== null && _c !== void 0 ? _c : "Tap to answer today's question together.",
    });
    return { queued: true, type: 'daily_question' };
});
/**
 * Callable function that queues a partner-answered notification.
 *
 * Expected body: { userId: string }
 * Auth context must match the request `userId`.
 */
exports.sendPartnerAnsweredNotification = functions.https.onCall(async (data, context) => {
    var _a, _b, _c;
    const callerId = (_a = context.auth) === null || _a === void 0 ? void 0 : _a.uid;
    if (!callerId) {
        throw new functions.https.HttpsError('unauthenticated', 'Caller must be authenticated.');
    }
    const userId = data.userId;
    if (!userId || typeof userId !== 'string') {
        throw new functions.https.HttpsError('invalid-argument', 'userId is required.');
    }
    if (userId !== callerId) {
        throw new functions.https.HttpsError('permission-denied', 'Cannot send notifications to another user.');
    }
    await writeNotificationRecord(userId, 'partner_answered', {
        title: (_b = data.title) !== null && _b !== void 0 ? _b : 'Your partner just answered!',
        body: (_c = data.body) !== null && _c !== void 0 ? _c : 'See what your partner shared.',
    });
    return { queued: true, type: 'partner_answered' };
});
async function writeNotificationRecord(userId, type, message) {
    const db = admin.firestore();
    await db
        .collection('users')
        .doc(userId)
        .collection('notification_queue')
        .add(Object.assign(Object.assign({ type }, message), { read: false, sent: false, createdAt: admin.firestore.FieldValue.serverTimestamp() }));
}
//# sourceMappingURL=reminders.js.map