package app.closer.notifications

import javax.inject.Inject

/**
 * Schedules partner-trigger notifications in response to partner actions.
 *
 * This class is intentionally synchronous: it validates quiet hours, opt-outs,
 * and rate limits at the moment the event is received. Background deferral is
 * handled by the backend FCM delivery schedule and [PartnerNotificationManager].
 */
class PartnerNotificationScheduler @Inject constructor(
    private val notificationManager: PartnerNotificationManager
) {

    /**
     * The partner answered today's question; prompt the user to answer too.
     */
    suspend fun onPartnerAnswered(coupleId: String, questionId: String? = null) {
        notificationManager.show(
            PartnerNotificationType.PARTNER_ANSWERED,
            coupleId,
            PartnerNotificationPayload(questionId = questionId)
        )
    }

    /**
     * Both partners have answered and the reveal is ready.
     */
    suspend fun onRevealReady(coupleId: String, questionId: String) {
        notificationManager.show(
            PartnerNotificationType.REVEAL_READY,
            coupleId,
            PartnerNotificationPayload(questionId = questionId)
        )
    }

    /**
     * The partner started a game for the couple.
     */
    suspend fun onPartnerStartedGame(coupleId: String, gameSessionId: String? = null) {
        notificationManager.show(
            PartnerNotificationType.PARTNER_STARTED_GAME,
            coupleId,
            PartnerNotificationPayload(gameSessionId = gameSessionId)
        )
    }

    /**
     * The partner completed their side of a two-player game.
     */
    suspend fun onPartnerCompletedPart(coupleId: String, gameSessionId: String? = null) {
        notificationManager.show(
            PartnerNotificationType.PARTNER_COMPLETED_PART,
            coupleId,
            PartnerNotificationPayload(gameSessionId = gameSessionId)
        )
    }

    /**
     * A daily connection challenge is waiting for the couple.
     */
    suspend fun onChallengeWaiting(coupleId: String, challengeId: String? = null) {
        notificationManager.show(
            PartnerNotificationType.CHALLENGE_WAITING,
            coupleId,
            PartnerNotificationPayload(challengeId = challengeId)
        )
    }

    /**
     * A previously sealed memory capsule reached its unlock date.
     */
    suspend fun onCapsuleUnlocked(coupleId: String, capsuleId: String? = null) {
        notificationManager.show(
            PartnerNotificationType.CAPSULE_UNLOCKED,
            coupleId,
            PartnerNotificationPayload(capsuleId = capsuleId)
        )
    }
}
