diff --git "a/app/auth.py" "b/app/auth.py" new file mode 100644 --- /dev/null +++ "b/app/auth.py" @@ -0,0 +1,7665 @@ +from app.models import AccountDeleteRequest, AccountCancelDeleteRequest +import httpx +# ========================================================= +# 1. IMPORTS & SETUP +# ========================================================= +import os +import re +import json +import time +import logging +import asyncio +import datetime +import gzip +import hmac +import hashlib +import uuid +from pathlib import Path +from typing import List, Optional, Dict, Any, Tuple, Union + +from fastapi import APIRouter, Request, HTTPException, status, Header, Query +from fastapi.responses import Response, JSONResponse, StreamingResponse +from pydantic import BaseModel, Field, field_validator, model_validator + + +import firebase_admin +from firebase_admin import credentials, auth as firebase_auth +from bson import json_util +from fastapi import Depends +from pymongo import ReturnDocument +from pymongo.errors import ( + AutoReconnect, + ConnectionFailure, + DuplicateKeyError, + ExecutionTimeout, + NetworkTimeout, + PyMongoError, + ServerSelectionTimeoutError, +) + + +from app.database import ( + users_collection, + hadith_collection, + chats_collection, + masjid_collection, + permanent_masjids_collection, + quran_collection, + pending_approvals_collection, + daily_progress_collection, + qaza_collection, + nimaz_stats_collection, + tasbeeh_collection, + tasbeeh_daily_collection, + tasbeeh_stats_collection, + admin_bootstrap_lock_collection, +) +from app.core.config import settings, Settings +from app.limiter import limiter +from app.auth_deps import ( + get_current_user, + get_optional_user, + require_admin, + require_superadmin, + enforce_self_or_admin, + invalidate_user_cache, + is_superadmin_email, + is_superadmin_user, +) +from app import realtime +from app.role_application_helpers import ( + decode_and_validate_image_data_uri, + summarize_application_for_list, + validate_dob, + validate_phone_with_country_code, +) + +# --- ENTERPRISE LOGGING --- +logger = logging.getLogger("sahihislam.auth") +logger.setLevel(logging.INFO) + +router = APIRouter() + +# ========================================================= +# MASJID ASSOCIATION NORMALIZATION & ELIGIBILITY HELPERS +# ========================================================= + +def normalize_user_masjid_fields(user: dict) -> dict: + """Ensures consistent, non-resurrecting representation of masjid associations. + Root fields are authoritative. Legacy community.masjid fields are only consulted + if the root key is completely absent (never if set to empty string or empty list). + """ + # 1. selected_masjid_id + if "selected_masjid_id" in user and user["selected_masjid_id"] is not None: + user["selected_masjid_id"] = str(user["selected_masjid_id"]).strip() + else: + user["selected_masjid_id"] = str(user.get("community", {}).get("masjid", {}).get("selected_masjid_id", "")).strip() + + # 2. followed_masjids + if "followed_masjids" in user and user["followed_masjids"] is not None: + raw_followed = user["followed_masjids"] + user["followed_masjids"] = list(raw_followed) if isinstance(raw_followed, (list, tuple, set)) else [] + else: + raw_nested = user.get("community", {}).get("masjid", {}).get("followed_masjids", []) + user["followed_masjids"] = list(raw_nested) if isinstance(raw_nested, (list, tuple, set)) else [] + + # 3. managed_masjid_id + if "managed_masjid_id" in user and user["managed_masjid_id"] is not None: + user["managed_masjid_id"] = str(user["managed_masjid_id"]).strip() + else: + user["managed_masjid_id"] = str(user.get("community", {}).get("masjid", {}).get("managed_masjid_id", "")).strip() + + # 4. masjid_status + if "masjid_status" in user and user["masjid_status"] is not None: + user["masjid_status"] = str(user["masjid_status"]).strip() + else: + user["masjid_status"] = str(user.get("community", {}).get("masjid", {}).get("status", "NONE")).strip() + + # 5. mufti_status & rejection reason + if "mufti_status" in user and user["mufti_status"] is not None: + user["mufti_status"] = str(user["mufti_status"]).strip() + else: + user["mufti_status"] = str(user.get("community", {}).get("mufti", {}).get("status", "NONE")).strip() + user["mufti_rejection_reason"] = str(user.get("mufti_rejection_reason") or "").strip() + + # 6. app_admin_status & rejection reason + raw_role = str(user.get("role") or user.get("auth", {}).get("role") or "USER").strip().upper() + if "app_admin_status" in user and user["app_admin_status"] is not None: + user["app_admin_status"] = str(user["app_admin_status"]).strip() + else: + user["app_admin_status"] = "APPROVED" if raw_role in ("ADMIN", "APP_ADMIN") else "NONE" + user["app_admin_rejection_reason"] = str(user.get("app_admin_rejection_reason") or "").strip() + + return user + + +async def find_selectable_masjid(clean_id: str) -> Optional[dict]: + """Finds an eligible, selectable masjid across active, permanent, and sanctuary collections. + Ensures that deleted or inactive masjids cannot be selected. + """ + if not clean_id: + return None + + # 1. Live masjid_collection (not deleted, not inactive) + masjid = await masjid_collection.find_one( + { + "masjid_id": clean_id, + "is_deleted": {"$ne": True}, + "status": {"$ne": "DELETED"}, + }, + {"_id": 1, "masjid_id": 1, "name": 1, "is_live": 1, "status": 1} + ) + if masjid: + return masjid + + # 2. Permanent directory collection + perm_masjid = await permanent_masjids_collection.find_one( + { + "masjid_id": clean_id, + "is_deleted": {"$ne": True}, + "status": {"$ne": "DELETED"}, + "is_live": {"$ne": False}, + }, + {"_id": 1, "masjid_id": 1, "name": 1, "is_live": 1, "status": 1} + ) + if perm_masjid: + return perm_masjid + + # 3. Popular sanctuary templates (e.g., POPULAR_NABAWI, POPULAR_HARAM, POPULAR_AQSA) + try: + from app.masjid_routes import POPULAR_MASJID_TEMPLATES + clean_key = clean_id.lower().replace("popular_", "") + for tpl in POPULAR_MASJID_TEMPLATES: + if tpl.get("masjid_id") == clean_id or tpl.get("key") == clean_key: + return { + "masjid_id": tpl["masjid_id"], + "name": tpl["name"], + "is_popular_placeholder": True, + } + except Exception: + pass + + return None + + +# ========================================================= +# 2. PYDANTIC SCHEMAS (Bulletproof Input Validation) +# ========================================================= +class AuthRequest(BaseModel): + idToken: str = Field(..., description="Firebase ID Token") + # 🚀 CRITICAL FIX: Removed min_length=2 so empty strings/null from Flutter don't cause 422 Crashes! + username: Optional[str] = None + privacy_accepted: Optional[bool] = None + terms_accepted: Optional[bool] = None + voluntary_disclosure_accepted: Optional[bool] = None + legal_consent_accepted: Optional[bool] = None + platform: Optional[str] = None + +class LegalConsentReq(BaseModel): + privacy_accepted: bool + terms_accepted: bool + voluntary_disclosure_accepted: bool + platform: Optional[str] = Field(None, max_length=32) + +class FollowMasjidRequest(BaseModel): + uid: Optional[str] = None + masjid_id: str = Field(..., min_length=1, max_length=128) + +# 🚀 NEW: Schema for unfollowing a specific masjid +class UnfollowMasjidRequest(BaseModel): + masjid_id: str = Field(..., min_length=1, max_length=128) + +# 🚀 Persistent Selected Masjid +class SelectMasjidRequest(BaseModel): + masjid_id: str = Field(..., max_length=128) + +# 🚀 Multi-device FCM Token Registration +class RegisterFcmTokenRequest(BaseModel): + fcm_token: str = Field(..., min_length=10, max_length=1024) + device_id: Optional[str] = Field(None, max_length=128) + platform: Optional[str] = Field(None, max_length=32) + +class ToggleFavoriteSurahRequest(BaseModel): + surah_no: int + name_english: Optional[str] = "Surah" + name_arabic: Optional[str] = "" + translation_english: Optional[str] = "" + total_ayahs: Optional[int] = 0 + +class ToggleFavoriteAyahRequest(BaseModel): + surah_no: int + ayah_no: int + surah_name: Optional[str] = "" + text_arabic: Optional[str] = "" + text_english: Optional[str] = "" + +class SyncFavoritesRequest(BaseModel): + surahs: List[dict] = Field(default_factory=list) + ayahs: List[dict] = Field(default_factory=list) + removed_surah_nos: List[int] = Field(default_factory=list) + removed_ayah_keys: List[str] = Field(default_factory=list) + +class ChatTitleRequest(BaseModel): + title: str = Field(..., max_length=100) + +class ChatPinRequest(BaseModel): + is_pinned: bool + +class LibrarySearchRequest(BaseModel): + # H1: cap query length so the regex fallback path can't be used + # to build a multi-megabyte ReDoS pattern. + query: str = Field(..., min_length=1, max_length=200) + books: Optional[List[str]] = [] + +# ========================================================= +# 3. INITIALIZE FIREBASE (Infrastructure Service) +# ========================================================= +from app.infrastructure.firebase import init_firebase, verify_firebase_token +init_firebase() + +# ========================================================= +# 5. AUTHENTICATION ROUTES & USER DOCUMENT BUILDER +# ========================================================= + +def _extract_legal_consent_doc( + payload: Optional[AuthRequest], + now: datetime.datetime, + default_accepted: bool = False, +) -> Dict[str, Any]: + """Extracts legal consent strictly from explicit individual acceptance flags. + Never defaults consent to True when omitted or when any required flag is False. + """ + privacy = bool(getattr(payload, "privacy_accepted", None)) + terms = bool(getattr(payload, "terms_accepted", None)) + voluntary = bool(getattr(payload, "voluntary_disclosure_accepted", None)) + explicit_overall = getattr(payload, "legal_consent_accepted", None) + overall = privacy and terms and voluntary and (explicit_overall is not False) + return { + "accepted": overall, + "privacy_accepted": privacy, + "terms_accepted": terms, + "voluntary_disclosure_accepted": voluntary, + "accepted_at": now if overall else None, + "platform": (getattr(payload, "platform", None) or "unknown").strip() if getattr(payload, "platform", None) else "unknown", + "version": "2026-11", + } + + +def build_user_document( + uid: str, + email: str, + username: str, + provider: str = "google", + platform: str = "android", + app_version: str = "1.0.0", + role: str = "USER", + is_email_verified: Optional[bool] = None, + legal_consent: Optional[Dict[str, Any]] = None, +) -> Dict[str, Any]: + """ + Builds an enterprise domain-driven user document for MongoDB. + Structured into profile, auth, community, business, metrics, and analytics + with flat mirrors for zero-breakage backward compatibility. + """ + now = datetime.datetime.utcnow() + display_name = username.strip() if username and username.strip() else (email.split('@')[0] if email else "User") + + return { + "uid": uid, + + # 1. Profile domain + "profile": { + "display_name": display_name, + "email": email, + "phone": None, + "avatar_url": None, + "locale": "en", + "timezone": "Asia/Kolkata", + }, + + # 2. Authentication & Authorization domain + "auth": { + "role": role, + "provider": provider, + "is_email_verified": is_email_verified if is_email_verified is not None else bool(email), + "status": "ACTIVE", + }, + + # 3. Community Affiliations domain + "community": { + "mufti": { + "is_mufti": False, + "status": "NONE", + "applied_at": None, + "resolved_at": None, + }, + "masjid": { + "managed_masjid_id": "", + "status": "NONE", + "co_admin_masjids": [], + "followed_masjids": [], + }, + }, + + # 4. Business Suite domain + "business": { + "is_enabled": False, + "activated_at": None, + "shop_id": None, + "wallet_balance": 0.0, + "total_ads_count": 0, + }, + + # 5. Subscription Domain (Pro & Free tier entitlements) + "subscription": { + "is_pro": False, + "tier": "FREE", + "status": "FREE", + "activated_at": None, + "expires_at": None, + "payment_method": None, + "transaction_id": None, + "daily_ai_queries_used": 0, + "last_query_date": None, + }, + + # 6. Usage & Engagement Metrics (High-speed counters for dashboards) + "metrics": { + "prayers_tracked": 0, + "consecutive_streak": 0, + "total_orders_placed": 0, + "total_spent_inr": 0.0, + "login_count": 1, + "lifetime_apples": 0, + }, + + # 7. Analytics, Telemetry & Audit (For DAU/MAU cohorts and graphs) + "analytics": { + "signup_platform": platform, + "app_version": app_version, + "created_at": now, + "updated_at": now, + "last_active_at": now, + }, + + # 7.5. Spiritual Progress Tracking Domain (Nimaz & Dhikr habit tracker configuration) + "progress_tracking": { + "enabled": False, + "prompt_responded": False, + "started_at": None, + "started_date": None, + }, + + # 8. Backward-compatibility flat mirrors (Guarantees Flutter mobile & web never breaks) + "username": display_name, + "email": email, + "role": role, + "is_pro": False, + "lifetime_apples": 0, + "progress_tracking_enabled": False, + "progress_tracking_prompt_responded": False, + "progress_tracking_started_at": None, + "progress_tracking_started_date": None, + "mufti_status": "NONE", + "masjid_status": "NONE", + "managed_masjid_id": "", + "followed_masjids": [], + "favorites": { + "surahs": [], + "ayahs": [], + }, + "is_business_enabled": False, + "legal_consent": legal_consent if legal_consent is not None else { + "accepted": False, + "privacy_accepted": False, + "terms_accepted": False, + "voluntary_disclosure_accepted": False, + "accepted_at": None, + "platform": platform, + "version": "2026-11", + }, + "created_at": now, + "updated_at": now, + } + + +@router.post('/signup') +@limiter.limit("5/minute") +async def signup(payload: AuthRequest, request: Request): + try: + decoded_token = verify_firebase_token(payload.idToken) + uid = decoded_token['uid'] + email = decoded_token.get('email', '') + + # Safe fallback for username + fallback_name = email.split('@')[0] if email else "User" + display_name = payload.username.strip() if payload.username and payload.username.strip() else fallback_name + + existing_user = await users_collection.find_one({"uid": uid}) + if existing_user: + raise HTTPException(status_code=409, detail="User already exists") + + explicit_flags = [ + payload.privacy_accepted, + payload.terms_accepted, + payload.voluntary_disclosure_accepted, + payload.legal_consent_accepted, + ] + if any(flag is False for flag in explicit_flags): + raise HTTPException( + status_code=400, + detail="You must accept the Privacy Policy, Terms of Service, and Voluntary Open-Access Disclosure.", + ) + + now = datetime.datetime.utcnow() + legal_consent_doc = _extract_legal_consent_doc(payload, now, default_accepted=False) + new_user = build_user_document( + uid=uid, + email=email, + username=display_name, + provider="password", + platform=payload.platform or "android", + legal_consent=legal_consent_doc, + ) + + try: + await users_collection.insert_one(new_user) + except DuplicateKeyError: + logger.warning( + "Signup duplicate-key for uid=%s email=%s — likely race " + "with login auto-sync or a Firebase account switch", + uid, email, + ) + raise HTTPException( + status_code=409, + detail="An account with this email already exists. Please sign in instead.", + ) + + logger.info(f"New user registered: {display_name} ({uid})") + return { + "message": "Account created and synced", + "requires_legal_consent": not bool(legal_consent_doc.get("accepted")), + } + + except firebase_auth.InvalidIdTokenError as e: + logger.error(f"🔥 FIREBASE REJECTED TOKEN (Signup): {str(e)}") + raise HTTPException(status_code=401, detail="Invalid or expired token") + except HTTPException: + raise + except Exception as e: + logger.error(f"Signup Error: {str(e)}") + raise HTTPException(status_code=500, detail="Internal Server Error") + +@router.post('/login') +@limiter.limit("10/minute") +async def login(payload: AuthRequest, request: Request): + try: + decoded_token = verify_firebase_token(payload.idToken) + uid = decoded_token['uid'] + email = decoded_token.get('email', '') + + user = await users_collection.find_one({"uid": uid}) + + if not user: + logger.info(f"Auto-syncing missing Firebase user to MongoDB: {uid}") + fallback_name = email.split('@')[0] if email else "User" + display_name = payload.username.strip() if payload.username and payload.username.strip() else fallback_name + now = datetime.datetime.utcnow() + is_new_login_user = True + new_user = build_user_document( + uid=uid, + email=email, + username=display_name, + provider="google", + platform=payload.platform or "android", + legal_consent=_extract_legal_consent_doc(payload, now, default_accepted=False), + ) + try: + await users_collection.insert_one(new_user) + user = new_user + except DuplicateKeyError: + is_new_login_user = False + logger.warning( + "Login auto-sync duplicate-key for email=%s — " + "fetching pre-existing user", email, + ) + user = await users_collection.find_one({"email": email}) + if user is None: + raise + else: + is_new_login_user = False + # Update telemetry & metrics on returning user login + now = datetime.datetime.utcnow() + await users_collection.update_one( + {"uid": uid}, + { + "$set": {"analytics.last_active_at": now, "updated_at": now}, + "$inc": {"metrics.login_count": 1} + } + ) + + user = normalize_user_masjid_fields(user) + lc = user.get("legal_consent") + requires_consent = isinstance(lc, dict) and lc.get("accepted") is False + return { + "username": user.get('username') or user.get('profile', {}).get('display_name', 'User'), + "role": user.get('role') or user.get('auth', {}).get('role', 'USER'), + "mufti_status": user.get("mufti_status", "NONE"), + "mufti_rejection_reason": user.get("mufti_rejection_reason", ""), + "app_admin_status": user.get("app_admin_status", "NONE"), + "app_admin_rejection_reason": user.get("app_admin_rejection_reason", ""), + "masjid_status": user.get("masjid_status", "NONE"), + "managed_masjid_id": user.get("managed_masjid_id", ""), + "selected_masjid_id": user.get("selected_masjid_id", ""), + "followed_masjids": user.get("followed_masjids", []), + "profile": user.get("profile"), + "auth": user.get("auth"), + "business": user.get("business"), + "subscription": user.get("subscription") or { + "is_pro": user.get("is_pro", False), + "tier": "FREE", + "status": "FREE", + }, + "is_pro": bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")), + "metrics": user.get("metrics"), + "has_acknowledged_rag": bool(user.get("has_acknowledged_rag_theology_disclaimer")), + "is_new_user": is_new_login_user, + "requires_legal_consent": requires_consent, + } + + except HTTPException: + raise + except firebase_auth.InvalidIdTokenError as e: + logger.error(f"🔥 FIREBASE REJECTED TOKEN (Login): {str(e)}") + raise HTTPException(status_code=401, detail="Invalid or expired token") + except Exception as e: + logger.error(f"Login Error: {str(e)}") + raise HTTPException(status_code=500, detail="Internal Server Error") + +async def _run_account_link_migration( + *, + email: str, + old_uid: str, + new_uid: str, + provider: str, + migration_id: str, + now: datetime.datetime, + rollback_user_on_failure: bool = True, +) -> None: + """Journaled & scoped migration of owned collections from old_uid to new_uid.""" + from app.database import ( + wallets_collection, + subscriptions_collection, + transactions_collection, + orders_collection, + checkout_operations_collection, + products_collection, + ads_collection, + chats_collection, + daily_progress_collection, + qaza_collection, + qaza_events_collection, + nimaz_stats_collection, + tasbeeh_collection, + tasbeeh_daily_collection, + tasbeeh_stats_collection, + masjid_collection, + device_tokens_collection, + shops_collection, + shop_settings_collection, + refund_requests_collection, + audit_logs_collection, + ) + + migration_plan = [ + (wallets_collection, "user_id"), + (subscriptions_collection, "user_id"), + (transactions_collection, "user_id"), + (refund_requests_collection, "user_id"), + (orders_collection, "user_id"), + (orders_collection, "buyer_uid"), + (orders_collection, "seller_uid"), + (checkout_operations_collection, "user_id"), + (products_collection, "seller_uid"), + (ads_collection, "advertiser_uid"), + (ads_collection, "user_id"), + (chats_collection, "user_id"), + (daily_progress_collection, "uid"), + (qaza_collection, "uid"), + (qaza_events_collection, "uid"), + (nimaz_stats_collection, "uid"), + (tasbeeh_collection, "uid"), + (tasbeeh_daily_collection, "uid"), + (tasbeeh_stats_collection, "uid"), + (masjid_collection, "admin_uid"), + (device_tokens_collection, "uid"), + (device_tokens_collection, "user_id"), + (shops_collection, "owner_uid"), + (shop_settings_collection, "user_id"), + ] + migrated_steps = [] + migrated_array_steps = [] + migrated_id_docs = [] + try: + for coll, field in migration_plan: + await coll.update_many( + {field: old_uid}, + { + "$set": { + field: new_uid, + "_link_migration_id": migration_id, + "_migrated_from_uid": old_uid, + } + }, + ) + migrated_steps.append((coll, field)) + + # Array-element ownership migrations (multi-seller orders, item sellers, masjid co_admins) + array_plan = [ + ( + orders_collection, + {"sellers": old_uid}, + {"$set": {"sellers.$[s]": new_uid, "_link_migration_id": migration_id, "_migrated_from_uid": old_uid}}, + [{"s": old_uid}], + {"$set": {"sellers.$[s]": old_uid}, "$unset": {"_link_migration_id": "", "_migrated_from_uid": ""}}, + [{"s": new_uid}], + ), + ( + orders_collection, + {"items.seller_uid": old_uid}, + {"$set": {"items.$[it].seller_uid": new_uid, "_link_migration_id": migration_id, "_migrated_from_uid": old_uid}}, + [{"it.seller_uid": old_uid}], + {"$set": {"items.$[it].seller_uid": old_uid}, "$unset": {"_link_migration_id": "", "_migrated_from_uid": ""}}, + [{"it.seller_uid": new_uid}], + ), + ( + masjid_collection, + {"co_admins": old_uid}, + {"$set": {"co_admins.$[c]": new_uid, "_link_migration_id": migration_id, "_migrated_from_uid": old_uid}}, + [{"c": old_uid}], + {"$set": {"co_admins.$[c]": old_uid}, "$unset": {"_link_migration_id": "", "_migrated_from_uid": ""}}, + [{"c": new_uid}], + ), + ] + for coll, fwd_flt, fwd_upd, fwd_af, rb_upd, rb_af in array_plan: + await coll.update_many(fwd_flt, fwd_upd, array_filters=fwd_af) + migrated_array_steps.append((coll, rb_upd, rb_af)) + + # _id-keyed stats documents (nimaz_stats, tasbeeh_stats use uid as _id) + for stats_coll in (nimaz_stats_collection, tasbeeh_stats_collection): + old_stats_doc = await stats_coll.find_one({"_id": old_uid}) + if isinstance(old_stats_doc, dict) and old_stats_doc.get("_id") == old_uid: + new_stats_doc = await stats_coll.find_one({"_id": new_uid}) + migrated_copy = dict(old_stats_doc) + migrated_copy["_id"] = new_uid + migrated_copy["uid"] = new_uid + migrated_copy["_link_migration_id"] = migration_id + migrated_copy["_migrated_from_uid"] = old_uid + if not isinstance(new_stats_doc, dict) or not new_stats_doc: + await stats_coll.update_one( + {"_id": new_uid}, + {"$set": migrated_copy}, + upsert=True, + ) + await stats_coll.delete_one({"_id": old_uid}) + migrated_id_docs.append((stats_coll, dict(old_stats_doc), False)) + else: + # Preserve preexisting new_uid stats document without clobbering + await stats_coll.update_one( + {"_id": old_uid}, + {"$set": {"uid": new_uid, "_link_migration_id": migration_id, "_migrated_from_uid": old_uid}}, + ) + migrated_id_docs.append((stats_coll, dict(old_stats_doc), True)) + + await audit_logs_collection.insert_one({ + "event": "ACCOUNT_LINKED_VIA_SOCIAL_LOGIN", + "email": email, + "old_uid": old_uid, + "new_uid": new_uid, + "migration_id": migration_id, + "provider": provider, + "timestamp": now, + }) + await users_collection.update_one( + {"email": email, "auth.pending_link_migration.migration_id": migration_id}, + {"$unset": {"auth.pending_link_migration": "", "auth.linking_lease_until": ""}}, + ) + except Exception as e: + logger.error(f"[Auth] Critical failure migrating records from {old_uid} to {new_uid}: {e}", exc_info=True) + rollback_failures = [] + for stats_coll, orig_old_doc, had_new_doc in reversed(migrated_id_docs): + try: + if had_new_doc: + await stats_coll.update_one( + {"_id": old_uid, "_link_migration_id": migration_id}, + {"$set": {"uid": old_uid}, "$unset": {"_link_migration_id": "", "_migrated_from_uid": ""}}, + ) + else: + await stats_coll.update_one({"_id": old_uid}, {"$set": orig_old_doc}, upsert=True) + await stats_coll.delete_one({"_id": new_uid, "_link_migration_id": migration_id}) + except Exception as rb_id_err: + logger.critical(f"[Auth] Failed to rollback _id doc on {getattr(stats_coll, 'name', 'collection')}: {rb_id_err}") + rollback_failures.append(f"{getattr(stats_coll, 'name', 'collection')}._id") + for coll, rb_upd, rb_af in reversed(migrated_array_steps): + try: + await coll.update_many( + {"_link_migration_id": migration_id}, + rb_upd, + array_filters=rb_af, + ) + except Exception as rb_arr_err: + logger.critical(f"[Auth] Failed to rollback array step on {getattr(coll, 'name', 'collection')}: {rb_arr_err}") + rollback_failures.append(f"{getattr(coll, 'name', 'collection')}.array") + for coll, field in reversed(migrated_steps): + try: + await coll.update_many( + {field: new_uid, "_link_migration_id": migration_id}, + { + "$set": {field: old_uid}, + "$unset": {"_link_migration_id": "", "_migrated_from_uid": ""}, + }, + ) + except Exception as rb_step_err: + logger.critical(f"[Auth] Failed to rollback step on {getattr(coll, 'name', 'collection')}.{field}: {rb_step_err}") + rollback_failures.append(f"{getattr(coll, 'name', 'collection')}.{field}") + + if rollback_user_on_failure: + try: + if rollback_failures: + await users_collection.update_one( + {"email": email}, + { + "$set": { + "auth.pending_link_migration.status": "ROLLBACK_FAILED", + "auth.pending_link_migration.rollback_failures": rollback_failures, + "auth.linking_lease_until": None, + "updated_at": datetime.datetime.utcnow(), + } + }, + ) + else: + await users_collection.update_one( + {"email": email}, + { + "$set": {"uid": old_uid, "updated_at": datetime.datetime.utcnow()}, + "$unset": {"auth.pending_link_migration": "", "auth.linking_lease_until": ""}, + "$pull": {"auth.linked_uids": old_uid, "linked_uids": old_uid}, + }, + ) + await invalidate_user_cache(old_uid) + await invalidate_user_cache(new_uid) + except Exception as rb_err: + logger.critical(f"[Auth] FATAL: Rollback failed during account linking error: {rb_err}") + + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Account linking failed during ownership records migration. Operation rolled back.", + ) + + +@router.post('/social-login') +@limiter.limit("15/minute") +async def social_login(payload: AuthRequest, request: Request): + try: + decoded_token = verify_firebase_token(payload.idToken) + uid = decoded_token['uid'] + email = (decoded_token.get('email') or '').strip().lower() + name = decoded_token.get('name', '') + is_email_verified = bool(decoded_token.get('email_verified', False)) + provider = decoded_token.get('firebase', {}).get('sign_in_provider', 'google') + now = datetime.datetime.utcnow() + + # 🔗 ACCOUNT LINKING: Check if user exists by UID or EMAIL + user = await users_collection.find_one({"uid": uid}) + + # If no user found by UID, check if email exists (account linking scenario) + if not user and email: + existing_user = await users_collection.find_one({"email": email}) + if existing_user: + # SI-SEC-011: Strict anti-takeover guard + # 1. Reject unverified email claims from social login + if not is_email_verified: + logger.warning(f"[Auth] Account linking rejected for {email}: incoming social identity email is NOT verified") + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="Cannot link account: Social identity email address is not verified by provider. Please verify your email with the identity provider first." + ) + + # 2. Reject automatic linking of administrative or privileged accounts + existing_role = (existing_user.get("role") or existing_user.get("auth", {}).get("role", "USER")).upper() + if existing_role in ("ADMIN", "SUPERADMIN", "STAFF") or existing_user.get("is_admin") or existing_user.get("is_superadmin"): + logger.warning(f"[Auth] Account linking rejected for {email}: administrative account cannot be linked automatically via social login") + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="Administrative accounts cannot be automatically linked via social login. Please sign in using primary administrator credentials." + ) + + # 3. Safe linking with atomic lease claim, foreign-key preservation, and journal (AUTH-001) + old_uid = existing_user.get("uid") + migration_id = f"link_{old_uid}_{uid}_{uuid.uuid4().hex[:10]}" if (old_uid and old_uid != uid) else None + lease_until = now + datetime.timedelta(seconds=60) + logger.info(f"Account Linking: Linking verified {provider} account to existing user {email} (old_uid={old_uid}, new_uid={uid})") + + update_doc = { + "$set": { + "uid": uid, + "auth.is_email_verified": True, + "auth.provider": provider, + "analytics.updated_at": now, + "updated_at": now, + } + } + claim_filter: Dict[str, Any] = { + "email": email, + "$or": [ + {"auth.linking_lease_until": {"$exists": False}}, + {"auth.linking_lease_until": None}, + {"auth.linking_lease_until": {"$lt": now}}, + ], + } + if old_uid and old_uid != uid: + claim_filter["uid"] = old_uid + update_doc["$set"]["auth.linking_lease_until"] = lease_until + update_doc["$set"]["auth.pending_link_migration"] = { + "migration_id": migration_id, + "old_uid": old_uid, + "new_uid": uid, + "status": "MIGRATING", + "started_at": now, + "lease_until": lease_until, + } + update_doc["$addToSet"] = { + "auth.linked_uids": old_uid, + "linked_uids": old_uid, + } + + claim_res = await users_collection.update_one(claim_filter, update_doc) + claim_mc = getattr(claim_res, "matched_count", None) + if isinstance(claim_mc, (int, float)) and not isinstance(claim_mc, bool) and claim_mc == 0: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Account linking is already in progress. Please retry shortly.", + ) + + # Migrate related records if old_uid exists and differs (NEW-012, SI-SEC-011, AUTH-001) + if old_uid and old_uid != uid: + await _run_account_link_migration( + email=email, + old_uid=old_uid, + new_uid=uid, + provider=provider, + migration_id=migration_id, + now=now, + rollback_user_on_failure=True, + ) + await invalidate_user_cache(old_uid) + + await invalidate_user_cache(uid) + user = await users_collection.find_one({"email": email}) + logger.info(f"Account Linked: {email} now accessible via social login") + + is_new_user = False + if not user: + logger.info(f"Social Login Sync: Creating new user {uid} in MongoDB") + fallback_name = email.split('@')[0] if email else "User" + + if payload.username and payload.username.strip(): + display_name = payload.username.strip() + elif name and name.strip(): + display_name = name.strip() + else: + display_name = fallback_name + + legal_consent_doc = _extract_legal_consent_doc(payload, now, default_accepted=False) + new_user = build_user_document( + uid=uid, + email=email, + username=display_name, + provider=provider, + platform=payload.platform or "android", + is_email_verified=is_email_verified, + legal_consent=legal_consent_doc, + ) + try: + await users_collection.insert_one(new_user) + user = new_user + is_new_user = True + except DuplicateKeyError: + logger.warning( + "Social login auto-sync duplicate-key for email=%s — " + "fetching pre-existing user", email, + ) + user = await users_collection.find_one({"email": email}) + if user is None: + raise + else: + # AUTH-001: If a prior account-link migration crashed after flipping user.uid, claim lease & finish it + pending_mig = ((user.get("auth") or {}).get("pending_link_migration") if isinstance(user, dict) else None) + if isinstance(pending_mig, dict) and pending_mig.get("old_uid") and pending_mig.get("migration_id"): + old_uid = pending_mig["old_uid"] + mig_id = pending_mig["migration_id"] + if old_uid != uid: + resume_lease_until = now + datetime.timedelta(seconds=60) + resume_claim = await users_collection.update_one( + { + "uid": uid, + "auth.pending_link_migration.migration_id": mig_id, + "$or": [ + {"auth.linking_lease_until": {"$exists": False}}, + {"auth.linking_lease_until": None}, + {"auth.linking_lease_until": {"$lt": now}}, + ], + }, + { + "$set": { + "auth.linking_lease_until": resume_lease_until, + "auth.pending_link_migration.lease_until": resume_lease_until, + } + }, + ) + resume_mc = getattr(resume_claim, "matched_count", None) + if isinstance(resume_mc, (int, float)) and not isinstance(resume_mc, bool) and resume_mc == 0: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Account linking recovery is already in progress. Please retry shortly.", + ) + try: + await _run_account_link_migration( + email=(user.get("email") or email), + old_uid=old_uid, + new_uid=uid, + provider=provider, + migration_id=mig_id, + now=now, + rollback_user_on_failure=False, + ) + except Exception: + try: + await users_collection.update_one( + {"uid": uid, "auth.pending_link_migration.migration_id": mig_id}, + {"$set": {"auth.linking_lease_until": None, "auth.pending_link_migration.lease_until": None}}, + ) + except Exception: + pass + raise + await invalidate_user_cache(old_uid) + await invalidate_user_cache(uid) + user.get("auth", {}).pop("pending_link_migration", None) + + # Update telemetry & metrics on returning user login + update_fields = {"analytics.last_active_at": now, "updated_at": now} + incoming_consent = _extract_legal_consent_doc(payload, now, default_accepted=False) + if incoming_consent.get("accepted"): + update_fields["legal_consent"] = incoming_consent + user["legal_consent"] = incoming_consent + await users_collection.update_one( + {"uid": uid}, + { + "$set": update_fields, + "$inc": {"metrics.login_count": 1} + } + ) + + user = normalize_user_masjid_fields(user) + lc = user.get("legal_consent") + requires_consent = bool(is_new_user and not (isinstance(lc, dict) and lc.get("accepted"))) or ( + isinstance(lc, dict) and lc.get("accepted") is False + ) + return { + "username": user.get('username') or user.get('profile', {}).get('display_name', 'User'), + "role": user.get('role') or user.get('auth', {}).get('role', 'USER'), + "mufti_status": user.get("mufti_status", "NONE"), + "mufti_rejection_reason": user.get("mufti_rejection_reason", ""), + "app_admin_status": user.get("app_admin_status", "NONE"), + "app_admin_rejection_reason": user.get("app_admin_rejection_reason", ""), + "masjid_status": user.get("masjid_status", "NONE"), + "managed_masjid_id": user.get("managed_masjid_id", ""), + "selected_masjid_id": user.get("selected_masjid_id", ""), + "followed_masjids": user.get("followed_masjids", []), + "profile": user.get("profile"), + "auth": user.get("auth"), + "business": user.get("business"), + "subscription": user.get("subscription") or { + "is_pro": user.get("is_pro", False), + "tier": "FREE", + "status": "FREE", + }, + "is_pro": bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")), + "metrics": user.get("metrics"), + "has_acknowledged_rag": bool(user.get("has_acknowledged_rag_theology_disclaimer")), + "is_new_user": is_new_user, + "requires_legal_consent": requires_consent, + } + + except HTTPException: + raise + except firebase_auth.InvalidIdTokenError as e: + logger.error(f"🔥 FIREBASE REJECTED TOKEN (Social): {str(e)}") + raise HTTPException(status_code=401, detail="Invalid or expired token") + except Exception as e: + logger.error(f"Social Login Error: {str(e)}") + raise HTTPException(status_code=500, detail="Internal Server Error") + + +@router.post('/legal-consent/accept') +@limiter.limit("30/minute") +async def accept_legal_consent( + payload: LegalConsentReq, + request: Request, + current_user: dict = Depends(get_current_user), +): + if not (payload.privacy_accepted and payload.terms_accepted and payload.voluntary_disclosure_accepted): + raise HTTPException( + status_code=400, + detail="You must accept the Privacy Policy, Terms of Service, and Voluntary Open-Access Disclosure.", + ) + uid = current_user["uid"] + now = datetime.datetime.utcnow() + consent_doc = { + "accepted": True, + "privacy_accepted": True, + "terms_accepted": True, + "voluntary_disclosure_accepted": True, + "accepted_at": now, + "platform": (payload.platform or "unknown").strip(), + "version": "2026-11", + } + res = await users_collection.update_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"$set": {"legal_consent": consent_doc, "updated_at": now}}, + ) + mc = getattr(res, "matched_count", None) + if isinstance(mc, (int, float)) and not isinstance(mc, bool) and mc == 0: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="User record not found; cannot record legal consent.", + ) + await invalidate_user_cache(uid) + return {"status": "ok", "accepted": True} + +_PROFILE_PRIVATE_FIELDS = ( + "password", + "password_hash", + "device_tokens", + "fcm_tokens", + "fcm_token", + "apns_token", + "deletion_op_token", + "otp_code", + "otp_expires_at", + "verification_token", +) + + +def _sanitize_profile_response(user: dict, *, caller_uid: str = "", target_uid: str = "") -> dict: + """Strip internal secrets, raw push/purchase tokens, and password hashes from profile payloads.""" + cleaned = dict(user) + for field in _PROFILE_PRIVATE_FIELDS: + cleaned.pop(field, None) + + if isinstance(cleaned.get("subscription"), dict): + sub = dict(cleaned["subscription"]) + sub.pop("purchase_token", None) + cleaned["subscription"] = sub + + if isinstance(cleaned.get("auth"), dict): + auth_obj = dict(cleaned["auth"]) + for af in ("password_hash", "superadmin", "is_superadmin", "pending_link_migration", "linking_lease_until"): + auth_obj.pop(af, None) + cleaned["auth"] = auth_obj + + if caller_uid != target_uid: + for admin_hidden in ("stripe_customer_id", "razorpay_customer_id", "payment_methods"): + cleaned.pop(admin_hidden, None) + return cleaned + + +def _resolve_progress_tracking_state_readonly(user_doc: dict) -> dict: + """ + Side-effect-free resolver for progress tracking state (PX-16). + Strictly honors explicit user deferral/disablement (prompt_responded=True, enabled=False). + """ + pt = user_doc.get("progress_tracking") or {} + responded = user_doc.get("progress_tracking_prompt_responded", pt.get("prompt_responded")) + enabled = user_doc.get("progress_tracking_enabled", pt.get("enabled")) + started_at = user_doc.get("progress_tracking_started_at", pt.get("started_at")) + started_date = user_doc.get("progress_tracking_started_date", pt.get("started_date")) + + explicitly_deferred = (responded is True and enabled is False) + if explicitly_deferred: + return { + "enabled": False, + "prompt_responded": True, + "started_at": str(started_at) if started_at else None, + "started_date": str(started_date) if started_date else None, + } + + is_enabled = bool(enabled) + if responded is None: + responded = is_enabled or bool(started_date) + return { + "enabled": is_enabled, + "prompt_responded": bool(responded), + "started_at": str(started_at) if started_at else None, + "started_date": str(started_date) if started_date else None, + } + + +@router.get('/profile/{uid}') +async def get_profile(uid: str, current_user: dict = Depends(get_current_user)): + # 🔒 IDOR FIX (audit C1): authenticate is not enough — a logged-in + # user must only read THEIR OWN profile (or an admin may read any). + # Without this, any user could read another user's email/role/masjid + # associations by supplying a different uid. + enforce_self_or_admin(current_user, uid) + user = await users_collection.find_one({"uid": uid}, {"_id": 0}) + if user: + user = normalize_user_masjid_fields(user) + user["is_pro"] = bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")) + user["has_password"] = bool(user.get("has_password")) + user["deleted_notification_ids"] = user.get("deleted_notification_ids") or [] + user["has_acknowledged_rag"] = bool(user.get("has_acknowledged_rag_theology_disclaimer")) + + # SuperAdmin status is code-governed via immutable whitelist (NEVER read from DB) + from .superadmin_routes import get_superadmin_whitelist + user_email = (user.get("email") or "").strip().lower() + user["superadmin"] = user_email in get_superadmin_whitelist() + user.pop("is_superadmin", None) + if "auth" in user and isinstance(user["auth"], dict): + user["auth"].pop("superadmin", None) + user["auth"].pop("is_superadmin", None) + + # Look up co-admin masjids dynamically so role changes take effect in real-time + co_masjids = await masjid_collection.find({"co_admins": uid}, {"masjid_id": 1, "name": 1, "_id": 0}).to_list(length=20) + user["co_admin_masjid_ids"] = [m["masjid_id"] for m in co_masjids] + user["is_co_admin"] = len(co_masjids) > 0 + + # Read-only progress tracking configuration & consent state (PX-16: no DB mutation on GET) + resolved_pt = _resolve_progress_tracking_state_readonly(user) + user["progress_tracking_enabled"] = resolved_pt["enabled"] + user["progress_tracking_prompt_responded"] = resolved_pt["prompt_responded"] + user["progress_tracking_started_at"] = resolved_pt["started_at"] + user["progress_tracking_started_date"] = resolved_pt["started_date"] + + return _sanitize_profile_response(user, caller_uid=current_user.get("uid", ""), target_uid=uid) + raise HTTPException(status_code=404, detail="User not found") + +class UpdateProfileRequest(BaseModel): + username: Optional[str] = Field(None, min_length=1, max_length=100) + phone: Optional[str] = Field(None, max_length=25) + location: Optional[str] = Field(None, max_length=100) + bio: Optional[str] = Field(None, max_length=300) + email: Optional[str] = Field(None, max_length=150) + +@router.put('/profile/{uid}') +async def update_profile( + uid: str, + payload: UpdateProfileRequest, + current_user: dict = Depends(get_current_user), +): + enforce_self_or_admin(current_user, uid) + + target_user = await users_collection.find_one({"uid": uid}) + if not target_user: + raise HTTPException(status_code=404, detail="User not found") + + target_email = (target_user.get("email") or target_user.get("primary_email") or "").strip().lower() + + update_fields = {} + if payload.username is not None: + name = payload.username.strip() + if name: + update_fields["username"] = name + update_fields["profile.display_name"] = name + if payload.phone is not None: + update_fields["phone"] = payload.phone.strip() + update_fields["profile.phone"] = payload.phone.strip() + if payload.location is not None: + update_fields["location"] = payload.location.strip() + update_fields["profile.location"] = payload.location.strip() + if payload.bio is not None: + update_fields["bio"] = payload.bio.strip() + update_fields["profile.bio"] = payload.bio.strip() + + if payload.email is not None: + em = payload.email.strip().lower() + if em and em != target_email: + raise HTTPException( + status_code=400, + detail="Direct email modification via profile update is disabled. Please use the verified email change flow (/auth/request-email-change and /auth/verify-email-change)." + ) + elif not em: + raise HTTPException( + status_code=400, + detail="Email cannot be emptied via profile update." + ) + # If em == target_email, it's an identical no-op, safe to proceed without modifying email + + if update_fields: + now = datetime.datetime.utcnow() + update_fields["updated_at"] = now + update_fields["analytics.updated_at"] = now + await users_collection.update_one({"uid": uid}, {"$set": update_fields}) + await invalidate_user_cache(uid) + + user = await users_collection.find_one({"uid": uid}, {"_id": 0}) + if user: + user = normalize_user_masjid_fields(user) + user["is_pro"] = bool(user.get("is_pro") or user.get("subscription", {}).get("is_pro")) + sanitized = _sanitize_profile_response(user, caller_uid=current_user.get("uid", ""), target_uid=uid) + return {"status": "success", "user": sanitized} + raise HTTPException(status_code=404, detail="User not found") + +# ========================================================= +# 6. ADMIN ROUTES — Pending Request Listing Only +def haversine_km(lat1, lon1, lat2, lon2) -> float: + try: + import math + r = 6371.0 + dlat = math.radians(float(lat2) - float(lat1)) + dlon = math.radians(float(lon2) - float(lon1)) + a = (math.sin(dlat / 2.0) ** 2 + + math.cos(math.radians(float(lat1))) * math.cos(math.radians(float(lat2))) * math.sin(dlon / 2.0) ** 2) + c = 2.0 * math.atan2(math.sqrt(a), math.sqrt(1.0 - a)) + return r * c + except Exception: + return 999999.0 + +class AdminLocationUpdate(BaseModel): + lat: float + lng: float + +@router.post('/admin/update-location') +async def update_admin_location(payload: AdminLocationUpdate, admin: dict = Depends(require_admin)): + """Keep App Admin's GPS location live so nearest masjid registrations & reports can be routed to them.""" + now = datetime.datetime.utcnow().isoformat() + await users_collection.update_one( + {"uid": admin["uid"]}, + {"$set": { + "last_location": { + "lat": payload.lat, + "lng": payload.lng, + "updated_at": now, + } + }} + ) + return {"status": "success", "lat": payload.lat, "lng": payload.lng} + +class AppAdminRegistration(BaseModel): + userId: Optional[str] = Field(default=None, max_length=256) + full_name: str = Field(..., min_length=2, max_length=120) + dob: str = Field(..., min_length=10, max_length=10) + fathers_name: str = Field(..., min_length=2, max_length=120) + phone: str = Field(..., min_length=8, max_length=30) + country_state: str = Field(..., min_length=2, max_length=120) + city_district: str = Field(..., min_length=2, max_length=120) + town_village: str = Field(..., min_length=2, max_length=120) + govt_id_image_base64: str = Field(..., min_length=10) + govt_id_image_name: Optional[str] = Field(default="govt_id.jpg", max_length=180) + # Optional legacy aliases synced from primary fields + name: Optional[str] = Field(default=None, max_length=120) + district: Optional[str] = Field(default=None, max_length=120) + city: Optional[str] = Field(default=None, max_length=120) + + @field_validator("full_name", "fathers_name", "country_state", "city_district", "town_village") + @classmethod + def _strip_required_text(cls, v: str) -> str: + cleaned = v.strip() + if len(cleaned) < 2: + raise ValueError("Field must contain at least 2 characters") + return cleaned + + @field_validator("dob") + @classmethod + def _check_dob(cls, v: str) -> str: + return validate_dob(v) + + @field_validator("phone") + @classmethod + def _check_phone(cls, v: str) -> str: + return validate_phone_with_country_code(v) + + @model_validator(mode="after") + def _normalize_id_and_aliases(self): + normalized_img, _ = decode_and_validate_image_data_uri(self.govt_id_image_base64) + self.govt_id_image_base64 = normalized_img + self.name = self.full_name + self.district = self.city_district + self.city = self.town_village + return self + +@router.post('/admin/register') +@limiter.limit("60/minute") +async def register_app_admin( + payload: AppAdminRegistration, + request: Request, + user: dict = Depends(get_current_user), +): + """Submits an application to become an App Admin. + + Auth-gated: verified uid is used. Upserts to pending_approvals_collection + and updates user app_admin_status to PENDING. + """ + try: + verified_uid = user["uid"] + current_role = str(user.get("role") or (user.get("auth") or {}).get("role") or "USER").strip().upper() + if current_role in ("ADMIN", "APP_ADMIN"): + raise HTTPException( + status_code=400, + detail="Your account already has App Admin privileges", + ) + + admin_data = payload.model_dump() + admin_data.pop("userId", None) + _, img_bytes_len = decode_and_validate_image_data_uri(payload.govt_id_image_base64) + admin_data.update({ + "uid": verified_uid, + "name": payload.full_name, + "district": payload.city_district, + "city": payload.town_village, + "govt_id_image_name": (payload.govt_id_image_name or "govt_id.jpg").strip() or "govt_id.jpg", + "govt_id_image_size_bytes": img_bytes_len, + "role": "APP_ADMIN", + "status": "PENDING", + "timestamp": datetime.datetime.utcnow(), + "email": user.get("email") or "", + "username": user.get("username") or payload.full_name, + }) + await asyncio.gather( + pending_approvals_collection.update_one( + {"uid": verified_uid, "role": "APP_ADMIN"}, + {"$set": admin_data}, + upsert=True + ), + users_collection.update_one( + {"uid": verified_uid}, + { + "$set": {"app_admin_status": "PENDING"}, + "$unset": {"app_admin_rejection_reason": ""}, + }, + ), + ) + await invalidate_user_cache(verified_uid) + logger.info(f"New App Admin application from: {verified_uid}") + try: + from .superadmin_routes import get_superadmin_whitelist_regexes + sa_uids = [ + u["uid"] + async for u in users_collection.find( + {"email": {"$in": get_superadmin_whitelist_regexes()}}, + {"uid": 1, "_id": 0}, + ) + if u.get("uid") + ] + if sa_uids: + realtime.publish_bulk( + sa_uids, + "new_pending_request", + {"role": "APP_ADMIN", "uid": verified_uid}, + ) + except Exception as pub_e: + logger.warning("realtime: publish APP_ADMIN pending request failed: %s", pub_e) + return JSONResponse(status_code=201, content={"message": "App Admin application submitted successfully"}) + except HTTPException: + raise + except Exception as e: + logger.error(f"App Admin Registration Error: {e}") + raise HTTPException(status_code=500, detail="Internal Server Error") + +# ========================================================= +@router.get('/admin/pending-requests') +async def get_pending_requests(admin: dict = Depends(require_admin)): + try: + # App Admin applications belong strictly to the SuperAdmin portal, never regular AppAdminDashboard. + apps = await pending_approvals_collection.find( + {"role": {"$in": ["MASJID", "MUFTI"]}}, + {"_id": 0}, + ).to_list(length=100) + # Batch the user lookups into ONE query instead of one-per-app (N+1). + uids = list({a.get('uid') for a in apps if a.get('uid')}) + users = await users_collection.find( + {"uid": {"$in": uids}}, {"_id": 0, "uid": 1, "username": 1, "email": 1, "phone": 1, "profile.phone": 1} + ).to_list(length=len(uids) or 1) if uids else [] + by_uid = {u["uid"]: u for u in users} + results = [] + + admin_loc = admin.get("last_location") or {} + a_lat = admin_loc.get("lat") + a_lng = admin_loc.get("lng") + + for app in apps: + user = by_uid.get(app.get('uid')) + if user: + app.update({ + "username": user.get('username'), + "email": user.get('email'), + "user_phone": user.get('phone') or (user.get('profile') or {}).get('phone') or app.get('phone') or app.get('contact_phone'), + }) + + # Compute distance to this App Admin if both coordinates exist + m_lat = app.get("latitude") if app.get("latitude") is not None else app.get("gps_lat") + m_lng = app.get("longitude") if app.get("longitude") is not None else app.get("gps_lng") + if a_lat is not None and a_lng is not None and m_lat is not None and m_lng is not None: + try: + d_km = haversine_km(float(a_lat), float(a_lng), float(m_lat), float(m_lng)) + app["distance_km"] = round(d_km, 2) + except Exception: + pass + + # Proximity check for duplicate masjids (within 400m) + if app.get("role") == "MASJID" and m_lat is not None and m_lng is not None: + try: + d_deg = 0.005 # ~500m bounding box + nearby_cursor = masjid_collection.find( + { + "latitude": {"$gte": float(m_lat) - d_deg, "$lte": float(m_lat) + d_deg}, + "longitude": {"$gte": float(m_lng) - d_deg, "$lte": float(m_lng) + d_deg}, + }, + {"name": 1, "latitude": 1, "longitude": 1, "masjid_id": 1, "_id": 0} + ) + nearby_masjids = await nearby_cursor.to_list(length=5) + closest_alert = None + for nm in nearby_masjids: + n_lat = nm.get("latitude") + n_lng = nm.get("longitude") + if n_lat is not None and n_lng is not None: + dist_m = haversine_km(float(m_lat), float(m_lng), float(n_lat), float(n_lng)) * 1000 + if dist_m <= 400: # within 400 meters + if closest_alert is None or dist_m < closest_alert["distance_m"]: + closest_alert = { + "name": nm.get("name") or "Unnamed Masjid", + "distance_m": int(dist_m), + "masjid_id": nm.get("masjid_id") + } + if closest_alert: + app["nearby_alert"] = closest_alert + except Exception as prox_e: + logger.debug("Proximity radar check skipped: %s", prox_e) + + results.append(summarize_application_for_list(app)) + + # Sort: applications assigned to this admin first, then by proximity + admin_uid = admin.get("uid") + results.sort(key=lambda r: ( + 0 if r.get("assigned_admin_uid") == admin_uid else 1, + r.get("distance_km") if r.get("distance_km") is not None else 999999.0 + )) + + return results + except Exception as e: + logger.error(f"Admin Pending Requests Error: {str(e)}") + raise HTTPException(status_code=500, detail="Internal Server Error") + +# ========================================================= +# 6b. ADMIN ROLE MANAGEMENT (promote / demote / list) +# ========================================================= +PROMOTABLE_ROLES = {"ADMIN", "MUFTI", "MASJID_ADMIN"} + +class AdminPromoteRequest(BaseModel): + target_uid: str = Field(..., min_length=1, max_length=256) + role: str = Field(..., min_length=1, max_length=32) + +@router.get('/admin/users') +async def list_users(_: dict = Depends(require_superadmin)): + try: + users = await users_collection.find( + {}, {"_id": 0, "uid": 1, "username": 1, "email": 1, "role": 1} + ).sort("username", 1).to_list(length=500) + for u in users: + u["role"] = u.get("role") or "USER" + return users + except Exception as e: + logger.error(f"Admin list users error: {str(e)}") + raise HTTPException(status_code=500, detail="Internal Server Error") + +@router.post('/admin/promote') +async def promote_user( + payload: AdminPromoteRequest, + admin: dict = Depends(require_superadmin), +): + from app.database import audit_logs_collection + + if payload.role not in PROMOTABLE_ROLES: + raise HTTPException( + status_code=400, + detail=f"role must be one of {sorted(PROMOTABLE_ROLES)}", + ) + if payload.target_uid == admin.get("uid"): + raise HTTPException( + status_code=403, + detail="Self-role modification is forbidden.", + ) + target = await users_collection.find_one({"uid": payload.target_uid}) + if not target: + raise HTTPException(status_code=404, detail="Target user not found") + if is_superadmin_user(target): + raise HTTPException( + status_code=403, + detail="Immutable SuperAdmin accounts cannot be modified via role management.", + ) + await users_collection.update_one( + {"uid": payload.target_uid}, + {"$set": {"role": payload.role, "updated_at": datetime.datetime.utcnow()}}, + ) + await invalidate_user_cache(payload.target_uid) + await audit_logs_collection.insert_one({ + "event": "ADMIN_ROLE_PROMOTED", + "actor_uid": admin.get("uid"), + "target_uid": payload.target_uid, + "new_role": payload.role, + "timestamp": datetime.datetime.utcnow(), + }) + logger.info( + "auth: superadmin=%s promoted uid=%s to role=%s", + admin.get("uid"), payload.target_uid, payload.role, + ) + realtime.publish( + payload.target_uid, + "role_changed", + {"new_role": payload.role}, + ) + return { + "status": "ok", + "target_uid": payload.target_uid, + "new_role": payload.role, + } + +@router.post('/admin/demote') +async def demote_user( + payload: AdminPromoteRequest, + admin: dict = Depends(require_superadmin), +): + from app.database import audit_logs_collection + + if payload.role != "USER": + raise HTTPException( + status_code=400, + detail="demote can only set role to 'USER'", + ) + if payload.target_uid == admin.get("uid"): + raise HTTPException( + status_code=403, + detail="Self-role modification is forbidden.", + ) + target = await users_collection.find_one({"uid": payload.target_uid}) + if not target: + raise HTTPException(status_code=404, detail="Target user not found") + if is_superadmin_user(target): + raise HTTPException( + status_code=403, + detail="Immutable SuperAdmin accounts cannot be modified via role management.", + ) + await users_collection.update_one( + {"uid": payload.target_uid}, + {"$set": {"role": "USER", "updated_at": datetime.datetime.utcnow()}}, + ) + await invalidate_user_cache(payload.target_uid) + await audit_logs_collection.insert_one({ + "event": "ADMIN_ROLE_DEMOTED", + "actor_uid": admin.get("uid"), + "target_uid": payload.target_uid, + "new_role": "USER", + "timestamp": datetime.datetime.utcnow(), + }) + logger.info( + "auth: superadmin=%s demoted uid=%s to USER", + admin.get("uid"), payload.target_uid, + ) + realtime.publish( + payload.target_uid, + "role_changed", + {"new_role": "USER"}, + ) + return {"status": "ok", "target_uid": payload.target_uid, "new_role": "USER"} + +@router.post('/admin/bootstrap') +async def bootstrap_admin( + admin: dict = Depends(get_current_user), + x_staff_secret_key: Optional[str] = Header(None, alias="X-Staff-Secret-Key"), +): + """ + Controlled one-time bootstrap endpoint to initialize the first ADMIN. + Guarded against default secret abuse (SI-SEC-005) and enforces caller binding. + Requires constant-time validation of X-Staff-Secret-Key. + Guarantees concurrency-safe atomicity via a singleton database lock. + """ + # 0. Safety guards against predictable default secrets and unauthorized bootstrap + effective_secret = settings.SECRET_KEY or settings.security.secret_key + if not effective_secret or effective_secret == Settings.DEFAULT_DEV_SECRET: + raise HTTPException( + status_code=403, + detail="Forbidden: Admin bootstrap is disabled when using default development secret.", + ) + + if settings.IS_PROD and os.getenv("ALLOW_ADMIN_BOOTSTRAP") != "1": + raise HTTPException( + status_code=403, + detail="Forbidden: Public admin bootstrap is disabled in production.", + ) + + # 1. Require staff secret key (constant time compare against SECRET_KEY or STAFF_SECRET_KEY) + staff_key = settings.security.staff_secret_key or effective_secret + if not x_staff_secret_key or not ( + hmac.compare_digest(x_staff_secret_key.strip(), effective_secret) + or hmac.compare_digest(x_staff_secret_key.strip(), staff_key) + ): + raise HTTPException( + status_code=403, + detail="Forbidden: Valid X-Staff-Secret-Key header is required to bootstrap an administrator." + ) + + # 1b. Identity binding: If BOOTSTRAP_ADMIN_UID is configured, ensure caller matches or is in superadmin email whitelist + bootstrap_uid = settings.security.bootstrap_admin_uid or os.getenv("BOOTSTRAP_ADMIN_UID", "").strip() + caller_email = (admin.get("email") or admin.get("_verified_token_email") or "").strip().lower() + if bootstrap_uid and admin.get("uid") != bootstrap_uid and not is_superadmin_email(caller_email): + raise HTTPException( + status_code=403, + detail="Forbidden: Caller UID does not match configured BOOTSTRAP_ADMIN_UID.", + ) + + # 2. Check if an admin already exists in the system + existing_admin = await users_collection.find_one({"role": "ADMIN"}) + if existing_admin: + raise HTTPException( + status_code=409, + detail="An administrator already exists. Further admin accounts must be promoted by an existing administrator.", + ) + + # 3. Concurrency guard: Atomically acquire singleton lock in DB + try: + await admin_bootstrap_lock_collection.insert_one({ + "_id": "admin_singleton_lock", + "bootstrapped_by_uid": admin["uid"], + "created_at": datetime.datetime.utcnow() + }) + except DuplicateKeyError: + raise HTTPException( + status_code=409, + detail="Bootstrap lock already held or concurrent bootstrap attempt. An admin already exists.", + ) + + # 4. Promote user to ADMIN atomically + now = datetime.datetime.utcnow() + await users_collection.update_one( + {"uid": admin["uid"]}, + {"$set": {"role": "ADMIN", "is_admin": True, "updated_at": now}}, + ) + await invalidate_user_cache(admin["uid"]) + + # 5. Audit logging + try: + from app.database import audit_logs_collection + await audit_logs_collection.insert_one({ + "action": "ADMIN_BOOTSTRAP", + "bootstrapped_uid": admin["uid"], + "bootstrapped_email": caller_email, + "timestamp": now, + }) + except Exception as e: + logger.warning(f"Failed to record admin bootstrap audit log: {e}") + + logger.info("auth: successfully bootstrapped first ADMIN: uid=%s", admin["uid"]) + return {"status": "ok", "new_role": "ADMIN"} + +# ========================================================= +# 6c. REAL-TIME EVENT STREAM (Server-Sent Events) +# ========================================================= +@router.get('/events') +async def sse_events(request: Request, user: dict = Depends(get_current_user)): + if request.method == "OPTIONS": + return JSONResponse(status_code=200, content={}) + + uid = user["uid"] + last_event_id = (request.headers.get("last-event-id") or "").strip() or None + logger.info("realtime: SSE connection opened uid=%s last_event_id=%s", uid, last_event_id) + + async def event_source(): + try: + async for chunk in realtime.sse_event_stream(uid, last_event_id=last_event_id): + yield chunk + except (GeneratorExit, asyncio.CancelledError): + logger.info("realtime: SSE connection closed uid=%s", uid) + return + except Exception as e: + logger.error("realtime: SSE stream error uid=%s: %s", uid, e) + return + + return StreamingResponse( + event_source(), + media_type="text/event-stream", + headers={ + "Cache-Control": "no-cache, no-transform", + "Connection": "keep-alive", + "X-Accel-Buffering": "no", + }, + ) + +# ========================================================= +# 7. CHAT HISTORY & USER ACTIONS +# ========================================================= +@router.post('/follow-masjid') +async def follow_masjid(payload: FollowMasjidRequest, user: dict = Depends(get_current_user)): + """Follows a masjid without altering the user's active selected masjid.""" + target_uid = user["uid"] + now = datetime.datetime.utcnow() + clean_id = (payload.masjid_id or "").strip() + + masjid = await find_selectable_masjid(clean_id) + if not masjid: + raise HTTPException(status_code=404, detail="Masjid not found or inactive") + + # IMPORTANT (Part 3): Following a masjid adds to followed membership, + # but does NOT overwrite the user's active selected_masjid_id. + res = await users_collection.update_one( + {"uid": target_uid}, + { + "$addToSet": { + "followed_masjids": clean_id, + "community.masjid.followed_masjids": clean_id, + }, + "$set": { + "analytics.updated_at": now, + } + } + ) + if res.matched_count == 0: + raise HTTPException(status_code=404, detail="User not found") + + if res.modified_count > 0: + await masjid_collection.update_one( + {"masjid_id": clean_id}, + {"$inc": {"metrics.followers_count": 1}} + ) + await invalidate_user_cache(target_uid) + return {"status": "success", "followed_masjid_id": clean_id} + + +@router.post('/select-masjid') +async def select_masjid(payload: SelectMasjidRequest, user: dict = Depends(get_current_user)): + """Authoritative server-side persistence of user's active/selected masjid.""" + target_uid = user["uid"] + now = datetime.datetime.utcnow() + clean_id = (payload.masjid_id or "").strip() + + if clean_id: + masjid = await find_selectable_masjid(clean_id) + if not masjid: + raise HTTPException(status_code=404, detail="Masjid not found or inactive") + res = await users_collection.update_one( + {"uid": target_uid}, + { + "$set": { + "selected_masjid_id": clean_id, + "community.masjid.selected_masjid_id": clean_id, + "analytics.updated_at": now, + }, + "$addToSet": { + "followed_masjids": clean_id, + "community.masjid.followed_masjids": clean_id, + } + } + ) + if res.matched_count == 0: + raise HTTPException(status_code=404, detail="User not found") + await invalidate_user_cache(target_uid) + return {"status": "success", "selected_masjid_id": clean_id, "name": masjid.get("name", "")} + else: + # Explicitly unlink / clear active masjid selection + res = await users_collection.update_one( + {"uid": target_uid}, + { + "$set": { + "selected_masjid_id": "", + "community.masjid.selected_masjid_id": "", + "analytics.updated_at": now, + } + } + ) + if res.matched_count == 0: + raise HTTPException(status_code=404, detail="User not found") + await invalidate_user_cache(target_uid) + return {"status": "success", "selected_masjid_id": ""} + + +@router.post('/unfollow-masjid') +async def unfollow_masjid(payload: UnfollowMasjidRequest, user: dict = Depends(get_current_user)): + """Unfollows a masjid. If the unfollowed masjid was the active selected masjid at write time, + atomically clears the selection without overwriting any concurrent newer selection.""" + target_uid = user["uid"] + now = datetime.datetime.utcnow() + clean_id = (payload.masjid_id or "").strip() + + # Step 1: Always pull from followed_masjids in both root and nested fields + pull_res = await users_collection.update_one( + {"uid": target_uid}, + { + "$pull": { + "followed_masjids": clean_id, + "community.masjid.followed_masjids": clean_id, + }, + "$set": {"analytics.updated_at": now} + } + ) + if pull_res.matched_count == 0: + raise HTTPException(status_code=404, detail="User not found") + + # Step 2: Fix unfollow race condition (Part 7) + # Clear selected_masjid_id ONLY IF it still matches clean_id at write time + await users_collection.update_one( + { + "uid": target_uid, + "$or": [ + {"selected_masjid_id": clean_id}, + {"community.masjid.selected_masjid_id": clean_id}, + ] + }, + { + "$set": { + "selected_masjid_id": "", + "community.masjid.selected_masjid_id": "", + "analytics.updated_at": now, + } + } + ) + + if pull_res.modified_count > 0: + await masjid_collection.update_one( + {"masjid_id": clean_id}, + {"$inc": {"metrics.followers_count": -1}} + ) + await invalidate_user_cache(target_uid) + return {"status": "success"} + + +@router.post('/fcm-token') +async def register_fcm_token(payload: RegisterFcmTokenRequest, user: dict = Depends(get_current_user)): + """Registers or refreshes an FCM device push token into dedicated `device_tokens` + `users` and syncs FCM topics.""" + from app.database import device_tokens_collection + from app.services.push_notification_service import ( + ALL_SEGMENT_TOPICS, + resolve_user_topics, + sync_token_topics, + ) + + target_uid = user["uid"] + token = payload.fcm_token.strip() + if not token: + raise HTTPException(status_code=400, detail="FCM token cannot be empty") + + device_id = (payload.device_id or "").strip() + platform = (payload.platform or "android").strip().lower() or "android" + + user_doc = await users_collection.find_one( + {"uid": target_uid}, + {"uid": 1, "role": 1, "is_pro": 1, "subscription": 1, "mufti_status": 1, "is_business_enabled": 1}, + ) + if not user_doc and user.get("email"): + clean_email = str(user.get("email") or "").strip().lower() + if clean_email: + user_doc = await users_collection.find_one( + {"email": clean_email}, + {"uid": 1, "role": 1, "is_pro": 1, "subscription": 1, "mufti_status": 1, "is_business_enabled": 1}, + ) + if user_doc and user_doc.get("uid"): + target_uid = user_doc["uid"] + + if not user_doc: + # Avoid silently returning 200 OK during first-login Mongo user creation race + raise HTTPException( + status_code=409, + detail="User profile initializing, retry token registration", + ) + + now = datetime.datetime.utcnow() + desired_topics = resolve_user_topics(user_doc) + + # Deduplicate same device_id for other old tokens + if device_id: + stale_docs = await device_tokens_collection.find( + {"uid": target_uid, "device_id": device_id, "token": {"$ne": token}}, + {"token": 1, "_id": 0}, + ).to_list(length=20) + stale_tokens = [d["token"] for d in stale_docs if d.get("token")] + if stale_tokens: + await device_tokens_collection.delete_many( + {"uid": target_uid, "device_id": device_id, "token": {"$ne": token}} + ) + try: + await sync_token_topics(stale_tokens, [], ALL_SEGMENT_TOPICS) + except Exception: + pass + + # Upsert dedicated indexed device_tokens row (enterprise million-user store) + await device_tokens_collection.update_one( + {"token": token}, + { + "$set": { + "token": token, + "uid": target_uid, + "device_id": device_id, + "platform": platform, + "topics": desired_topics, + "valid": True, + "updated_at": now, + "last_seen_at": now, + }, + "$setOnInsert": {"created_at": now}, + }, + upsert=True, + ) + + # PX-17: Scrub legacy mirrors of this token from any prior account on the same device + await users_collection.update_many( + {"uid": {"$ne": target_uid}, "fcm_token": token}, + {"$set": {"fcm_token": None, "analytics.updated_at": now}}, + ) + await users_collection.update_many( + {"uid": {"$ne": target_uid}, "fcm_tokens.token": token}, + {"$pull": {"fcm_tokens": {"token": token}}, "$set": {"analytics.updated_at": now}}, + ) + + # Sync Firebase FCM segment topics asynchronously + topic_res = {"ok": False} + try: + topic_res = await sync_token_topics([token], desired_topics, ALL_SEGMENT_TOPICS) + except Exception as exc: + logger.warning("[FCM] Topic subscription notice for %s: %s", target_uid, exc) + + # Backward-compatible mirror onto users_collection + device_entry = { + "token": token, + "device_id": device_id, + "platform": platform, + "updated_at": now.isoformat(), + } + pull_cond = {"token": token} + if device_id: + pull_cond = {"$or": [{"token": token}, {"device_id": device_id}]} + await users_collection.update_one( + {"uid": target_uid}, + {"$pull": {"fcm_tokens": pull_cond}}, + ) + await users_collection.update_one( + {"uid": target_uid}, + { + "$set": { + "fcm_token": token, + "fcm_platform": platform, + "fcm_updated_at": now, + "fcm_topics": desired_topics, + "analytics.updated_at": now, + }, + "$push": { + "fcm_tokens": { + "$each": [device_entry], + "$slice": -10, + } + }, + }, + ) + return { + "status": "success", + "message": "FCM token registered successfully", + "topics": desired_topics, + "topics_synced": bool(topic_res.get("ok")), + } + + +@router.delete('/fcm-token') +async def unregister_fcm_token(token: Optional[str] = Query(None), user: dict = Depends(get_current_user)): + """Unregisters an FCM token from both `device_tokens` and `users` and removes topic memberships.""" + from app.database import device_tokens_collection + from app.services.push_notification_service import ALL_SEGMENT_TOPICS, sync_token_topics + + target_uid = user["uid"] + removed_tokens: List[str] = [] + + if token and token.strip(): + clean_token = token.strip() + removed_tokens.append(clean_token) + await device_tokens_collection.delete_many({"uid": target_uid, "token": clean_token}) + await users_collection.update_one( + {"uid": target_uid}, + {"$pull": {"fcm_tokens": {"token": clean_token}}}, + ) + # Rotate root fcm_token cleanly if the unregistered token was the active root token + if user.get("fcm_token") == clean_token: + updated_doc = await users_collection.find_one({"uid": target_uid}, {"fcm_tokens": 1}) + remaining = (updated_doc or {}).get("fcm_tokens") or [] + next_tok = remaining[-1].get("token") if (remaining and isinstance(remaining[-1], dict)) else None + await users_collection.update_one( + {"uid": target_uid}, + {"$set": {"fcm_token": next_tok}}, + ) + else: + docs = await device_tokens_collection.find({"uid": target_uid}, {"token": 1, "_id": 0}).to_list(length=50) + removed_tokens.extend([d["token"] for d in docs if d.get("token")]) + if user.get("fcm_token"): + removed_tokens.append(str(user["fcm_token"]).strip()) + await device_tokens_collection.delete_many({"uid": target_uid}) + await users_collection.update_one( + {"uid": target_uid}, + {"$set": {"fcm_tokens": [], "fcm_token": None}}, + ) + + if removed_tokens: + try: + await sync_token_topics(removed_tokens, [], ALL_SEGMENT_TOPICS) + except Exception: + pass + + return {"status": "success", "message": "FCM token unregistered"} + +_USER_FAVORITES_CACHE: Dict[str, Tuple[float, bytes]] = {} +_USER_FAVORITES_TTL = 30.0 + +@router.get('/favorites') +async def get_favorites(user: dict = Depends(get_current_user)): + uid = user["uid"] + ent = _USER_FAVORITES_CACHE.get(uid) + if ent and (time.monotonic() - ent[0]) < _USER_FAVORITES_TTL: + return Response(content=ent[1], media_type="application/json") + u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) + favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} + res = { + "status": "success", + "surahs": favs.get("surahs", []), + "ayahs": favs.get("ayahs", []) + } + raw_bytes = json.dumps(res).encode("utf-8") + _sweep_cache_if_needed(_USER_FAVORITES_CACHE) + _USER_FAVORITES_CACHE[uid] = (time.monotonic(), raw_bytes) + return Response(content=raw_bytes, media_type="application/json") + +@router.post('/favorites/toggle-surah') +async def toggle_favorite_surah(payload: ToggleFavoriteSurahRequest, user: dict = Depends(get_current_user)): + uid = user["uid"] + now = datetime.datetime.utcnow() + u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) + favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} + surahs = favs.get("surahs", []) + + existing = next((s for s in surahs if (isinstance(s, dict) and s.get("surah_no") == payload.surah_no) or (isinstance(s, (int, str)) and str(s) == str(payload.surah_no))), None) + if existing: + pull_target = {"surah_no": payload.surah_no} if isinstance(existing, dict) else existing + await users_collection.update_one( + {"uid": uid}, + { + "$pull": {"favorites.surahs": pull_target}, + "$set": {"analytics.updated_at": now} + } + ) + _USER_FAVORITES_CACHE.pop(uid, None) + return {"status": "success", "is_favorite": False, "surah_no": payload.surah_no} + else: + new_item = { + "surah_no": payload.surah_no, + "name_english": payload.name_english or f"Surah {payload.surah_no}", + "name_arabic": payload.name_arabic or "", + "translation_english": payload.translation_english or "", + "total_ayahs": payload.total_ayahs or 0, + "favorited_at": now.isoformat() + } + await users_collection.update_one( + {"uid": uid}, + { + "$push": {"favorites.surahs": new_item}, + "$set": {"analytics.updated_at": now} + } + ) + _USER_FAVORITES_CACHE.pop(uid, None) + return {"status": "success", "is_favorite": True, "surah_no": payload.surah_no, "item": new_item} + +@router.post('/favorites/toggle-ayah') +async def toggle_favorite_ayah(payload: ToggleFavoriteAyahRequest, user: dict = Depends(get_current_user)): + uid = user["uid"] + now = datetime.datetime.utcnow() + key = f"{payload.surah_no}:{payload.ayah_no}" + u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) + favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} + ayahs = favs.get("ayahs", []) + + existing = next((a for a in ayahs if (isinstance(a, dict) and (a.get("key") == key or (a.get("surah_no") == payload.surah_no and a.get("ayah_no") == payload.ayah_no))) or (isinstance(a, str) and a == key)), None) + if existing: + pull_target = {"key": key} if isinstance(existing, dict) else existing + await users_collection.update_one( + {"uid": uid}, + { + "$pull": {"favorites.ayahs": pull_target}, + "$set": {"analytics.updated_at": now} + } + ) + _USER_FAVORITES_CACHE.pop(uid, None) + return {"status": "success", "is_favorite": False, "key": key} + else: + new_item = { + "key": key, + "surah_no": payload.surah_no, + "ayah_no": payload.ayah_no, + "surah_name": payload.surah_name or f"Surah {payload.surah_no}", + "text_arabic": payload.text_arabic or "", + "text_english": payload.text_english or "", + "favorited_at": now.isoformat() + } + await users_collection.update_one( + {"uid": uid}, + { + "$push": {"favorites.ayahs": new_item}, + "$set": {"analytics.updated_at": now} + } + ) + _USER_FAVORITES_CACHE.pop(uid, None) + return {"status": "success", "is_favorite": True, "key": key, "item": new_item} + +@router.post('/favorites/sync') +async def sync_favorites(payload: SyncFavoritesRequest, user: dict = Depends(get_current_user)): + uid = user["uid"] + now = datetime.datetime.utcnow() + u = await users_collection.find_one({"uid": uid}, {"_id": 0, "favorites": 1}) + cloud_favs = (u or {}).get("favorites") or {"surahs": [], "ayahs": []} + + removed_surahs = {int(s) for s in (payload.removed_surah_nos or [])} + removed_ayahs = {str(k).strip() for k in (payload.removed_ayah_keys or []) if k} + + # Merge surahs by surah_no, applying durable client tombstones first (PX-22) + cloud_surahs = { + int(s.get("surah_no")): s + for s in cloud_favs.get("surahs", []) + if s.get("surah_no") is not None and int(s.get("surah_no")) not in removed_surahs + } + for s in payload.surahs: + sno = s.get("surah_no") + if sno is not None: + sno_int = int(sno) + if sno_int not in removed_surahs and sno_int not in cloud_surahs: + cloud_surahs[sno_int] = s + merged_surahs = sorted(cloud_surahs.values(), key=lambda x: x.get("surah_no", 0)) + + # Merge ayahs by key, applying durable client tombstones first (PX-22) + cloud_ayahs = {} + for a in cloud_favs.get("ayahs", []): + ak = a.get("key") or f"{a.get('surah_no')}:{a.get('ayah_no')}" + if ak not in removed_ayahs: + cloud_ayahs[ak] = a + for a in payload.ayahs: + k = a.get("key") or f"{a.get('surah_no')}:{a.get('ayah_no')}" + if k not in removed_ayahs and k not in cloud_ayahs: + a["key"] = k + cloud_ayahs[k] = a + merged_ayahs = list(cloud_ayahs.values()) + + await users_collection.update_one( + {"uid": uid}, + { + "$set": { + "favorites": { + "surahs": merged_surahs, + "ayahs": merged_ayahs + }, + "analytics.updated_at": now + } + } + ) + _USER_FAVORITES_CACHE.pop(uid, None) + return { + "status": "success", + "surahs": merged_surahs, + "ayahs": merged_ayahs + } + +@router.get('/chat-history/{uid}') +async def get_chat_history(uid: str, user: dict = Depends(get_current_user)): + enforce_self_or_admin(user, uid) + chats = await chats_collection.find({"user_id": uid}, {"_id": 0}).sort("last_updated", -1).to_list(length=100) + safe_chats = json.loads(json_util.dumps(chats)) + return [{"session_id": c.get("session_id"), "messages": c.get("messages", []), "custom_title": c.get("custom_title"), "is_pinned": c.get("is_pinned", False)} for c in safe_chats] + +@router.get('/chat-sessions/{uid}') +async def get_chat_sessions(uid: str, user: dict = Depends(get_current_user)): + """Lightweight chat list for the history drawer. + + SCALABILITY: this NEVER ships the full message arrays (a single chat + can hold dozens of long RAG answers). A server-side aggregation + projects only the first message (for a title) and the last message + (for a one-line preview) plus the metadata the drawer needs. This + keeps the payload tiny and the response fast even at 1M users. The + full conversation is fetched on demand via /chat-session/... when + the user actually opens a chat. + """ + enforce_self_or_admin(user, uid) + pipeline = [ + {"$match": {"user_id": uid}}, + {"$project": { + "_id": 0, + "session_id": 1, + "custom_title": 1, + "is_pinned": 1, + "last_updated": 1, + "first_message": {"$arrayElemAt": ["$messages", 0]}, + "last_message": {"$arrayElemAt": ["$messages", -1]}, + "message_count": {"$size": {"$ifNull": ["$messages", []]}}, + }}, + {"$sort": {"last_updated": -1}}, + {"$limit": 100}, + ] + chats = await chats_collection.aggregate(pipeline).to_list(length=100) + safe = json.loads(json_util.dumps(chats)) + result = [] + for c in safe: + first = c.get("first_message") or {} + last = c.get("last_message") or {} + result.append({ + "session_id": c.get("session_id"), + "custom_title": c.get("custom_title"), + "is_pinned": c.get("is_pinned", False), + "title": (first.get("content") or "")[:120], + "preview": (last.get("content") or "")[:160], + "message_count": c.get("message_count", 0), + }) + return result + + +@router.get('/chat-session/{uid}/{session_id}') +async def get_chat_session(uid: str, session_id: str, user: dict = Depends(get_current_user)): + """Full message history for ONE conversation, fetched on demand when + the user opens a chat from the history drawer.""" + enforce_self_or_admin(user, uid) + chat = await chats_collection.find_one( + {"user_id": uid, "session_id": session_id}, {"_id": 0} + ) + if not chat: + raise HTTPException(status_code=404, detail="Chat not found") + safe = json.loads(json_util.dumps(chat)) + return { + "session_id": session_id, + "messages": safe.get("messages", []), + "custom_title": safe.get("custom_title"), + "is_pinned": safe.get("is_pinned", False), + } + +@router.delete('/chat-history/{user_id}/{session_id}') +async def delete_chat(user_id: str, session_id: str, user: dict = Depends(get_current_user)): + enforce_self_or_admin(user, user_id) + await chats_collection.delete_one({"user_id": user_id, "session_id": session_id}) + return {"status": "deleted"} + +@router.put('/chat-history/{user_id}/{session_id}/rename') +async def rename_chat(user_id: str, session_id: str, payload: ChatTitleRequest, user: dict = Depends(get_current_user)): + enforce_self_or_admin(user, user_id) + await chats_collection.update_one({"user_id": user_id, "session_id": session_id}, {"$set": {"custom_title": payload.title}}) + return {"status": "renamed"} + +@router.put('/chat-history/{user_id}/{session_id}/pin') +async def pin_chat(user_id: str, session_id: str, payload: ChatPinRequest, user: dict = Depends(get_current_user)): + enforce_self_or_admin(user, user_id) + await chats_collection.update_one({"user_id": user_id, "session_id": session_id}, {"$set": {"is_pinned": payload.is_pinned}}) + return {"status": "pin_updated"} + +# ========================================================= +# 9. HIERARCHICAL LIBRARY ROUTES & BOUNDED CACHE +# ========================================================= +import urllib.parse +from collections import OrderedDict +import threading + +class BoundedTtlCache: + """Thread-safe bounded in-memory cache with TTL and LRU eviction (Fixes HFL-06).""" + def __init__(self, max_entries: int = 500, ttl: float = 600.0): + self._max_entries = max_entries + self._ttl = ttl + self._cache: OrderedDict = OrderedDict() + self._lock = threading.Lock() + + def get(self, key: Any) -> Optional[Any]: + with self._lock: + if key not in self._cache: + return None + ts, val = self._cache[key] + if (time.monotonic() - ts) > self._ttl: + del self._cache[key] + return None + self._cache.move_to_end(key) + return val + + def put(self, key: Any, value: Any) -> Any: + with self._lock: + now = time.monotonic() + if key in self._cache: + self._cache[key] = (now, value) + self._cache.move_to_end(key) + else: + if len(self._cache) >= self._max_entries: + self._cache.popitem(last=False) + self._cache[key] = (now, value) + return value + + def pop(self, key: Any, default: Any = None) -> Any: + with self._lock: + if key in self._cache: + _, val = self._cache.pop(key) + return val + return default + + def invalidate(self, prefix: Optional[str] = None): + with self._lock: + if prefix is None: + self._cache.clear() + else: + keys_to_del = [k for k in self._cache if isinstance(k, str) and k.startswith(prefix)] + for k in keys_to_del: + del self._cache[k] + + def clear(self): + with self._lock: + self._cache.clear() + + def __contains__(self, key: Any) -> bool: + return self.get(key) is not None + + def __len__(self) -> int: + with self._lock: + return len(self._cache) + +_LIB_CACHE = BoundedTtlCache(max_entries=500, ttl=600.0) + +def _lib_cache_get(key): + return _LIB_CACHE.get(key) + +def _lib_cache_put(key, value): + return _LIB_CACHE.put(key, value) + +def invalidate_hadith_library_cache(collection_name: Optional[str] = None): + """Invalidates library cache entries (called on Hadith modifications, Fixes HFL-07).""" + if collection_name: + clean_col = urllib.parse.unquote(collection_name).strip() + _LIB_CACHE.invalidate(f"hadiths:{clean_col}") + _LIB_CACHE.invalidate(f"books:{clean_col}") + _LIB_CACHE.invalidate(f"volumes:{clean_col}") + _BOOK_BUNDLE_CACHE.pop(clean_col, None) + _BOOK_BUNDLE_GZIP_CACHE.pop(clean_col, None) + _LIB_CACHE.pop("books", None) + _LIB_CACHE.pop("stats", None) + else: + _LIB_CACHE.clear() + _BOOK_BUNDLE_CACHE.clear() + _BOOK_BUNDLE_GZIP_CACHE.clear() + +HADITH_READER_PROJECTION = { + "_id": 1, + "hadith_id": 1, + "hadith_no": 1, + "book_no": 1, + "book_name": 1, + "chapter_no": 1, + "chapter_name": 1, + "volume": 1, + "collection": 1, + "narrator": 1, + "text_ar": 1, + "text_en": 1, + "text_ur": 1, + "text_hi": 1, + "text_arabic": 1, + "text_english": 1, + "text_urdu": 1, + "text_hinglish": 1, + "grade": 1, + "reference": 1, + "sanad": 1, + "source": 1, +} + +_EXPORTS_DIR = Path(__file__).resolve().parent.parent / "exports" / "hadith_collections" + +def _get_hadith_slug(collection_name: str) -> str: + lower = collection_name.lower().strip() + if 'bukhari' in lower: + return 'sahih_al_bukhari' + if 'muslim' in lower: + return 'sahih_muslim' + if 'tirmidh' in lower or 'tirmiz' in lower: + return 'jami_at_tirmidhi' + if 'dawud' in lower or 'dawood' in lower or 'dawd' in lower: + return 'sunan_abi_dawud' + return re.sub(r'[^a-z0-9]+', '_', lower).strip('_') + +@router.get('/api/library/books') +async def get_all_books(): + cached = _lib_cache_get("books") + if cached is not None: + return cached + + # Fast-path: read from exports/index.json if present + idx_file = _EXPORTS_DIR / "index.json" + if idx_file.is_file(): + try: + with open(idx_file, "r", encoding="utf-8") as f: + idx_data = json.load(f) + names = [c["name"] for c in idx_data.get("collections", []) if c.get("name")] + if names: + res = { + "books": names, + "total_collections": len(names), + "content_version": "v1", + } + return _lib_cache_put("books", res) + except Exception: + pass + + raw = await hadith_collection.distinct("collection") + books = [b for b in raw if b and isinstance(b, str) and b.strip()] + res = { + "books": books, + "total_collections": len(books), + "content_version": "v1", + } + return _lib_cache_put("books", res) + +@router.get('/api/library/volumes/{collection_name}') +async def get_volumes(collection_name: str): + collection_name = urllib.parse.unquote(collection_name).strip() + ckey = f"volumes:{collection_name}" + cached = _lib_cache_get(ckey) + if cached is not None: + return cached + + # Fast-path: read from exports//metadata.json if present + slug = _get_hadith_slug(collection_name) + meta_file = _EXPORTS_DIR / slug / "metadata.json" + if meta_file.is_file(): + try: + with open(meta_file, "r", encoding="utf-8") as f: + meta = json.load(f) + vols = meta.get("volumes", []) + if vols: + res = { + "volumes": [ + {"volume": v["volume"], "first": v.get("first"), "last": v.get("last"), "count": v.get("count", 0)} + for v in vols + ], + "content_version": "v1", + } + return _lib_cache_put(ckey, res) + except Exception: + pass + + pipeline = [ + {"$match": {"collection": collection_name}}, + {"$group": {"_id": "$volume", + "first": {"$min": "$hadith_id"}, + "last": {"$max": "$hadith_id"}, + "count": {"$sum": 1}}}, + ] + rows = await hadith_collection.aggregate(pipeline).to_list(length=1000) + rows = [r for r in rows if r.get("_id") is not None and str(r["_id"]).strip()] + rows.sort(key=lambda r: int(r["_id"]) if str(r["_id"]).isdigit() else float('inf')) + res = { + "volumes": [ + {"volume": r["_id"], "first": r.get("first"), "last": r.get("last"), "count": r.get("count", 0)} + for r in rows + ], + "content_version": "v1", + } + return _lib_cache_put(ckey, res) + +@router.get('/api/library/books/{collection_name}/{volume_no}') +async def get_books_by_volume(collection_name: str, volume_no: str): + collection_name = urllib.parse.unquote(collection_name).strip() + ckey = f"books:{collection_name}:{volume_no}" + cached = _lib_cache_get(ckey) + if cached is not None: + return cached + + # Fast-path: read from exports//metadata.json sub_books if present + slug = _get_hadith_slug(collection_name) + meta_file = _EXPORTS_DIR / slug / "metadata.json" + if meta_file.is_file(): + try: + with open(meta_file, "r", encoding="utf-8") as f: + meta = json.load(f) + vol_target = int(volume_no) if volume_no.isdigit() else volume_no + for v in meta.get("volumes", []): + if str(v.get("volume")) == str(vol_target): + sub_books = v.get("sub_books", []) + res = {"sub_books": sub_books, "content_version": "v1"} + return _lib_cache_put(ckey, res) + except Exception: + pass + + vol_num = int(volume_no) if volume_no.isdigit() else volume_no + pipeline = [ + {"$match": {"collection": collection_name, "volume": {"$in": [vol_num, str(volume_no)]}}}, + {"$group": {"_id": "$book_no", "book_name": {"$first": "$book_name"}, "unique_chapters": {"$addToSet": "$chapter_no"}, "total_hadiths": {"$sum": 1}, "first": {"$min": "$hadith_id"}, "last": {"$max": "$hadith_id"}}} + ] + results = await hadith_collection.aggregate(pipeline).to_list(length=1000) + formatted_books = sorted([{"book_no": r["_id"], "book_name": r.get("book_name", "Unknown Book"), "chapter_count": len(r.get("unique_chapters", [])), "hadith_count": r.get("total_hadiths", 0), "first": r.get("first"), "last": r.get("last")} for r in results], key=lambda x: int(x["book_no"]) if str(x["book_no"]).isdigit() else 0) + res = {"sub_books": formatted_books, "content_version": "v1"} + return _lib_cache_put(ckey, res) + +@router.get('/api/library/hadiths/{collection_name}/{volume_no}/{book_no}') +async def get_hadiths_final( + collection_name: str, + volume_no: str, + book_no: str, + request: Request, + cursor: Optional[str] = Query(default=None, description="Cursor for pagination (last seen hadith_id)"), + limit: int = Query(default=20, ge=1, le=100, description="Page size (default 20, max 100)"), + all: bool = Query(default=False, description="Fetch all hadiths without pagination"), +): + collection_name = urllib.parse.unquote(collection_name).strip() + ckey = f"hadiths:{collection_name}:{volume_no}:{book_no}:{cursor or 'first'}:{limit}:{all}" + cached = _lib_cache_get(ckey) + if cached is not None: + etag = cached.get("etag") + client_etag = request.headers.get("if-none-match") + if client_etag and etag and client_etag.strip('"') == etag.strip('"'): + return Response(status_code=304, headers={"ETag": etag, "Cache-Control": "private, must-revalidate"}) + return JSONResponse(content=cached, headers={"ETag": etag, "Cache-Control": "private, must-revalidate"}) if etag else cached + + vol_num = int(volume_no) if volume_no.isdigit() else volume_no + b_num = int(book_no) if book_no.isdigit() else book_no + + query: Dict[str, Any] = { + "collection": collection_name, + "volume": {"$in": [vol_num, str(volume_no)]}, + "book_no": {"$in": [b_num, str(book_no)]}, + } + + if cursor is not None and str(cursor).strip(): + cur_str = str(cursor).strip() + if cur_str.isdigit(): + query["hadith_id"] = {"$gt": int(cur_str)} + else: + query["hadith_id"] = {"$gt": cur_str} + + page_size = 2000 if all else min(max(int(limit), 1), 100) + # Fetch page_size + 1 to detect has_more without a secondary count query + docs = await hadith_collection.find(query, HADITH_READER_PROJECTION).sort([("hadith_id", 1), ("_id", 1)]).to_list(length=page_size + 1) + + # HFL-02: DO NOT drop volume constraint if docs is empty. Volume separation must be strictly preserved. + has_more = False + next_cursor = None + if len(docs) > page_size: + has_more = True + docs = docs[:page_size] + last_hid = docs[-1].get("hadith_id") + next_cursor = str(last_hid) if last_hid is not None else None + + # Single-pass fast serialization + for d in docs: + if "_id" in d: + d["_id"] = str(d["_id"]) + + # Total count for initial page + total_count = None + if cursor is None: + total_count = await hadith_collection.count_documents({ + "collection": collection_name, + "volume": {"$in": [vol_num, str(volume_no)]}, + "book_no": {"$in": [b_num, str(book_no)]}, + }) + + # Generate deterministic ETag + first_id = docs[0].get("hadith_id") if docs else 0 + last_id = docs[-1].get("hadith_id") if docs else 0 + etag_raw = f"{collection_name}:{volume_no}:{book_no}:{cursor}:{len(docs)}:{first_id}:{last_id}:{total_count}" + etag = f'"{hashlib.md5(etag_raw.encode("utf-8")).hexdigest()}"' + + client_etag = request.headers.get("if-none-match") + if client_etag and client_etag.strip('"') == etag.strip('"'): + return Response(status_code=304, headers={"ETag": etag, "Cache-Control": "private, must-revalidate"}) + + res = { + "hadiths": docs, + "next_cursor": next_cursor, + "has_more": has_more, + "total_count": total_count, + "content_version": "v1", + "etag": etag, + } + _lib_cache_put(ckey, res) + return JSONResponse( + content=res, + headers={ + "ETag": etag, + "Cache-Control": "private, must-revalidate, max-age=60", + }, + ) + +_BOOK_BUNDLE_CACHE: BoundedTtlCache = BoundedTtlCache(max_entries=10, ttl=3600.0) +_BOOK_BUNDLE_GZIP_CACHE: BoundedTtlCache = BoundedTtlCache(max_entries=10, ttl=3600.0) + +@router.get('/api/library/bundle/{collection_name}') +async def get_book_bundle(collection_name: str, request: Request): + """ + High-performance single-request collection bundle for instant offline downloads. + Transfers the entire hadith book collection in a single gzip-compressed payload, + reducing 100+ sequential round-trips to 1 instant transfer (30x speedup). + """ + accept_encoding = request.headers.get("accept-encoding", "") + use_gzip = "gzip" in accept_encoding + + cached_bundle = _BOOK_BUNDLE_CACHE.get(collection_name) + cached_gzip = _BOOK_BUNDLE_GZIP_CACHE.get(collection_name) + if cached_bundle is not None: + if use_gzip and cached_gzip is not None: + return Response( + content=cached_gzip, + media_type="application/json", + headers={ + "Content-Encoding": "gzip", + "Cache-Control": "public, max-age=604800, immutable", + "Accept-Ranges": "bytes", + } + ) + return Response( + content=cached_bundle, + media_type="application/json", + headers={ + "Cache-Control": "public, max-age=604800, immutable", + "Accept-Ranges": "bytes", + } + ) + + # 1. Fetch volumes + pipeline_vols = [ + {"$match": {"collection": collection_name}}, + {"$group": {"_id": "$volume", + "first": {"$min": "$hadith_id"}, + "last": {"$max": "$hadith_id"}, + "count": {"$sum": 1}}}, + ] + rows = await hadith_collection.aggregate(pipeline_vols).to_list(length=1000) + rows = [r for r in rows if r.get("_id") is not None and str(r["_id"]).strip()] + rows.sort(key=lambda r: int(r["_id"]) if str(r["_id"]).isdigit() else float('inf')) + volumes_data = [ + {"volume": r["_id"], "first": r.get("first"), "last": r.get("last"), "count": r.get("count", 0)} + for r in rows + ] + + # 2. Fetch all hadiths in this collection (eliminate 32MB database in-memory sort limit) + proj = {"embedding": 0, "embedding_1024": 0} + all_hadiths = await hadith_collection.find({"collection": collection_name}, proj).to_list(length=20000) + + # Group hadiths by volume and book_no + grouped_hadiths: Dict[str, Dict[str, list]] = {} + sub_books_by_vol: Dict[str, list] = {} + + for h in all_hadiths: + v_str = str(h.get("volume", "1")) + b_str = str(h.get("book_no", "1")) + if v_str not in grouped_hadiths: + grouped_hadiths[v_str] = {} + if b_str not in grouped_hadiths[v_str]: + grouped_hadiths[v_str][b_str] = [] + grouped_hadiths[v_str][b_str].append(h) + + def _hadith_sort_key(doc): + hid = doc.get("hadith_id") + try: + return (0, int(hid)) + except (ValueError, TypeError): + return (1, str(hid or "")) + + # Format sub_books per volume + for v_str, b_dict in grouped_hadiths.items(): + sub_books = [] + for b_str, h_list in b_dict.items(): + h_list.sort(key=_hadith_sort_key) + b_name = h_list[0].get("book_name", "Book " + b_str) if h_list else "Book " + b_str + unique_chapters = list({h.get("chapter_no") for h in h_list if h.get("chapter_no")}) + sub_books.append({ + "book_no": b_str, + "book_name": b_name, + "chapter_count": len(unique_chapters), + "hadith_count": len(h_list), + "first": h_list[0].get("hadith_id") if h_list else None, + "last": h_list[-1].get("hadith_id") if h_list else None, + }) + sub_books.sort(key=lambda x: int(x["book_no"]) if str(x["book_no"]).isdigit() else 0) + sub_books_by_vol[v_str] = sub_books + + payload = { + "collection": collection_name, + "volumes": volumes_data, + "sub_books_by_volume": sub_books_by_vol, + "hadiths_by_volume_book": grouped_hadiths, + } + + raw_json = json_util.dumps(payload) + raw_bytes = raw_json.encode("utf-8") + compressed = gzip.compress(raw_bytes) + + _BOOK_BUNDLE_CACHE.put(collection_name, raw_bytes) + _BOOK_BUNDLE_GZIP_CACHE.put(collection_name, compressed) + + if use_gzip: + return Response( + content=compressed, + media_type="application/json", + headers={ + "Content-Encoding": "gzip", + "Cache-Control": "public, max-age=604800, immutable", + "Accept-Ranges": "bytes", + } + ) + return Response( + content=raw_bytes, + media_type="application/json", + headers={ + "Cache-Control": "public, max-age=604800, immutable", + "Accept-Ranges": "bytes", + } + ) + + +@router.get('/api/library/stats') +async def get_library_stats(): + cached = _lib_cache_get("stats") + if cached is not None: + return cached + + # Fast-path: read from exports/index.json if present + idx_file = _EXPORTS_DIR / "index.json" + if idx_file.is_file(): + try: + with open(idx_file, "r", encoding="utf-8") as f: + idx_data = json.load(f) + cols = idx_data.get("collections", []) + if cols: + total_collections = len(cols) + total_books = sum(int(c.get("total_books") or 0) for c in cols) + total_hadiths = sum(int(c.get("total_hadiths") or 0) for c in cols) + res = { + "collections": total_collections, + "books": total_books, + "hadiths": total_hadiths, + "content_version": "v1", + } + return _lib_cache_put("stats", res) + except Exception: + pass + + raw_collections = await hadith_collection.distinct("collection") + valid_collections = [c for c in raw_collections if c and isinstance(c, str) and c.strip()] + total_collections = len(valid_collections) + # HFL-16: Group by collection, volume, and book_no so sub-books across volumes are counted accurately + book_pairs = await hadith_collection.aggregate( + [{"$group": {"_id": {"collection": "$collection", "volume": "$volume", "book_no": "$book_no"}}}] + ).to_list(length=None) + valid_pairs = [p for p in book_pairs if p.get("_id", {}).get("collection")] + total_books = len(valid_pairs) + total_hadiths = await hadith_collection.estimated_document_count() + res = { + "collections": total_collections, + "books": total_books, + "hadiths": total_hadiths, + "content_version": "v1", + } + return _lib_cache_put("stats", res) + +# ========================================================= +# 10. LIBRARY SEARCH ROUTE +# ========================================================= +_LIBRARY_SEARCH_CACHE: BoundedTtlCache = BoundedTtlCache(max_entries=200, ttl=300.0) + +@router.post('/api/library/search') +@limiter.limit("1200/minute") +async def library_search(payload: LibrarySearchRequest, request: Request): + try: + query = payload.query.lower().strip() + if not query: return {"results": []} + + cache_key = (query, tuple(sorted(payload.books or []))) + cached_res = _LIBRARY_SEARCH_CACHE.get(cache_key) + if cached_res is not None: + return Response(content=cached_res, media_type="application/json") + + numbers_in_query = re.findall(r'\d+', query) + combined_results = [] + seen_ids = set() + + # Only fetch the handful of fields the response actually uses — never + # the 1024-float `embedding` nor the full Arabic/Urdu/Hinglish/sanad + # text. This keeps every search result tiny and the query fast. + proj = {"hadith_id": 1, "collection": 1, "book_name": 1, "chapter_name": 1, + "text_english": 1, "reference": 1, "volume": 1, "book_no": 1} + + # --- 10.1 FAST-PATH EXACT MATCH (find hadith number N) --- + collection_mapping = { + "bukhari": "Bukhari", "muslim": "Muslim", "tirmidhi": "Tirmidhi", "tirmizi": "Tirmidhi", + "dawud": "Dawud", "dawood": "Dawud", "nasai": "Nasa'i", "majah": "Majah", + "muwatta": "Muwatta", "ahmad": "Ahmad", "darimi": "Darimi", "daraqutni": "Daraqutni" + } + is_pure_lookup = False # "just find hadith N" (no topical words) -> skip text search + if numbers_in_query: + # A pure lookup is a number (optionally with citation words / a + # collection name) and NO topical words like "ablution". + leftover = [w for w in re.sub(r'[\W\d_]+', ' ', query).split() + if w not in collection_mapping + and w not in ("hadith", "no", "number", "book", "bk", "vol", "volume")] + is_pure_lookup = len(leftover) == 0 + + detected_col = next((val for key, val in collection_mapping.items() if key in query), None) + if detected_col and payload.books and not any(detected_col.lower() in b.lower() for b in payload.books): + detected_col = None + + # Scope precedence: a collection named in the query > the user's + # selected book filter(s) > (bare number only) every collection. + scope_filters = None + if detected_col: + scope_filters = [{"collection": {"$regex": detected_col, "$options": "i"}}] + elif payload.books: + scope_filters = [{"collection": b} for b in payload.books] + elif is_pure_lookup: + scope_filters = [{}] + + if scope_filters is not None: + hadith_match = re.search(r'(?:hadith|no\.?|number|#)\s*(\d+)', query) + book_match = re.search(r'(?:book|bk)\s*(\d+)', query) + vol_match = re.search(r'(?:vol|volume)\s*(\d+)', query) + + ids = [int(hadith_match.group(1))] if hadith_match else [int(n) for n in numbers_in_query] + # Match the HADITH NUMBER only (not book_no) — searching "50" + # means hadith 50, returned from each scoped collection. + id_or = [{"hadith_id": {"$in": ids}}, {"hadith_id": {"$in": [str(x) for x in ids]}}] + + for sf in scope_filters: + ex = dict(sf) + ex["$or"] = id_or + if book_match: + ex["book_no"] = {"$in": [int(book_match.group(1)), book_match.group(1)]} + if vol_match: + ex["volume"] = {"$in": [int(vol_match.group(1)), vol_match.group(1)]} + exact_docs = await hadith_collection.find(ex, proj).sort("hadith_id", 1).limit(25).to_list(length=25) + for doc in exact_docs: + if str(doc['_id']) not in seen_ids: + combined_results.append(doc) + seen_ids.add(str(doc['_id'])) + + # --- 10.2 STANDARD TEXT SEARCH (skip for pure number lookups) --- + if not is_pure_lookup: + try: + search_filter = {"$text": {"$search": query}} + if payload.books: search_filter["collection"] = {"$in": payload.books} + + text_results = await hadith_collection.find(search_filter, {**proj, "score": {"$meta": "textScore"}}).sort([("score", {"$meta": "textScore"})]).limit(25).to_list(length=25) + except Exception as text_e: + logger.warning("Text index missing in library search, using Regex fallback.") + search_filter = {"text_english": {"$regex": re.escape(query), "$options": "i"}} + if payload.books: search_filter["collection"] = {"$in": payload.books} + text_results = await hadith_collection.find(search_filter, proj).limit(25).to_list(length=25) + + for doc in text_results: + if str(doc['_id']) not in seen_ids: + combined_results.append(doc) + seen_ids.add(str(doc['_id'])) + + # --- 10.3 FORMATTING --- + formatted_results = [{ + "hadith_id": str(r.get("hadith_id", "")), "collection": r.get("collection", "Unknown"), + "book_name": r.get("book_name", "Unknown"), "chapter_name": r.get("chapter_name", "Unknown"), + "text_english": r.get("text_english", ""), "reference": r.get("reference", ""), + "volume": str(r.get("volume", "1")), "book_no": str(r.get("book_no", "1")) + } for r in combined_results] + + res_obj = {"results": formatted_results} + res_bytes = json.dumps(res_obj).encode("utf-8") + _LIBRARY_SEARCH_CACHE.put(cache_key, res_bytes) + return Response(content=res_bytes, media_type="application/json") + + except Exception as e: + logger.error(f"Library Search Error: {str(e)}") + raise HTTPException(status_code=500, detail="Internal Server Error") + + # ========================================================= +# 📖 QURAN READER ENDPOINTS (Offloaded from AI Engine) +# ========================================================= + +_QURAN_ALL_SURAHS_CACHE: Optional[bytes] = None +_QURAN_ALL_SURAHS_GZIP_CACHE: Optional[bytes] = None +_QURAN_SURAH_CACHE: Dict[int, bytes] = {} +from app.services.quran_service import get_all_surahs_payload, get_surah_payload + +@router.get("/quran/surahs") +@limiter.limit("1200/minute") +async def get_all_surahs(request: Request): + """ + Fetches the lightweight metadata for all 114 Surahs. + Ultra-high performance in-memory cached (0.005ms response). + """ + accept_encoding = request.headers.get("accept-encoding", "") + use_gzip = "gzip" in accept_encoding + payload_bytes, is_gzip = await get_all_surahs_payload(use_gzip=use_gzip) + headers = {"Content-Encoding": "gzip"} if is_gzip else None + return Response(content=payload_bytes, media_type="application/json", headers=headers) + + +@router.get("/quran/surah/{surah_no}") +@limiter.limit("1200/minute") +async def get_surah_details(request: Request, surah_no: int): + """ + Fetches the complete Surah, including all Arabic text and translations. + Ultra-high performance in-memory cached with pre-compressed gzip (0.01ms response). + """ + if surah_no < 1 or surah_no > 114: + raise HTTPException(status_code=400, detail="Invalid Surah number (must be 1-114)") + + accept_encoding = request.headers.get("accept-encoding", "") + use_gzip = "gzip" in accept_encoding + payload_bytes, is_gzip = await get_surah_payload(surah_no, use_gzip=use_gzip) + headers = {"Content-Encoding": "gzip"} if is_gzip else None + return Response(content=payload_bytes, media_type="application/json", headers=headers) + + + + +# ========================================================= +# 11. DAILY PROGRESS / STREAK (server-authoritative, per user) +# ========================================================= +# Replaces the old device-local (SharedPreferences) store so a user's +# "Today's Progress" and streak follow them across devices and survive a +# reinstall. One small doc per (uid, local-date): +# { uid, date: "YYYY-MM-DD", prayers: [...], quran_secs, hadith_secs } +# The streak is computed by walking these docs backwards from today. +PROGRESS_PRAYERS = ["Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"] +PROGRESS_GOAL_SECONDS = 600 # 10 minutes to earn Quran / Hadith +PROGRESS_TASBEEH_GOAL = 297 # 297 beads to satisfy the 7th deed option +PROGRESS_TOTAL_GOALS = 7 # 5 prayers + quran + (hadith OR 297 tasbeeh) + + +class ProgressPrayerReq(BaseModel): + prayer: str = Field(..., max_length=20) + tz: float = Field(default=0.0, ge=-12, le=14) + done: Optional[bool] = None + date: Optional[str] = Field(None, max_length=15, description="YYYY-MM-DD local Islamic date to toggle") + fajr: Optional[str] = Field(None, max_length=20, description="Fajr azaan time (e.g. 05:15 AM)") + + +class SetPasswordRequest(BaseModel): + has_password: bool = True + + +class ProgressTimeReq(BaseModel): + category: str = Field(..., pattern="^(quran|hadith)$") + seconds: int = Field(..., ge=0, le=3600) + tz: float = Field(default=0.0, ge=-12, le=14) + date: Optional[str] = Field(None, max_length=15) + fajr: Optional[str] = Field(None, max_length=20) + event_id: Optional[str] = Field(None, max_length=64) + + +class HarvestApplesReq(BaseModel): + tz: float = Field(default=0.0, ge=-12, le=14) + date: Optional[str] = Field(None, max_length=15) + fajr: Optional[str] = Field(None, max_length=20) + quran_secs: Optional[int] = Field(default=None, ge=0) + hadith_secs: Optional[int] = Field(default=None, ge=0) + tasbeeh_count: Optional[int] = Field(default=None, ge=0) + prayers: Optional[List[str]] = Field(default=None) + + +def _progress_local_date(tz: float, fajr_time_str: Optional[str] = None) -> datetime.date: + """The user's *Islamic prayer date*, rolling over at Fajr Azaan rather than civil midnight. + A streak day and marking prayers starts at Fajr azaan and ends at next day's Fajr azaan. + Between midnight (00:00) and Fajr azaan, time belongs to the previous day's prayer cycle.""" + local_now = datetime.datetime.utcnow() + datetime.timedelta(hours=tz) + fajr_hour, fajr_minute = 5, 15 # Sensible default if not provided + if fajr_time_str and str(fajr_time_str).strip(): + m = re.match(r"^(\d{1,2}):(\d{2})(?:\s*(AM|PM))?", str(fajr_time_str).strip(), re.IGNORECASE) + if m: + try: + h = int(m.group(1)) + minute = int(m.group(2)) + ampm = (m.group(3) or "").upper() + if ampm == "PM" and h != 12: + h += 12 + elif ampm == "AM" and h == 12: + h = 0 + # L-01: Strict range validation for hour (0..23) and minute (0..59) + if 0 <= h <= 23 and 0 <= minute <= 59: + fajr_hour, fajr_minute = h, minute + except (ValueError, TypeError): + fajr_hour, fajr_minute = 5, 15 + + # If local time is before Fajr azaan, it belongs to the previous day's prayer cycle! + try: + fajr_time = datetime.time(fajr_hour, fajr_minute) + except (ValueError, TypeError): + fajr_time = datetime.time(5, 15) + + if local_now.time() < fajr_time: + return (local_now - datetime.timedelta(days=1)).date() + return local_now.date() + + +def _validate_progress_date(date_str: Optional[str], tz: float, fajr: Optional[str] = None, max_past_days: int = 30) -> str: + """M-05: Validates that a client-supplied date is well-formed, not in the future, + and within allowable historical bounds.""" + today = _progress_local_date(tz, fajr) + if not date_str or not date_str.strip(): + return today.isoformat() + clean = date_str.strip() + try: + parsed = datetime.date.fromisoformat(clean) + except Exception: + raise HTTPException(status_code=400, detail=f"Invalid date format: {clean}. Expected YYYY-MM-DD.") + if parsed > today: + raise HTTPException(status_code=400, detail="Cannot record activity for future dates.") + if (today - parsed).days > max_past_days: + raise HTTPException(status_code=400, detail=f"Date {clean} exceeds maximum historical window ({max_past_days} days).") + return clean + + +def _normalize_progress_prayer_name(raw: str) -> Optional[str]: + if not raw: + return None + cleaned = str(raw).strip().upper() + if cleaned in ("FAJR", "FAJAR"): + return "Fajr" + if cleaned in ("DHUHR", "ZUHR", "ZOHR", "DHUR", "DUHR", "ZUHAR", "JUMMAH", "JUMMA"): + return "Dhuhr" + if cleaned == "ASR": + return "Asr" + if cleaned in ("MAGHRIB", "MAGRIB"): + return "Maghrib" + if cleaned in ("ISHA", "ISHAA"): + return "Isha" + for p in PROGRESS_PRAYERS: + if p.upper() == cleaned: + return p + return None + + +def _progress_prayer_streak_day_complete(doc: dict) -> bool: + """A day counts toward prayer streak if all 5 canonical prayers are marked (PT-06). + The authoritative canonical prayer array is always verified; a stale is_full flag never overrides an incomplete array. + """ + raw = doc.get("prayers", []) or [] + prayers = set(_normalize_progress_prayer_name(p) for p in raw if _normalize_progress_prayer_name(p)) + return len(prayers.intersection(PROGRESS_PRAYERS)) == 5 + + +def _progress_golden_streak_day_complete(doc: dict) -> bool: + """A day counts toward golden streak if all 5 prayers are marked AND 10 min Quran AND (10 min Hadith OR 297 Tasbeeh) (PT-06). + The authoritative canonical prayer array is always verified. + """ + raw = doc.get("prayers", []) or [] + prayers = set(_normalize_progress_prayer_name(p) for p in raw if _normalize_progress_prayer_name(p)) + if len(prayers.intersection(PROGRESS_PRAYERS)) != 5: + return False + quran_done = int(doc.get("quran_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS + hadith_done = int(doc.get("hadith_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS + tasbeeh_done = int(doc.get("tasbeeh_count", 0) or 0) >= PROGRESS_TASBEEH_GOAL + return quran_done and (hadith_done or tasbeeh_done) + + +def _progress_deeds_count(doc: dict) -> int: + """Calculates the number of completed deeds out of 7 for a daily progress document. + Deeds (7 total): + - 5 daily prayers (Fajr, Dhuhr, Asr, Maghrib, Isha) + - Quran recitation (>= 10 minutes / 600s) + - Hadith study (>= 10 minutes / 600s) OR Tasbeeh dhikr (>= 297 beads) + """ + raw = doc.get("prayers", []) or [] + prayers = set(_normalize_progress_prayer_name(p) for p in raw if _normalize_progress_prayer_name(p)) + prayers_count = len([p for p in PROGRESS_PRAYERS if p in prayers]) + quran_done = 1 if int(doc.get("quran_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS else 0 + hadith_done = int(doc.get("hadith_secs", 0) or 0) >= PROGRESS_GOAL_SECONDS + tasbeeh_done = int(doc.get("tasbeeh_count", 0) or 0) >= PROGRESS_TASBEEH_GOAL + seventh_deed = 1 if (hadith_done or tasbeeh_done) else 0 + return prayers_count + quran_done + seventh_deed + + +async def _recalculate_nimaz_stats_for_user(uid: str, today_str: Optional[str] = None) -> dict: + """Aggregates all-time Nimaz & Spiritual stats for a user from daily_progress_collection + and persists the single-document profile into nimaz_stats_collection (_id: uid). + Guarantees O(1) performance for lifetime queries, missed counts, and qaza tracking.""" + if not today_str: + today_str = datetime.datetime.utcnow().date().isoformat() + + user_doc = None + try: + user_doc = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + { + "progress_tracking_started_date": 1, + "progress_tracking_started_at": 1, + "progress_tracking": 1, + "created_at": 1, + } + ) + except Exception: + user_doc = None + pt = (user_doc or {}).get("progress_tracking") or {} + started_date = ( + (user_doc or {}).get("progress_tracking_started_date") + or pt.get("started_date") + or (user_doc or {}).get("progress_tracking_started_at") + or pt.get("started_at") + ) + + try: + docs = await daily_progress_collection.find( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"_id": 0, "date": 1, "prayers": 1, "quran_secs": 1, "hadith_secs": 1, "tasbeeh_count": 1, "is_full": 1, "is_golden": 1} + ).to_list(length=50000) + except Exception as e: + logger.error(f"Error reading daily_progress for user {uid} during stats recalculation: {e}") + # M-12: Abort recalculation on transient database read failure to avoid zeroing out existing stats! + existing_stats = await nimaz_stats_collection.find_one({"_id": uid}, {"_id": 0}) or {} + return existing_stats + + # Extract all valid recorded dates + valid_doc_dates = [ + d.get("date") for d in docs + if d.get("date") and isinstance(d.get("date"), str) + ] + earliest_doc_date = min(valid_doc_dates) if valid_doc_dates else None + + # The user started recording progress on the earliest of their records or started_date + candidates = [d[:10] for d in [started_date, earliest_doc_date] if d and isinstance(d, str)] + if candidates: + effective_started_date = min(candidates) + else: + effective_started_date = today_str[:10] + + # Auto-heal user's start date if not persisted or if records precede started_date + if effective_started_date and (not started_date or effective_started_date < started_date[:10]): + try: + await users_collection.update_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + { + "$set": { + "progress_tracking_started_date": effective_started_date, + "progress_tracking.started_date": effective_started_date, + } + } + ) + except Exception: + pass + + try: + start_d = datetime.date.fromisoformat(effective_started_date[:10]) + except Exception: + start_d = datetime.date.fromisoformat(today_str[:10]) + try: + today_d = datetime.date.fromisoformat(today_str[:10]) + except Exception: + today_d = datetime.datetime.utcnow().date() + + if start_d > today_d: + start_d = today_d + + docs_by_date = {} + for d in docs: + dt_str = d.get("date") + if dt_str and dt_str not in docs_by_date: + docs_by_date[dt_str] = d + + prayed_counts = {p: 0 for p in PROGRESS_PRAYERS} + missed_counts = {p: 0 for p in PROGRESS_PRAYERS} + total_quran_days = 0 + total_golden_days = 0 + total_full_days = 0 + + num_past_days = (today_d - start_d).days + valid_days_count = num_past_days + 1 + + # Process all elapsed calendar days from the start of progress tracking up to yesterday + for offset in range(num_past_days): + day_date = start_d + datetime.timedelta(days=offset) + day_str = day_date.isoformat() + doc = docs_by_date.get(day_str) + + if doc is not None: + raw_prayers = doc.get("prayers", []) or [] + norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) + + for p in PROGRESS_PRAYERS: + if p in norm_set: + prayed_counts[p] += 1 + else: + missed_counts[p] += 1 + + is_full = doc.get("is_full") + if is_full is None: + is_full = len(norm_set) == 5 + if is_full: + total_full_days += 1 + + q_secs = int(doc.get("quran_secs", 0) or 0) + h_secs = int(doc.get("hadith_secs", 0) or 0) + t_count = int(doc.get("tasbeeh_count", 0) or 0) + + if q_secs >= PROGRESS_GOAL_SECONDS: + total_quran_days += 1 + + is_golden = doc.get("is_golden") + if is_golden is None: + is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) + if is_golden: + total_golden_days += 1 + else: + # Tracked elapsed day with no records: user missed all 5 obligatory prayers + for p in PROGRESS_PRAYERS: + missed_counts[p] += 1 + + # Process today + today_doc = docs_by_date.get(today_str[:10]) + if today_doc is not None: + raw_prayers = today_doc.get("prayers", []) or [] + norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) + for p in PROGRESS_PRAYERS: + if p in norm_set: + prayed_counts[p] += 1 + + is_full = today_doc.get("is_full") + if is_full is None: + is_full = len(norm_set) == 5 + if is_full: + total_full_days += 1 + + q_secs = int(today_doc.get("quran_secs", 0) or 0) + h_secs = int(today_doc.get("hadith_secs", 0) or 0) + t_count = int(today_doc.get("tasbeeh_count", 0) or 0) + if q_secs >= PROGRESS_GOAL_SECONDS: + total_quran_days += 1 + is_golden = today_doc.get("is_golden") + if is_golden is None: + is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) + if is_golden: + total_golden_days += 1 + + last_active_date = max(valid_doc_dates) if valid_doc_dates else today_str[:10] + + total_prayed = sum(prayed_counts.values()) + total_missed = sum(missed_counts.values()) + + try: + today_date = datetime.date.fromisoformat(today_str) + streak, golden_streak = await _progress_streaks(uid, today_date) + except Exception: + streak, golden_streak = 0, 0 + + # PT-07: Fully reconstruct longest contiguous prayer and golden streaks across complete history + sorted_complete_dates = sorted(list(set( + datetime.date.fromisoformat(d.get("date")) + for d in docs + if d.get("date") and _progress_prayer_streak_day_complete(d) + ))) + max_contiguous_prayer = 0 + current_run = 0 + prev_d = None + for d in sorted_complete_dates: + if prev_d is None or d == prev_d + datetime.timedelta(days=1): + current_run += 1 + else: + current_run = 1 + prev_d = d + if current_run > max_contiguous_prayer: + max_contiguous_prayer = current_run + + sorted_golden_dates = sorted(list(set( + datetime.date.fromisoformat(d.get("date")) + for d in docs + if d.get("date") and _progress_golden_streak_day_complete(d) + ))) + max_contiguous_golden = 0 + g_run = 0 + g_prev_d = None + for d in sorted_golden_dates: + if g_prev_d is None or d == g_prev_d + datetime.timedelta(days=1): + g_run += 1 + else: + g_run = 1 + g_prev_d = d + if g_run > max_contiguous_golden: + max_contiguous_golden = g_run + + existing = {} + try: + existing = await nimaz_stats_collection.find_one({"_id": uid}, {"longest_streak": 1, "longest_golden_streak": 1}) or {} + except Exception: + pass + longest_streak = max(int((existing or {}).get("longest_streak", 0) or 0), max_contiguous_prayer, streak) + longest_golden_streak = max(int((existing or {}).get("longest_golden_streak", 0) or 0), max_contiguous_golden, golden_streak) + + stats = { + "_id": uid, + "uid": uid, + "started_date": effective_started_date, + "evaluated_date": today_str[:10], + "prayed": prayed_counts, + "missed": missed_counts, + "total_prayed": total_prayed, + "total_missed": total_missed, + "total_days": valid_days_count, + "total_full_days": total_full_days, + "total_golden_days": total_golden_days, + "total_quran_days": total_quran_days, + "streak": streak, + "golden_streak": golden_streak, + "longest_streak": longest_streak, + "longest_golden_streak": longest_golden_streak, + "last_active_date": last_active_date or today_str, + "updated_at": datetime.datetime.utcnow().isoformat() + "Z", + } + + try: + await nimaz_stats_collection.update_one( + {"_id": uid}, + {"$set": stats}, + upsert=True + ) + except Exception: + pass + return stats + + +async def _progress_streaks(uid: str, today: datetime.date) -> tuple[int, int]: + """Calculate both normal prayer streak and golden streak. + - Normal streak: consecutive days with all 5 prayers marked. + - Golden streak: consecutive days with all 5 prayers + 10m Quran + (10m Hadith OR 297 Tasbeeh).""" + start = (today - datetime.timedelta(days=366)).isoformat() + # M-10: Exclude future dates from streak query + docs = await daily_progress_collection.find( + {"uid": uid, "date": {"$gte": start, "$lte": today.isoformat()}}, {"_id": 0} + ).to_list(length=400) + + prayer_complete = {d["date"] for d in docs if _progress_prayer_streak_day_complete(d)} + golden_complete = {d["date"] for d in docs if _progress_golden_streak_day_complete(d)} + + # Prayer streak + cursor = today if today.isoformat() in prayer_complete else today - datetime.timedelta(days=1) + prayer_streak = 0 + while cursor.isoformat() in prayer_complete: + prayer_streak += 1 + cursor -= datetime.timedelta(days=1) + + # Golden streak + g_cursor = today if today.isoformat() in golden_complete else today - datetime.timedelta(days=1) + golden_streak = 0 + while g_cursor.isoformat() in golden_complete: + golden_streak += 1 + g_cursor -= datetime.timedelta(days=1) + + return prayer_streak, golden_streak + + +def _progress_payload(doc: dict, date_str: str, streak: int, golden_streak: int = 0, lifetime_apples: int = 0) -> dict: + raw = doc.get("prayers", []) or [] + norm_prayers = [p for p in PROGRESS_PRAYERS if p in set(_normalize_progress_prayer_name(x) for x in raw)] + return { + "date": date_str, + "prayers": norm_prayers, + "quran_secs": int(doc.get("quran_secs", 0) or 0), + "hadith_secs": int(doc.get("hadith_secs", 0) or 0), + "tasbeeh_count": int(doc.get("tasbeeh_count", 0) or 0), + "streak": streak, + "golden_streak": golden_streak, + "goal_seconds": PROGRESS_GOAL_SECONDS, + "goal_tasbeeh": PROGRESS_TASBEEH_GOAL, + "total_goals": PROGRESS_TOTAL_GOALS, + "apples_collected": bool(doc.get("apples_collected", False)), + "apples_collected_count": int(doc.get("apples_collected_count", 0) or 0), + "apples_collected_at": doc.get("apples_collected_at"), + "lifetime_apples": int(lifetime_apples or 0), + } + + +_DAILY_PROGRESS_CACHE: Dict[str, Tuple[float, bytes]] = {} +_DAILY_PROGRESS_TTL = 15.0 + +# _MONTHLY_PROGRESS_CACHE declared above +# _RECORDED_MISSED_CACHE declared above +# _QAZA_STATE_CACHE declared above +# _TASBEEH_SUMMARY_CACHE declared above + +_MONTHLY_PROGRESS_CACHE: Dict[str, Tuple[float, bytes]] = {} +_MONTHLY_PROGRESS_TTL = 30.0 +_RECORDED_MISSED_CACHE: Dict[str, Tuple[float, bytes]] = {} +_RECORDED_MISSED_TTL = 30.0 +_QAZA_STATE_CACHE: Dict[str, Tuple[float, bytes]] = {} +_QAZA_STATE_TTL = 30.0 +_TASBEEH_SUMMARY_CACHE: Dict[str, Tuple[float, bytes]] = {} +_TASBEEH_SUMMARY_TTL = 15.0 + +def _sweep_cache_if_needed(cache_dict: Dict[str, Tuple[float, bytes]], max_size: int = 1000) -> None: + """L-05: Evicts expired entries and caps in-memory cache size to avoid unbounded memory growth.""" + now = time.monotonic() + expired = [k for k, v in list(cache_dict.items()) if (now - v[0]) > 60.0] + for k in expired: + cache_dict.pop(k, None) + if len(cache_dict) > max_size: + sorted_keys = sorted(cache_dict.keys(), key=lambda k: cache_dict[k][0]) + for k in sorted_keys[:max(1, len(sorted_keys) // 5)]: + cache_dict.pop(k, None) + +def _invalidate_all_progress_caches(uid: str) -> None: + """Invalidates all progress, streak, monthly calendar, recorded missed, and qaza caches for a user.""" + prefix = f"{uid}:" + for cache in (_DAILY_PROGRESS_CACHE, _MONTHLY_PROGRESS_CACHE, _RECORDED_MISSED_CACHE, _QAZA_STATE_CACHE, _TASBEEH_SUMMARY_CACHE): + for k in list(cache.keys()): + if k == uid or k.startswith(prefix): + cache.pop(k, None) + +@router.get('/progress') +@limiter.limit("1200/minute") +async def get_progress( + request: Request, + tz: float = 0.0, + fajr: Optional[str] = None, + date: Optional[str] = None, + user: Optional[dict] = Depends(get_optional_user) +): + if isinstance(date, str) and date.strip(): + today_str = date.strip() + try: + today = datetime.date.fromisoformat(today_str) + except Exception: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + today_str = today.isoformat() + else: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + today_str = today.isoformat() + + if not user: + return { + "date": today_str, + "prayers": [], + "quran_secs": 0, + "hadith_secs": 0, + "tasbeeh_count": 0, + "streak": 0, + "golden_streak": 0, + "goal_seconds": PROGRESS_GOAL_SECONDS, + "goal_tasbeeh": PROGRESS_TASBEEH_GOAL, + "total_goals": PROGRESS_TOTAL_GOALS, + "apples_collected": False, + "apples_collected_count": 0, + "apples_collected_at": None, + "lifetime_apples": 0, + } + uid = user["uid"] + date_str = today.isoformat() + cache_key = f"{uid}:{date_str}" + ent = _DAILY_PROGRESS_CACHE.get(cache_key) + if ent and (time.monotonic() - ent[0]) < _DAILY_PROGRESS_TTL: + return Response(content=ent[1], media_type="application/json") + + doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} + + # Self-healing fallback: check tasbeeh_daily / tasbeeh_collection so tasbeeh deeds never reset to zero + t_count = int(doc.get("tasbeeh_count", 0) or 0) + try: + t_daily = await tasbeeh_daily_collection.find_one({"_id": f"{uid}:{date_str}"}, {"total_count": 1}) + if t_daily: + sum_sessions = int(t_daily.get("total_count", 0) or 0) + else: + sessions = await tasbeeh_collection.find({"uid": uid, "date": date_str}, {"_id": 0, "count": 1}).to_list(length=300) + sum_sessions = sum(int(s.get("count", 0) or 0) for s in sessions) if sessions else 0 + if sum_sessions > t_count: + t_count = sum_sessions + doc["tasbeeh_count"] = t_count + await daily_progress_collection.update_one( + {"uid": uid, "date": date_str}, + {"$set": {"tasbeeh_count": t_count}, "$setOnInsert": {"uid": uid, "date": date_str}}, + upsert=True, + ) + except Exception: + pass + + user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) + lifetime_apples = 0 + if user_doc: + lifetime_apples = int(user_doc.get("lifetime_apples") or (user_doc.get("metrics") or {}).get("lifetime_apples", 0) or 0) + elif user: + lifetime_apples = int(user.get("lifetime_apples") or (user.get("metrics") or {}).get("lifetime_apples", 0) or 0) + + streak, golden_streak = await _progress_streaks(uid, today) + res = _progress_payload(doc, date_str, streak, golden_streak, lifetime_apples=lifetime_apples) + raw_bytes = json.dumps(res).encode("utf-8") + _sweep_cache_if_needed(_DAILY_PROGRESS_CACHE) + _DAILY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) + return Response(content=raw_bytes, media_type="application/json") + + +@router.post('/progress/prayer') +@limiter.limit("120/minute") +async def toggle_progress_prayer(payload: ProgressPrayerReq, request: Request, user: dict = Depends(get_current_user)): + target_prayer = _normalize_progress_prayer_name(payload.prayer) + if not target_prayer: + raise HTTPException(status_code=400, detail=f"Unknown prayer: {payload.prayer}") + uid = user["uid"] + today = _progress_local_date(payload.tz, payload.fajr) + today_str = today.isoformat() + + # M-05: Bound write dates + date_str = _validate_progress_date(payload.date, payload.tz, payload.fajr, max_past_days=30) + cache_key = f"{uid}:{date_str}" + + if payload.done is not None: + target_done = bool(payload.done) + else: + existing_doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"prayers": 1, "_id": 0}) or {} + existing_prayers = [str(x) for x in ((existing_doc or {}).get("prayers", []) or [])] + existing_norm_set = set(_normalize_progress_prayer_name(p) for p in existing_prayers if _normalize_progress_prayer_name(p)) + target_done = not (target_prayer in existing_norm_set) + + # ATOMIC MONGODB UPDATE returning ReturnDocument.BEFORE to detect atomic state transition (PT-05) + if target_done: + update_op = { + "$addToSet": {"prayers": target_prayer}, + "$setOnInsert": {"uid": uid, "date": date_str, "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0} + } + else: + update_op = { + "$pull": {"prayers": target_prayer}, + "$setOnInsert": {"uid": uid, "date": date_str, "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0} + } + + before_doc = await daily_progress_collection.find_one_and_update( + {"uid": uid, "date": date_str}, + update_op, + upsert=True, + return_document=ReturnDocument.BEFORE, + projection={"_id": 0} + ) + + if before_doc is None: + was_done = False + raw_prayers_before = [] + base_doc = {} + else: + raw_prayers_before = before_doc.get("prayers", []) or [] + before_norm_set = set(_normalize_progress_prayer_name(str(p)) for p in raw_prayers_before if _normalize_progress_prayer_name(str(p))) + was_done = target_prayer in before_norm_set + base_doc = before_doc + + state_changed = (was_done != target_done) + + # Derive updated_doc accurately without a second database read + updated_norm_set = set(_normalize_progress_prayer_name(str(p)) for p in raw_prayers_before if _normalize_progress_prayer_name(str(p))) + if target_done: + updated_norm_set.add(target_prayer) + else: + updated_norm_set.discard(target_prayer) + + ordered = [p for p in PROGRESS_PRAYERS if p in updated_norm_set] + completed_count = len(ordered) + is_full = completed_count == 5 + q_secs = int(base_doc.get("quran_secs", 0) or 0) + h_secs = int(base_doc.get("hadith_secs", 0) or 0) + t_count = int(base_doc.get("tasbeeh_count", 0) or 0) + is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) + now_iso = datetime.datetime.utcnow().isoformat() + "Z" + + # Persist metadata; canonical order is maintained in response payload + set_fields = { + "completed_count": completed_count, + "is_full": is_full, + "is_golden": is_golden, + "updated_at": now_iso, + } + + try: + await daily_progress_collection.update_one( + {"uid": uid, "date": date_str}, + {"$set": set_fields} + ) + except Exception as e: + logger.warning(f"Metadata update error: {e}") + + updated_doc = { + **base_doc, + "uid": uid, + "date": date_str, + "prayers": ordered, + "completed_count": completed_count, + "is_full": is_full, + "is_golden": is_golden, + "updated_at": now_iso, + } + + # M-01: Synchronize nimaz_stats_collection atomically ONLY when state actually changed + if state_changed: + try: + existing_stats = None + stats_lookup_ok = False + try: + existing_stats = await nimaz_stats_collection.find_one({"_id": uid}, {"evaluated_date": 1}) + stats_lookup_ok = True + except Exception: + stats_lookup_ok = False + + needs_rollover_recalc = ( + date_str != today_str + or (stats_lookup_ok and (not existing_stats or existing_stats.get("evaluated_date") != today_str)) + ) + if needs_rollover_recalc: + # Full recalculation for historical edits or new prayer-day rollover guarantees 100% precision + await _recalculate_nimaz_stats_for_user(uid, today_str) + else: + inc_ops = { + f"prayed.{target_prayer}": 1 if target_done else -1, + "total_prayed": 1 if target_done else -1, + } + await nimaz_stats_collection.update_one( + {"_id": uid}, + { + "$inc": inc_ops, + "$set": { + "uid": uid, + "evaluated_date": today_str, + "last_active_date": date_str, + "updated_at": now_iso, + }, + }, + upsert=True + ) + except Exception: + pass + + streak, golden_streak = await _progress_streaks(uid, today) + try: + await nimaz_stats_collection.update_one( + {"_id": uid}, + { + "$set": {"streak": streak, "golden_streak": golden_streak}, + "$max": {"longest_streak": streak, "longest_golden_streak": golden_streak} + } + ) + except Exception: + pass + + user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) + lifetime_apples = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or (user or {}).get("lifetime_apples", 0) or 0) + res = _progress_payload(updated_doc, date_str, streak, golden_streak, lifetime_apples=lifetime_apples) + raw_bytes = json.dumps(res).encode("utf-8") + + # Invalidate monthly/recorded/qaza caches and populate fresh daily cache + _invalidate_all_progress_caches(uid) + _sweep_cache_if_needed(_DAILY_PROGRESS_CACHE) + _DAILY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) + + # Notify client devices via real-time bus + realtime.publish(uid, "prayer_progress_updated", {"date": date_str, "prayers": ordered}) + return Response(content=raw_bytes, media_type="application/json") + + +@router.post('/set-password-status') +async def set_password_status(payload: SetPasswordRequest, user: dict = Depends(get_current_user)): + await users_collection.update_one( + {"uid": user["uid"]}, + {"$set": {"has_password": payload.has_password}} + ) + await invalidate_user_cache(user["uid"]) + return {"status": "ok", "has_password": payload.has_password} + + +@router.post('/progress/time') +@limiter.limit("120/minute") +async def add_progress_time(payload: ProgressTimeReq, request: Request, user: dict = Depends(get_current_user)): + uid = user["uid"] + # M-05: Strict date validation and bounds checking (reject future dates) + date_str = _validate_progress_date(payload.date, payload.tz, payload.fajr, max_past_days=7) + today = datetime.date.fromisoformat(date_str) + field = "quran_secs" if payload.category == "quran" else "hadith_secs" + event_id = (payload.event_id or "").strip() or None + now_iso = datetime.datetime.utcnow().isoformat() + "Z" + + if payload.seconds <= 0: + doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} + streak, golden_streak = await _progress_streaks(uid, today) + return _progress_payload(doc, date_str, streak, golden_streak) + + # DATA-004: Atomic, capped, idempotent increment (no read-modify-write race or duplicate replay) + doc = None + if event_id: + try: + doc = await daily_progress_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "applied_time_events": {"$ne": event_id}, + }, + { + "$inc": {field: payload.seconds}, + "$push": {"applied_time_events": {"$each": [event_id], "$slice": -300}}, + "$set": {"updated_at": now_iso}, + "$setOnInsert": { + "_id": f"{uid}:{date_str}", + "uid": uid, + "date": date_str, + "prayers": [], + }, + }, + upsert=True, + return_document=ReturnDocument.AFTER, + ) + except DuplicateKeyError: + # (uid, date_str) doc already exists and already applied this event_id + doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} + else: + doc = await daily_progress_collection.find_one_and_update( + {"uid": uid, "date": date_str}, + { + "$inc": {field: payload.seconds}, + "$set": {"updated_at": now_iso}, + "$setOnInsert": { + "_id": f"{uid}:{date_str}", + "uid": uid, + "date": date_str, + "prayers": [], + }, + }, + upsert=True, + return_document=ReturnDocument.AFTER, + ) + + doc = dict(doc or {}) + doc.pop("_id", None) + + raw_val = int(doc.get(field, 0) or 0) + if raw_val > PROGRESS_GOAL_SECONDS: + clamped = await daily_progress_collection.find_one_and_update( + {"uid": uid, "date": date_str, field: {"$gt": PROGRESS_GOAL_SECONDS}}, + {"$min": {field: PROGRESS_GOAL_SECONDS}}, + return_document=ReturnDocument.AFTER, + ) + if clamped: + doc = dict(clamped) + doc.pop("_id", None) + doc[field] = min(int(doc.get(field, raw_val) or 0), PROGRESS_GOAL_SECONDS) + + nxt = int(doc.get(field, 0) or 0) + raw_prayers = doc.get("prayers", []) or [] + norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) + is_full = len(norm_set) == 5 + q_secs = min(int(doc.get("quran_secs", 0) or 0), PROGRESS_GOAL_SECONDS) + h_secs = min(int(doc.get("hadith_secs", 0) or 0), PROGRESS_GOAL_SECONDS) + t_count = int(doc.get("tasbeeh_count", 0) or 0) + is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) + + await daily_progress_collection.update_one( + {"uid": uid, "date": date_str}, + { + "$set": { + "is_golden": is_golden, + "is_full": is_full, + "completed_count": len(norm_set), + "updated_at": now_iso, + } + }, + ) + doc["is_golden"] = is_golden + doc["is_full"] = is_full + + streak, golden_streak = await _progress_streaks(uid, today) + try: + await nimaz_stats_collection.update_one( + {"_id": uid}, + { + "$set": {"streak": streak, "golden_streak": golden_streak}, + "$max": {"longest_streak": streak, "longest_golden_streak": golden_streak} + } + ) + except Exception: + pass + + user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) + lifetime_apples = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or (user or {}).get("lifetime_apples", 0) or 0) + res = _progress_payload(doc, date_str, streak, golden_streak, lifetime_apples=lifetime_apples) + raw_bytes = json.dumps(res).encode("utf-8") + _invalidate_all_progress_caches(uid) + _sweep_cache_if_needed(_DAILY_PROGRESS_CACHE) + _DAILY_PROGRESS_CACHE[f"{uid}:{date_str}"] = (time.monotonic(), raw_bytes) + + # Broadcast realtime event so connected devices / homepage update instantly + realtime.publish(uid, "prayer_progress_updated", { + "date": date_str, + field: nxt, + }) + return Response(content=raw_bytes, media_type="application/json") + + +class PrayerSyncEvent(BaseModel): + event_id: Optional[str] = None + prayer: str + done: bool + date: str = Field(..., pattern=r"^\d{4}-\d{2}-\d{2}$") + created_at: Optional[str] = None + + +class DailyProgressBatchItem(BaseModel): + date: str = Field(..., pattern=r"^\d{4}-\d{2}-\d{2}$") + prayers: Optional[List[str]] = Field(default_factory=list) + quran_secs: Optional[int] = Field(default=None, ge=0, le=3600) + hadith_secs: Optional[int] = Field(default=None, ge=0, le=3600) + tasbeeh_count: Optional[int] = Field(default=None, ge=0) + + +class ProgressBatchSyncReq(BaseModel): + events: Optional[List[PrayerSyncEvent]] = Field(default=None, max_length=120) + updates: Optional[List[DailyProgressBatchItem]] = Field(default=None, max_length=60) + tz: float = Field(default=0.0, ge=-12, le=14) + fajr: Optional[str] = Field(default=None) + + +@router.post('/progress/batch-sync') +@limiter.limit("60/minute") +async def batch_sync_progress(payload: ProgressBatchSyncReq, request: Request, user: dict = Depends(get_current_user)): + """Idempotently batch-syncs offline prayer events and spiritual progress logs (PT-01 & PT-03). + - Applies per-prayer atomic updates ($addToSet / $pull) to never overwrite unrelated prayers. + - Returns structured per-event and per-date results with explicit status (accepted/skipped/rejected). + """ + uid = user["uid"] + today = _progress_local_date(payload.tz, payload.fajr) + today_str = today.isoformat() + now_iso = datetime.datetime.utcnow().isoformat() + "Z" + + event_results = [] + date_results = [] + + # 1. Process per-prayer desired state events (PT-01 & PT-03) + if payload.events: + for ev in payload.events: + ev_id = ev.event_id or f"{ev.date}_{ev.prayer}_{now_iso}" + try: + parsed_d = datetime.date.fromisoformat(ev.date) + if parsed_d > today: + event_results.append({"event_id": ev_id, "date": ev.date, "prayer": ev.prayer, "status": "skipped", "reason": "future_date"}) + continue + if (today - parsed_d).days > 30: + event_results.append({"event_id": ev_id, "date": ev.date, "prayer": ev.prayer, "status": "skipped", "reason": "date_too_old"}) + continue + d_str = parsed_d.isoformat() + except Exception: + event_results.append({"event_id": ev_id, "date": ev.date, "prayer": ev.prayer, "status": "skipped", "reason": "invalid_date"}) + continue + + target_p = _normalize_progress_prayer_name(ev.prayer) + if not target_p: + event_results.append({"event_id": ev_id, "date": d_str, "prayer": ev.prayer, "status": "rejected", "reason": "unknown_prayer"}) + continue + + # Per-prayer atomic mutation ensures no other prayers are overwritten (PT-01) + if ev.done: + op = { + "$addToSet": {"prayers": target_p}, + "$setOnInsert": {"_id": f"{uid}:{d_str}", "uid": uid, "date": d_str, "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0} + } + else: + op = { + "$pull": {"prayers": target_p}, + "$setOnInsert": {"_id": f"{uid}:{d_str}", "uid": uid, "date": d_str, "quran_secs": 0, "hadith_secs": 0, "tasbeeh_count": 0} + } + + await daily_progress_collection.update_one({"uid": uid, "date": d_str}, op, upsert=True) + + # Re-read or update completion metadata + doc_after = await daily_progress_collection.find_one({"uid": uid, "date": d_str}, {"_id": 0}) or {} + raw_p = doc_after.get("prayers", []) or [] + norm_s = set(_normalize_progress_prayer_name(x) for x in raw_p if _normalize_progress_prayer_name(x)) + is_full = len(norm_s) == 5 + q_s = int(doc_after.get("quran_secs", 0) or 0) + h_s = int(doc_after.get("hadith_secs", 0) or 0) + t_c = int(doc_after.get("tasbeeh_count", 0) or 0) + is_golden = is_full and (q_s >= PROGRESS_GOAL_SECONDS) and (h_s >= PROGRESS_GOAL_SECONDS or t_c >= PROGRESS_TASBEEH_GOAL) + await daily_progress_collection.update_one( + {"uid": uid, "date": d_str}, + {"$set": { + "completed_count": len(norm_s), + "is_full": is_full, + "is_golden": is_golden, + "updated_at": now_iso, + }} + ) + + event_results.append({"event_id": ev_id, "date": d_str, "prayer": target_p, "status": "accepted"}) + + # 2. Process legacy updates (full snapshots & time) for backwards compatibility + if payload.updates: + for item in payload.updates: + try: + parsed_d = datetime.date.fromisoformat(item.date) + if parsed_d > today: + date_results.append({"date": item.date, "status": "skipped", "reason": "future_date"}) + continue + if (today - parsed_d).days > 30: + date_results.append({"date": item.date, "status": "skipped", "reason": "date_too_old"}) + continue + d_str = parsed_d.isoformat() + except Exception: + date_results.append({"date": item.date, "status": "skipped", "reason": "invalid_date"}) + continue + + existing = await daily_progress_collection.find_one({"uid": uid, "date": d_str}, {"_id": 0}) or {} + update_set: dict = {"updated_at": now_iso} + + # If prayer events were NOT used and only prayers snapshot was provided: + if item.prayers is not None and len(item.prayers) > 0: + norm_set = set(_normalize_progress_prayer_name(p) for p in item.prayers if _normalize_progress_prayer_name(p)) + ordered = [p for p in PROGRESS_PRAYERS if p in norm_set] + completed_count = len(ordered) + is_full = completed_count == 5 + update_set["prayers"] = ordered + update_set["completed_count"] = completed_count + update_set["is_full"] = is_full + else: + raw_p = existing.get("prayers", []) or [] + norm_set = set(_normalize_progress_prayer_name(p) for p in raw_p if _normalize_progress_prayer_name(p)) + is_full = len(norm_set) == 5 + + if item.quran_secs is not None: + update_set["quran_secs"] = item.quran_secs + if item.hadith_secs is not None: + update_set["hadith_secs"] = item.hadith_secs + if item.tasbeeh_count is not None: + update_set["tasbeeh_count"] = item.tasbeeh_count + + q_secs = update_set.get("quran_secs", existing.get("quran_secs", 0)) or 0 + h_secs = update_set.get("hadith_secs", existing.get("hadith_secs", 0)) or 0 + t_count = update_set.get("tasbeeh_count", existing.get("tasbeeh_count", 0)) or 0 + update_set["is_golden"] = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) + + await daily_progress_collection.update_one( + {"uid": uid, "date": d_str}, + { + "$set": update_set, + "$setOnInsert": { + "_id": f"{uid}:{d_str}", + "uid": uid, + "date": d_str, + } + }, + upsert=True + ) + date_results.append({"date": d_str, "status": "accepted"}) + + # Recalculate lifetime stats & streaks + stats = await _recalculate_nimaz_stats_for_user(uid, today_str) + _invalidate_all_progress_caches(uid) + + today_doc = await daily_progress_collection.find_one({"uid": uid, "date": today_str}, {"_id": 0}) or {} + user_doc = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"lifetime_apples": 1, "metrics.lifetime_apples": 1}) + lifetime_apples = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or (user or {}).get("lifetime_apples", 0) or 0) + res = _progress_payload(today_doc, today_str, stats.get("streak", 0), stats.get("golden_streak", 0), lifetime_apples=lifetime_apples) + + # Publish realtime update so all connected user devices refresh + realtime.publish(uid, "prayer_progress_updated", {"date": today_str}) + + res["status"] = "ok" + res["results"] = event_results + res["date_results"] = date_results + return res + + +@router.post('/progress/harvest-apples') +@limiter.limit("60/minute") +async def harvest_daily_apples(payload: HarvestApplesReq, request: Request, user: dict = Depends(get_current_user)): + """ + Harvests 10 apples for the user when all 7 daily deeds are completed. + - Validates server-authoritative progress: user completed all 7 deeds (H-01). + - Rejects client-fabricated claims (H-01). + - Strictly limits eligible harvest date to current or immediately preceding prayer date (H-01, M-05). + - Idempotent: if already harvested today, returns current state with status 'already_harvested'. + - Atomically sets apples_collected: True on daily_progress_collection (M-11). + - Atomically increments lifetime_apples by +10 in users_collection & nimaz_stats_collection. + - Apples never expire and accumulate across a user's lifetime. + """ + uid = user["uid"] + today = _progress_local_date(payload.tz, payload.fajr) + today_str = today.isoformat() + yesterday_str = (today - datetime.timedelta(days=1)).isoformat() + + if payload.date and payload.date.strip(): + req_date = payload.date.strip() + try: + req_d = datetime.date.fromisoformat(req_date) + except Exception: + raise HTTPException(status_code=400, detail=f"Invalid date format: {req_date}. Expected YYYY-MM-DD.") + if req_date > today_str: + raise HTTPException(status_code=400, detail="Cannot harvest apples for a future date.") + if req_date < yesterday_str: + raise HTTPException(status_code=400, detail="Apple harvesting is strictly allowed only for the current or immediately preceding prayer date.") + date_str = req_date + else: + date_str = today_str + + cache_key = f"{uid}:{date_str}" + + # H-01: Authoritatively load server-owned record; do NOT trust client claims + doc = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} + + # Self-healing fallback: reconcile server-verified tasbeeh sessions + t_count = int(doc.get("tasbeeh_count", 0) or 0) + try: + t_daily = await tasbeeh_daily_collection.find_one({"_id": f"{uid}:{date_str}"}, {"total_count": 1}) + if t_daily: + sum_sessions = int(t_daily.get("total_count", 0) or 0) + else: + sessions = await tasbeeh_collection.find({"uid": uid, "date": date_str}, {"_id": 0, "count": 1}).to_list(length=300) + sum_sessions = sum(int(s.get("count", 0) or 0) for s in sessions) if sessions else 0 + if sum_sessions > t_count: + t_count = sum_sessions + doc["tasbeeh_count"] = t_count + await daily_progress_collection.update_one( + {"uid": uid, "date": date_str}, + {"$set": {"tasbeeh_count": t_count}} + ) + except Exception: + pass + + completed_deeds = _progress_deeds_count(doc) + if completed_deeds < PROGRESS_TOTAL_GOALS: + raise HTTPException( + status_code=400, + detail=f"Cannot harvest apples: {completed_deeds}/{PROGRESS_TOTAL_GOALS} deeds completed. All 7 deeds must be verified on the server." + ) + + # Fetch current lifetime apples (matching by either uid or _id) + user_doc = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"lifetime_apples": 1, "metrics.lifetime_apples": 1} + ) + current_lifetime = int((user_doc or {}).get("lifetime_apples") or ((user_doc or {}).get("metrics") or {}).get("lifetime_apples", 0) or 0) + + streak, golden_streak = await _progress_streaks(uid, today) + + # Idempotent check: completed or legacy docs are already harvested; incomplete CLAIMED/USER_CREDITED docs resume recovery + harvest_state = doc.get("apples_harvest_state") + is_resuming_incomplete = bool(doc.get("apples_collected", False)) and harvest_state in ("CLAIMED", "USER_CREDITED") + if bool(doc.get("apples_collected", False)) and not is_resuming_incomplete: + payload_res = _progress_payload(doc, date_str, streak, golden_streak, lifetime_apples=current_lifetime) + return { + "status": "already_harvested", + "message": "10 apples already harvested today.", + "apples_harvested": 0, + "apples_collected_count": int(doc.get("apples_collected_count", 10)), + "lifetime_apples": current_lifetime, + "progress": payload_res, + } + + now_dt = datetime.datetime.utcnow() + now_iso = now_dt.isoformat() + "Z" + lease_until_iso = (now_dt + datetime.timedelta(seconds=15)).isoformat() + "Z" + + if is_resuming_incomplete: + dp_update = await daily_progress_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "apples_harvest_state": {"$in": ["CLAIMED", "USER_CREDITED"]}, + "$or": [ + {"apples_harvest_lease_until": {"$exists": False}}, + {"apples_harvest_lease_until": None}, + {"apples_harvest_lease_until": {"$lt": now_iso}}, + ], + }, + { + "$set": { + "apples_harvest_lease_until": lease_until_iso, + "updated_at": now_iso, + } + }, + return_document=ReturnDocument.AFTER, + ) + if dp_update is None: + raise HTTPException( + status_code=503, + detail="Apple harvest is currently in progress. Please retry in a moment.", + ) + else: + # M-11 / DATA-002: Atomically claim daily_progress record in CLAIMED state before downstream credits + dp_update = await daily_progress_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "$or": [ + {"apples_collected": {"$ne": True}}, + {"apples_collected": {"$exists": False}}, + ] + }, + { + "$set": { + "apples_collected": True, + "apples_collected_count": 10, + "apples_collected_at": now_iso, + "apples_harvest_state": "CLAIMED", + "apples_user_credited": False, + "apples_stats_credited": False, + "apples_harvest_lease_until": lease_until_iso, + "updated_at": now_iso, + }, + "$setOnInsert": { + "_id": f"{uid}:{date_str}", + "uid": uid, + "date": date_str, + } + }, + upsert=False, + return_document=ReturnDocument.AFTER, + ) + + # If atomic claim didn't match, distinguish concurrent COMPLETED vs in-flight/crashed CLAIMED + if dp_update is None: + fresh_dp = await daily_progress_collection.find_one({"uid": uid, "date": date_str}, {"_id": 0}) or {} + if fresh_dp.get("apples_harvest_state") in ("CLAIMED", "USER_CREDITED"): + raise HTTPException( + status_code=503, + detail="Apple harvest is currently being processed. Please retry shortly.", + ) + user_doc_fresh = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"lifetime_apples": 1, "metrics.lifetime_apples": 1} + ) + fresh_lifetime = int((user_doc_fresh or {}).get("lifetime_apples") or ((user_doc_fresh or {}).get("metrics") or {}).get("lifetime_apples", 0) or current_lifetime) + payload_res = _progress_payload(fresh_dp, date_str, streak, golden_streak, lifetime_apples=fresh_lifetime) + return { + "status": "already_harvested", + "message": "10 apples already harvested today.", + "apples_harvested": 0, + "apples_collected_count": int(fresh_dp.get("apples_collected_count", 10)), + "lifetime_apples": fresh_lifetime, + "progress": payload_res, + } + + user_credited = bool(dp_update.get("apples_user_credited", False)) + stats_credited = bool(dp_update.get("apples_stats_credited", False)) + new_lifetime = current_lifetime + + try: + if not user_credited: + user_update = None + if user_doc is not None: + user_update = await users_collection.find_one_and_update( + { + "$or": [{"uid": uid}, {"_id": uid}], + "harvested_apple_dates": {"$ne": date_str}, + }, + { + "$inc": { + "lifetime_apples": 10, + "metrics.lifetime_apples": 10, + }, + "$addToSet": {"harvested_apple_dates": date_str}, + "$set": { + "uid": uid, + "updated_at": datetime.datetime.utcnow(), + }, + }, + upsert=False, + return_document=ReturnDocument.AFTER, + ) + if user_update is None: + # Previously credited before marker persisted; read current balance without double-crediting + user_update = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"lifetime_apples": 1, "metrics.lifetime_apples": 1}, + ) + else: + try: + user_update = await users_collection.find_one_and_update( + {"_id": uid, "harvested_apple_dates": {"$ne": date_str}}, + { + "$inc": { + "lifetime_apples": 10, + "metrics.lifetime_apples": 10, + }, + "$addToSet": {"harvested_apple_dates": date_str}, + "$set": { + "uid": uid, + "updated_at": datetime.datetime.utcnow(), + }, + }, + upsert=True, + return_document=ReturnDocument.AFTER, + ) + except DuplicateKeyError: + user_update = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"lifetime_apples": 1, "metrics.lifetime_apples": 1}, + ) + + if user_update: + new_lifetime = int(user_update.get("lifetime_apples") or (user_update.get("metrics") or {}).get("lifetime_apples", 0) or current_lifetime) + else: + new_lifetime = current_lifetime + 10 + + dp_update = await daily_progress_collection.find_one_and_update( + {"uid": uid, "date": date_str}, + { + "$set": { + "apples_user_credited": True, + "apples_harvest_state": "USER_CREDITED", + "updated_at": now_iso, + } + }, + return_document=ReturnDocument.AFTER, + ) or dp_update + + if not stats_credited: + try: + await nimaz_stats_collection.update_one( + {"_id": uid, "harvested_apple_dates": {"$ne": date_str}}, + { + "$inc": {"lifetime_apples": 10}, + "$addToSet": {"harvested_apple_dates": date_str}, + "$set": {"updated_at": now_iso}, + }, + upsert=True, + ) + except DuplicateKeyError: + # Already credited in nimaz_stats for this date; treat as idempotently satisfied + pass + + final_dp = await daily_progress_collection.find_one_and_update( + {"uid": uid, "date": date_str}, + { + "$set": { + "apples_user_credited": True, + "apples_stats_credited": True, + "apples_harvest_state": "COMPLETED", + "apples_harvest_lease_until": None, + "updated_at": datetime.datetime.utcnow().isoformat() + "Z", + } + }, + return_document=ReturnDocument.AFTER, + ) + if final_dp: + dp_update = final_dp + except HTTPException: + raise + except Exception as harvest_err: + logger.warning(f"Apple harvest partial failure for {uid} on {date_str}: {harvest_err}") + try: + await daily_progress_collection.find_one_and_update( + {"uid": uid, "date": date_str}, + {"$set": {"apples_harvest_lease_until": None}}, + return_document=ReturnDocument.AFTER, + ) + except Exception: + pass + raise HTTPException( + status_code=503, + detail="Failed to finalize apple harvest across storage. Please retry shortly.", + ) + + await invalidate_user_cache(uid) + _invalidate_all_progress_caches(uid) + + dp_doc = dict(dp_update) + dp_doc.pop("_id", None) + dp_doc["apples_collected"] = True + dp_doc["apples_collected_count"] = 10 + + payload_res = _progress_payload(dp_doc, date_str, streak, golden_streak, lifetime_apples=new_lifetime) + raw_bytes = json.dumps(payload_res).encode("utf-8") + _sweep_cache_if_needed(_DAILY_PROGRESS_CACHE) + _DAILY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) + + # Broadcast realtime event + realtime.publish(uid, "apple_harvest_completed", { + "date": date_str, + "apples_collected": True, + "apples_harvested": 10, + "lifetime_apples": new_lifetime, + }) + + return { + "status": "success", + "message": "Masha'Allah! You harvested 10 apples into your Barakah Basket.", + "apples_harvested": 10, + "apples_collected_count": 10, + "lifetime_apples": new_lifetime, + "progress": payload_res, + } + + +# ========================================================= +# 11. LIFETIME QAZA TRACKER & MONTHLY SALAH ANALYTICS +# ========================================================= + +class QazaSetupReq(BaseModel): + fajr: int = Field(0, ge=0) + dhuhr: int = Field(0, ge=0) + asr: int = Field(0, ge=0) + maghrib: int = Field(0, ge=0) + isha: int = Field(0, ge=0) + witr: int = Field(0, ge=0) + include_witr: bool = True + daily_target: int = Field(1, ge=1, le=50) + tz: Optional[float] = Field(None, ge=-12, le=14) + fajr_time: Optional[str] = Field(None, max_length=20) + date: Optional[str] = Field(None, max_length=15) + + +class QazaLogReq(BaseModel): + prayer: str = Field(..., pattern="^(fajr|dhuhr|asr|maghrib|isha|witr)$") + count: int = Field(1, ge=-10, le=10) + event_id: Optional[str] = Field(None, max_length=64) + date: Optional[str] = Field(None, max_length=15) + tz: Optional[float] = Field(None, ge=-12, le=14) + fajr_time: Optional[str] = Field(None, max_length=20) + + +async def _resolve_qaza_tz(uid: str, explicit_tz: Optional[float]) -> float: + if explicit_tz is not None: + return float(explicit_tz) + try: + u = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"profile.timezone": 1, "timezone": 1} + ) + tz_name = ((u or {}).get("profile") or {}).get("timezone") or (u or {}).get("timezone") + if isinstance(tz_name, str) and tz_name.strip(): + import pytz + tz_obj = pytz.timezone(tz_name.strip()) + offset = datetime.datetime.now(tz_obj).utcoffset() + if offset is not None: + return round(offset.total_seconds() / 3600.0, 2) + except Exception: + pass + return 0.0 + + +def _resolve_qaza_today_date(tz: float, fajr: Optional[str] = None, date_str: Optional[str] = None) -> datetime.date: + local_today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + if isinstance(date_str, str) and date_str.strip(): + try: + parsed = datetime.date.fromisoformat(date_str.strip()[:10]) + if abs((parsed - local_today).days) <= 1: + return parsed + except Exception: + pass + return local_today + + +async def _calculate_dynamic_qaza_state( + uid: str, + tz: float = 0.0, + fajr: Optional[str] = None, + date: Optional[str] = None, +): + today = _resolve_qaza_today_date(tz, fajr, date) + today_str = today.isoformat() + + # 1. Read lifetime stats in O(1); automatically recalculate when missing or rolled to a new prayer day + stats = None + try: + stats = await nimaz_stats_collection.find_one({"_id": uid}, {"_id": 0}) + except Exception: + pass + if not stats or stats.get("evaluated_date") != today_str: + stats = await _recalculate_nimaz_stats_for_user(uid, today_str) + + hist_missed = (stats or {}).get("missed", {}) or {} + missed_counts = { + "fajr": int(hist_missed.get("Fajr", 0) or 0), + "dhuhr": int(hist_missed.get("Dhuhr", 0) or 0), + "asr": int(hist_missed.get("Asr", 0) or 0), + "maghrib": int(hist_missed.get("Maghrib", 0) or 0), + "isha": int(hist_missed.get("Isha", 0) or 0), + } + + # 2. Check today's daily progress in O(1) + today_doc = None + try: + today_doc = await daily_progress_collection.find_one({"uid": uid, "date": today_str}, {"_id": 0, "prayers": 1}) + except Exception: + pass + + # Even if today_doc has not been inserted yet, unprayed prayers past expiration time count as missed + today_raw = (today_doc.get("prayers", []) if today_doc else []) or [] + today_prayed_set = set(_normalize_progress_prayer_name(p).lower() for p in today_raw if _normalize_progress_prayer_name(p)) + now_utc = datetime.datetime.now(datetime.timezone.utc) + local_hour = (now_utc.hour + tz + (now_utc.minute / 60.0)) % 24.0 + for p in ["fajr", "dhuhr", "asr", "maghrib", "isha"]: + if p not in today_prayed_set: + expired = False + if p == "fajr" and local_hour >= 7.0: # Expired at sunrise + expired = True + elif p == "dhuhr" and local_hour >= 16.0: # Expired at Asr azan + expired = True + elif p == "asr" and local_hour >= 18.0: # Expired at Maghrib azan + expired = True + elif p == "maghrib" and local_hour >= 19.5: # Expired at Isha azan + expired = True + if expired: + missed_counts[p] += 1 + + # 3. Fetch logged makeups and baseline from qaza_collection in O(1) + q_doc = await qaza_collection.find_one({"$or": [{"_id": uid}, {"uid": uid}]}) or {} + makeups = q_doc.get("makeups") or {} + completed_total = 0 + + pending = {} + total_baseline = 0 + for p in ["fajr", "dhuhr", "asr", "maghrib", "isha"]: + base_p = int(q_doc.get(f"baseline_{p}", 0) or 0) + total_baseline += base_p + done_p = int(makeups.get(p, 0) or q_doc.get(f"completed_{p}", 0) or 0) + completed_total += done_p + pending[p] = max(0, base_p + missed_counts[p] - done_p) + + witr_base = int(q_doc.get("baseline_witr", 0) or 0) + witr_done = int(makeups.get("witr", 0) or q_doc.get("completed_witr", 0) or 0) + pending["witr"] = max(0, witr_base - witr_done) + completed_total += witr_done + total_baseline += witr_base + + initial_total = total_baseline + sum(missed_counts.values()) + remaining_total = sum(pending[p] for p in ["fajr", "dhuhr", "asr", "maghrib", "isha"]) + if q_doc.get("include_witr", False): + remaining_total += pending["witr"] + + return { + "_id": uid, + "uid": uid, + "fajr": pending["fajr"], + "dhuhr": pending["dhuhr"], + "asr": pending["asr"], + "maghrib": pending["maghrib"], + "isha": pending["isha"], + "witr": pending["witr"], + "initial_total": initial_total, + "completed_total": completed_total, + "remaining_total": remaining_total, + "include_witr": q_doc.get("include_witr", False), + "daily_target": q_doc.get("daily_target", 1), + "is_setup": bool(q_doc.get("is_setup", False)) or total_baseline > 0, + "missed_from_progress": missed_counts, + "started_date": (stats or {}).get("started_date"), + "makeups": {p: int(makeups.get(p, 0) or 0) for p in ["fajr", "dhuhr", "asr", "maghrib", "isha", "witr"]}, + "updated_at": datetime.datetime.utcnow().isoformat() + "Z", + } + + +@router.get('/qaza') +@limiter.limit("1200/minute") +async def get_qaza( + request: Request, + tz: Optional[float] = None, + fajr: Optional[str] = None, + date: Optional[str] = None, + user: dict = Depends(get_current_user), +): + uid = user["uid"] + eff_tz = await _resolve_qaza_tz(uid, tz) + today = _resolve_qaza_today_date(eff_tz, fajr, date) + today_str = today.isoformat() + cache_key = f"{uid}:{eff_tz}:{today_str}" + ent = _QAZA_STATE_CACHE.get(cache_key) + if ent and (time.monotonic() - ent[0]) < _QAZA_STATE_TTL: + return Response(content=ent[1], media_type="application/json") + + res = await _calculate_dynamic_qaza_state(uid, eff_tz, fajr=fajr, date=today_str) + raw_bytes = json.dumps(res, default=str).encode("utf-8") + _sweep_cache_if_needed(_QAZA_STATE_CACHE) + _QAZA_STATE_CACHE[cache_key] = (time.monotonic(), raw_bytes) + return Response(content=raw_bytes, media_type="application/json") + + +@router.post('/qaza/setup') +@limiter.limit("60/minute") +async def setup_qaza(payload: QazaSetupReq, request: Request, user: dict = Depends(get_current_user)): + uid = user["uid"] + eff_tz = await _resolve_qaza_tz(uid, payload.tz) + await qaza_collection.update_one( + {"_id": uid}, + { + "$set": { + "baseline_fajr": payload.fajr, + "baseline_dhuhr": payload.dhuhr, + "baseline_asr": payload.asr, + "baseline_maghrib": payload.maghrib, + "baseline_isha": payload.isha, + "baseline_witr": payload.witr, + "include_witr": payload.include_witr, + "daily_target": payload.daily_target, + "is_setup": True, + "updated_at": datetime.datetime.utcnow().isoformat() + "Z", + }, + "$setOnInsert": { + "_id": uid, + "uid": uid, + "makeups": {}, + "created_at": datetime.datetime.utcnow().isoformat() + "Z", + } + }, + upsert=True + ) + _invalidate_all_progress_caches(uid) + return await _calculate_dynamic_qaza_state(uid, eff_tz, fajr=payload.fajr_time, date=payload.date) + + +def _qaza_payload_hash(prayer: str, count: int, date_str: str) -> str: + canonical = json.dumps( + { + "prayer": str(prayer).lower(), + "count": int(count), + "date": str(date_str), + }, + sort_keys=True, + separators=(",", ":"), + ) + return hashlib.sha256(canonical.encode("utf-8")).hexdigest() + + +def _qaza_existing_matches( + existing_ev: Optional[Dict[str, Any]], + prayer: str, + count: int, + date_str: str, + req_hash: str, +) -> bool: + if not existing_ev: + return True + stored_hash = existing_ev.get("payload_hash") + if stored_hash is not None: + return stored_hash == req_hash + if "prayer" in existing_ev and str(existing_ev["prayer"]).lower() != str(prayer).lower(): + return False + if "count" in existing_ev and int(existing_ev["count"]) != int(count): + return False + if "date" in existing_ev and str(existing_ev["date"]) != str(date_str): + return False + return True + + +@router.post('/qaza/log') +@limiter.limit("120/minute") +async def log_qaza(payload: QazaLogReq, request: Request, user: dict = Depends(get_current_user)): + """M-03, L-07, DATA-003, NEW-DATA-001: Atomic claim-then-apply logging for Qaza completions.""" + uid = user["uid"] + eff_tz = await _resolve_qaza_tz(uid, payload.tz) + prayer = payload.prayer.lower() + delta = payload.count + event_id = payload.event_id or str(uuid.uuid4()) + now_dt = datetime.datetime.utcnow() + now_iso = now_dt.isoformat() + "Z" + lease_until_iso = (now_dt + datetime.timedelta(seconds=15)).isoformat() + "Z" + event_date = payload.date or _progress_local_date(eff_tz or 0.0).isoformat() + req_hash = _qaza_payload_hash(prayer=prayer, count=delta, date_str=event_date) + qaza_events_col = qaza_collection.database["qaza_events"] + event_doc_id = f"{uid}:{event_id}" + + # 1. Atomically claim event in qaza_events ledger BEFORE modifying counters + claimed = False + existing_ev = None + try: + await qaza_events_col.insert_one({ + "_id": event_doc_id, + "uid": uid, + "event_id": event_id, + "prayer": prayer, + "count": delta, + "effective_delta": 0, + "date": event_date, + "payload_hash": req_hash, + "counter_applied": False, + "lease_until": lease_until_iso, + "created_at": now_iso, + }) + claimed = True + except DuplicateKeyError: + existing_ev = await qaza_events_col.find_one({"_id": event_doc_id}) + if existing_ev and not _qaza_existing_matches( + existing_ev, prayer=prayer, count=delta, date_str=event_date, req_hash=req_hash + ): + raise HTTPException( + status_code=409, + detail="Idempotency conflict: event_id was already recorded with a different Qaza payload.", + ) + if existing_ev and existing_ev.get("counter_applied"): + return await _calculate_dynamic_qaza_state(uid, eff_tz, fajr=payload.fajr_time, date=payload.date) + lease_doc = await qaza_events_col.find_one_and_update( + { + "_id": event_doc_id, + "counter_applied": {"$ne": True}, + "$or": [ + {"lease_until": {"$exists": False}}, + {"lease_until": None}, + {"lease_until": {"$lt": now_iso}}, + ], + }, + {"$set": {"lease_until": lease_until_iso, "updated_at": now_iso}}, + return_document=ReturnDocument.AFTER, + ) + if lease_doc is None: + fresh_ev = await qaza_events_col.find_one({"_id": event_doc_id}) + if fresh_ev and not _qaza_existing_matches( + fresh_ev, prayer=prayer, count=delta, date_str=event_date, req_hash=req_hash + ): + raise HTTPException( + status_code=409, + detail="Idempotency conflict: event_id was already recorded with a different Qaza payload.", + ) + if fresh_ev and fresh_ev.get("counter_applied"): + return await _calculate_dynamic_qaza_state(uid, eff_tz, fajr=payload.fajr_time, date=payload.date) + raise HTTPException( + status_code=503, + detail="Qaza completion is already being processed. Please retry shortly.", + ) + # Always recover using the stored canonical event fields + prayer = str(lease_doc.get("prayer") or prayer).lower() + if "count" in lease_doc: + delta = int(lease_doc["count"]) + claimed = True + except HTTPException: + raise + except Exception as exc: + logger.error(f"qaza_events claim failure for {uid}: {exc}") + raise HTTPException(status_code=503, detail="Failed to persist Qaza event ledger.") + + # 2. Apply idempotent counter update to qaza_collection & compute actual effective_delta + effective_delta = 0 + try: + if delta > 0: + try: + upd_res = await qaza_collection.update_one( + {"_id": uid, "applied_events": {"$ne": event_id}}, + { + "$inc": { + f"makeups.{prayer}": delta, + f"completed_{prayer}": delta, + }, + "$push": {"applied_events": {"$each": [event_id], "$slice": -1000}}, + "$set": {"updated_at": now_iso, "last_event_id": event_id}, + "$setOnInsert": {"_id": uid, "uid": uid, "created_at": now_iso}, + }, + upsert=True, + ) + mc = getattr(upd_res, "matched_count", None) + upc = getattr(upd_res, "upserted_id", None) + if mc is None or mc > 0 or upc is not None: + effective_delta = delta + else: + effective_delta = int((existing_ev or {}).get("effective_delta") or delta) + except DuplicateKeyError: + # DuplicateKeyError here only occurs when uid already exists AND applied_events contains event_id + effective_delta = int((existing_ev or {}).get("effective_delta") or delta) + elif delta < 0: + dec_doc = await qaza_collection.find_one_and_update( + { + "_id": uid, + "applied_events": {"$ne": event_id}, + f"makeups.{prayer}": {"$gte": -delta}, + }, + { + "$inc": { + f"makeups.{prayer}": delta, + f"completed_{prayer}": delta, + }, + "$push": {"applied_events": {"$each": [event_id], "$slice": -1000}}, + "$set": {"updated_at": now_iso, "last_event_id": event_id}, + }, + upsert=False, + return_document=ReturnDocument.AFTER, + ) + if dec_doc is not None: + effective_delta = delta + else: + for _attempt in range(3): + q_doc = await qaza_collection.find_one({"_id": uid}) + if q_doc and event_id in (q_doc.get("applied_events") or []): + effective_delta = int((existing_ev or {}).get("effective_delta") or 0) + break + if q_doc is None: + try: + await qaza_collection.update_one( + {"_id": uid, "applied_events": {"$ne": event_id}}, + { + "$set": { + f"makeups.{prayer}": 0, + f"completed_{prayer}": 0, + "updated_at": now_iso, + "last_event_id": event_id, + }, + "$push": {"applied_events": {"$each": [event_id], "$slice": -1000}}, + "$setOnInsert": {"_id": uid, "uid": uid, "created_at": now_iso}, + }, + upsert=True, + ) + effective_delta = 0 + break + except DuplicateKeyError: + continue + makeups = q_doc.get("makeups") or {} + raw_val = makeups.get(prayer) + cur_makeups = int(raw_val if raw_val is not None else (q_doc.get(f"completed_{prayer}", 0) or 0)) + new_makeups = max(0, cur_makeups + delta) + actual_step = new_makeups - cur_makeups + cas_filter: Dict[str, Any] = {"_id": uid, "applied_events": {"$ne": event_id}} + if raw_val is not None: + cas_filter[f"makeups.{prayer}"] = cur_makeups + else: + cas_filter[f"makeups.{prayer}"] = {"$exists": False} + upd_res = await qaza_collection.update_one( + cas_filter, + { + "$set": { + f"makeups.{prayer}": new_makeups, + f"completed_{prayer}": new_makeups, + "updated_at": now_iso, + "last_event_id": event_id, + }, + "$push": {"applied_events": {"$each": [event_id], "$slice": -1000}}, + }, + upsert=False, + ) + mc = getattr(upd_res, "matched_count", None) + if mc is None or mc > 0: + effective_delta = actual_step + break + + # 3. Mark ledger event counter_applied cleanly with actual effective_delta + await qaza_events_col.update_one( + {"_id": event_doc_id}, + { + "$set": { + "effective_delta": effective_delta, + "counter_applied": True, + "lease_until": None, + "applied_at": datetime.datetime.utcnow().isoformat() + "Z", + } + }, + ) + except Exception as exc: + if claimed: + try: + await qaza_events_col.update_one( + {"_id": event_doc_id}, + {"$set": {"lease_until": None}}, + ) + except Exception: + pass + logger.error(f"qaza counter update failed for {uid}: {exc}") + raise HTTPException(status_code=503, detail="Failed to update Qaza counter. Please retry.") + + _invalidate_all_progress_caches(uid) + return await _calculate_dynamic_qaza_state(uid, eff_tz, fajr=payload.fajr_time, date=payload.date) + + +_MONTHLY_PROGRESS_TTL = 30.0 + +@router.get('/progress/monthly') +@limiter.limit("1200/minute") +async def get_progress_monthly( + request: Request, + tz: float = 0.0, + fajr: Optional[str] = None, + date: Optional[str] = None, + year: Optional[int] = None, + month: Optional[int] = None, + user: dict = Depends(get_current_user) +): + uid = user["uid"] + cache_key = f"{uid}:{tz}:{year}:{month}:{fajr}:{date}" + ent = _MONTHLY_PROGRESS_CACHE.get(cache_key) + if ent and (time.monotonic() - ent[0]) < _MONTHLY_PROGRESS_TTL: + return Response(content=ent[1], media_type="application/json") + + import calendar + if isinstance(date, str) and date.strip(): + try: + today = datetime.date.fromisoformat(date.strip()) + except Exception: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + else: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + + if year is not None and month is not None: + target_year = int(year) + target_month = int(month) + if target_month < 1 or target_month > 12: + raise HTTPException(status_code=400, detail="Invalid month (must be 1-12)") + else: + target_year = today.year + target_month = today.month + + # 1. Reject future months + if target_year > today.year or (target_year == today.year and target_month > today.month): + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Cannot navigate to future months.", + ) + + # 2. Access control for previous/historical months + is_current_month = (target_year == today.year and target_month == today.month) + if not is_current_month: + from app.auth_deps import has_user_historical_progress + if not await has_user_historical_progress(user): + locked_res = { + "locked": True, + "message": "Historical progress is locked for previous months. Upgrade to Progress Plus (₹60/mo) or Sahih Pro to view your saved history.", + "required_plans": ["sahih_progress_plus_monthly", "sahih_pro_monthly"], + "year": target_year, + "month": target_month, + "days": [], + "total_days": 0, + "elapsed_days": 0, + "full_days": 0, + "golden_days": 0, + "consistency_pct": 0.0, + "prayer_breakdown": {"Fajr": 0, "Dhuhr": 0, "Asr": 0, "Maghrib": 0, "Isha": 0}, + "strongest_prayer": "Locked", + "weakest_prayer": "Locked", + "streak": 0, + "golden_streak": 0, + } + return Response(content=json.dumps(locked_res).encode("utf-8"), media_type="application/json") + + # 3. Bound past data to the user's tracking start date + user_doc = None + try: + user_doc = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + {"progress_tracking": 1, "progress_tracking_enabled": 1, "progress_tracking_started_date": 1, "created_at": 1} + ) + except Exception: + user_doc = None + + effective_user = {**(user or {})} + if user_doc: + effective_user.update(user_doc) + pt = effective_user.get("progress_tracking") or {} + is_tracking_enabled = effective_user.get("progress_tracking_enabled", pt.get("enabled", True)) + started_date_str = effective_user.get("progress_tracking_started_date", pt.get("started_date")) + + earliest_year = today.year + earliest_month = today.month + + if started_date_str: + try: + earliest_year = int(started_date_str[:4]) + earliest_month = int(started_date_str[5:7]) + except Exception: + earliest_year = today.year + earliest_month = today.month + + # If requested month is strictly before earliest tracking month, boundary reached! + if target_year < earliest_year or (target_year == earliest_year and target_month < earliest_month): + before_res = { + "before_start": True, + "locked": False, + "is_tracking_enabled": bool(is_tracking_enabled), + "earliest_date": started_date_str, + "earliest_year": earliest_year, + "earliest_month": earliest_month, + "year": target_year, + "month": target_month, + "days": [], + "total_days": 0, + "elapsed_days": 0, + "full_days": 0, + "golden_days": 0, + "consistency_pct": 0.0, + "prayer_breakdown": {"Fajr": 0, "Dhuhr": 0, "Asr": 0, "Maghrib": 0, "Isha": 0}, + "strongest_prayer": "None", + "weakest_prayer": "None", + "streak": 0, + "golden_streak": 0, + } + return Response(content=json.dumps(before_res).encode("utf-8"), media_type="application/json") + else: + started_date_str = today.isoformat() + + _, num_days = calendar.monthrange(target_year, target_month) + start_date = f"{target_year:04d}-{target_month:02d}-01" + end_date = f"{target_year:04d}-{target_month:02d}-{num_days:02d}" + dates_range = [f"{target_year:04d}-{target_month:02d}-{d:02d}" for d in range(1, num_days + 1)] + + docs = await daily_progress_collection.find( + {"$or": [{"uid": uid}, {"_id": uid}], "date": {"$gte": start_date, "$lte": end_date}}, {"_id": 0} + ).sort("date", 1).to_list(length=100) + + # Fetch tasbeeh daily rollups for the date range to guarantee all deeds are included + tasbeeh_by_date = {} + try: + tasbeeh_dailies = await tasbeeh_daily_collection.find( + {"$or": [{"uid": uid}, {"_id": uid}], "date": {"$gte": start_date, "$lte": end_date}}, {"_id": 0, "date": 1, "total_count": 1} + ).to_list(length=35) + for td in tasbeeh_dailies: + td_date = td.get("date") + if td_date: + tasbeeh_by_date[td_date] = int(td.get("total_count", 0) or 0) + except Exception: + pass + + if not tasbeeh_by_date: + tasbeeh_sessions = await tasbeeh_collection.find( + {"$or": [{"uid": uid}, {"_id": uid}], "date": {"$gte": start_date, "$lte": end_date}}, {"_id": 0, "date": 1, "count": 1} + ).to_list(length=1000) + for ts in tasbeeh_sessions: + ts_date = ts.get("date") + if ts_date: + tasbeeh_by_date[ts_date] = tasbeeh_by_date.get(ts_date, 0) + int(ts.get("count", 0) or 0) + + docs_by_date = {d["date"]: d for d in docs} + days_list = [] + prayer_counts = {"Fajr": 0, "Dhuhr": 0, "Asr": 0, "Maghrib": 0, "Isha": 0} + full_days = 0 + golden_days = 0 + + for d in dates_range: + item = docs_by_date.get(d, {}) + raw_prayers = item.get("prayers", []) or [] + norm_set = set(_normalize_progress_prayer_name(p) for p in raw_prayers if _normalize_progress_prayer_name(p)) + prayers = [p for p in PROGRESS_PRAYERS if p in norm_set] + is_full = len(prayers) == 5 + q_secs = int(item.get("quran_secs", 0) or 0) + h_secs = int(item.get("hadith_secs", 0) or 0) + t_count = max(int(item.get("tasbeeh_count", 0) or 0), tasbeeh_by_date.get(d, 0)) + is_golden = is_full and (q_secs >= PROGRESS_GOAL_SECONDS) and (h_secs >= PROGRESS_GOAL_SECONDS or t_count >= PROGRESS_TASBEEH_GOAL) + is_untracked = bool(started_date_str and d < started_date_str) + + if not is_untracked: + if is_full: + full_days += 1 + if is_golden: + golden_days += 1 + + for p in prayers: + if p in prayer_counts: + prayer_counts[p] += 1 + + days_list.append({ + "date": d, + "prayers": prayers if not is_untracked else [], + "completed_count": len(prayers) if not is_untracked else 0, + "is_full": is_full if not is_untracked else False, + "is_golden": is_golden if not is_untracked else False, + "is_untracked": is_untracked, + "quran_secs": q_secs if not is_untracked else 0, + "hadith_secs": h_secs if not is_untracked else 0, + "tasbeeh_count": t_count if not is_untracked else 0, + }) + + # Elapsed days calculation (so future unelapsed days & pre-start days don't falsely register as missed) + is_current_month = (target_year == today.year and target_month == today.month) + is_earliest_month = (target_year == earliest_year and target_month == earliest_month) + start_day = 1 + if is_earliest_month and started_date_str: + try: + start_day = int(started_date_str[8:10]) + except Exception: + start_day = 1 + + if is_current_month: + elapsed_days = max(1, min(num_days, today.day) - start_day + 1) if is_earliest_month else min(num_days, today.day) + elif target_year < today.year or (target_year == today.year and target_month < today.month): + elapsed_days = max(1, num_days - start_day + 1) if is_earliest_month else num_days + else: + elapsed_days = 0 + + total_days = len(dates_range) + effective_days = elapsed_days if elapsed_days > 0 else total_days + + # Due days per prayer calculation (prayers whose time has not started today are not yet due) + due_counts = {} + today_str = today.isoformat() + now_local = datetime.datetime.utcnow() + datetime.timedelta(hours=tz) + local_h = now_local.hour + (now_local.minute / 60.0) + + today_item = docs_by_date.get(today_str, {}) + today_raw = today_item.get("prayers", []) or [] + today_norm = set(_normalize_progress_prayer_name(x) for x in today_raw if _normalize_progress_prayer_name(x)) + + if is_current_month: + past_days_count = max(0, min(num_days, today.day - 1) - start_day + 1) if is_earliest_month else max(0, min(num_days, today.day - 1)) + else: + past_days_count = elapsed_days + + missed_today_count = 0 + for p in PROGRESS_PRAYERS: + if is_current_month: + p_started = False + if p == "Fajr": + p_started = local_h >= 5.0 + elif p == "Dhuhr": + p_started = local_h >= 12.0 + elif p == "Asr": + p_started = local_h >= 16.0 + elif p == "Maghrib": + p_started = local_h >= 17.75 + elif p == "Isha": + p_started = local_h >= 19.25 + + if p in today_norm: + p_started = True + + due_counts[p] = past_days_count + (1 if p_started else 0) + if p_started and p not in today_norm: + missed_today_count += 1 + else: + due_counts[p] = elapsed_days + + # M-09: Only actually completed 5-prayer full days are counted in consistency + effective_full_days = full_days + + consistency_pct = round((effective_full_days / float(effective_days)) * 100, 1) if effective_days > 0 else 0.0 + + # Determine strongest and weakest prayer with tie handling + counts_vals = list(prayer_counts.values()) + max_c = max(counts_vals) if counts_vals else 0 + min_c = min(counts_vals) if counts_vals else 0 + + if max_c == 0: + strongest = "None" + weakest = "None" + elif max_c == min_c: + strongest = "Equal Consistency" + weakest = "Equal Consistency" + else: + max_prayers = [p for p, c in prayer_counts.items() if c == max_c] + min_prayers = [p for p, c in prayer_counts.items() if c == min_c] + strongest = ", ".join(max_prayers) if len(max_prayers) < 5 else "Equal Consistency" + weakest = ", ".join(min_prayers) if len(min_prayers) < 5 else "Equal Consistency" + + current_streak, golden_streak = await _progress_streaks(uid, today) + + res = { + "locked": False, + "days": days_list, + "total_days": total_days, + "elapsed_days": elapsed_days, + "full_days": full_days, + "golden_days": golden_days, + "consistency_pct": consistency_pct, + "prayer_breakdown": prayer_counts, + "due_days": due_counts, + "strongest_prayer": strongest, + "weakest_prayer": weakest, + "streak": current_streak, + "golden_streak": golden_streak, + "year": target_year, + "month": target_month, + "earliest_date": started_date_str, + "earliest_year": earliest_year, + "earliest_month": earliest_month, + "is_tracking_enabled": bool(is_tracking_enabled), + } + raw_bytes = json.dumps(res).encode("utf-8") + _sweep_cache_if_needed(_MONTHLY_PROGRESS_CACHE) + _MONTHLY_PROGRESS_CACHE[cache_key] = (time.monotonic(), raw_bytes) + return Response(content=raw_bytes, media_type="application/json") + + +_RECORDED_MISSED_TTL = 30.0 + +@router.get('/progress/recorded-missed') +@limiter.limit("1200/minute") +async def get_recorded_missed_prayers( + request: Request, + tz: float = 0.0, + fajr: Optional[str] = None, + date: Optional[str] = None, + user: dict = Depends(get_current_user) +): + """Calculates all-time completed and missed prayers across all recorded daily progress data in O(1).""" + uid = user["uid"] + if isinstance(date, str) and date.strip(): + today_str = _validate_progress_date(date.strip(), tz, fajr if isinstance(fajr, str) else None, max_past_days=3650) + else: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + today_str = today.isoformat() + + cache_key = f"{uid}:{tz}:{today_str}" + ent = _RECORDED_MISSED_CACHE.get(cache_key) + if ent and (time.monotonic() - ent[0]) < _RECORDED_MISSED_TTL: + return Response(content=ent[1], media_type="application/json") + + stats = None + try: + stats = await nimaz_stats_collection.find_one({"_id": uid}, {"_id": 0}) + except Exception: + pass + if not stats or stats.get("evaluated_date") != today_str: + stats = await _recalculate_nimaz_stats_for_user(uid, today_str) + + payload = { + "total_days": int((stats or {}).get("total_days", 0) or 0), + "missed": (stats or {}).get("missed", {p: 0 for p in PROGRESS_PRAYERS}), + "prayed": (stats or {}).get("prayed", {p: 0 for p in PROGRESS_PRAYERS}), + "total_missed": int((stats or {}).get("total_missed", 0) or 0), + "total_prayed": int((stats or {}).get("total_prayed", 0) or 0), + "total_quran_days": int((stats or {}).get("total_quran_days", 0) or 0), + "total_golden_days": int((stats or {}).get("total_golden_days", 0) or 0), + } + raw_bytes = json.dumps(payload).encode("utf-8") + _sweep_cache_if_needed(_RECORDED_MISSED_CACHE) + _RECORDED_MISSED_CACHE[cache_key] = (time.monotonic(), raw_bytes) + return Response(content=raw_bytes, media_type="application/json") + + +class TrackingStatusReq(BaseModel): + action: str = Field(..., description="'enable' or 'later'") + tz: float = 0.0 + + +@router.get('/progress/tracking-status') +@limiter.limit("1200/minute") +async def get_progress_tracking_status(request: Request, user: dict = Depends(get_current_user)): + """Retrieves whether the user has enabled progress tracking, consent response state, and recording start date.""" + uid = user["uid"] + u = await users_collection.find_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + { + "progress_tracking": 1, + "progress_tracking_enabled": 1, + "progress_tracking_prompt_responded": 1, + "progress_tracking_started_at": 1, + "progress_tracking_started_date": 1, + "lifetime_apples": 1, + "metrics.lifetime_apples": 1, + "created_at": 1, + } + ) + if not u: + raise HTTPException(status_code=404, detail="User not found") + + return _resolve_progress_tracking_state_readonly(u) + + +@router.post('/progress/tracking-status') +@limiter.limit("120/minute") +async def update_progress_tracking_status(payload: TrackingStatusReq, request: Request, user: dict = Depends(get_current_user)): + """Allows user to enable tracking or choose 'later'.""" + uid = user["uid"] + act = payload.action.strip().lower() + now = datetime.datetime.utcnow() + now_iso = now.isoformat() + "Z" + today_str = _progress_local_date(payload.tz).isoformat() + + if act in ("enable", "yes", "true"): + u = await users_collection.find_one({"$or": [{"uid": uid}, {"_id": uid}]}, {"progress_tracking_started_date": 1, "progress_tracking.started_date": 1}) + existing_start = (u or {}).get("progress_tracking_started_date") or ((u or {}).get("progress_tracking") or {}).get("started_date") + start_date = existing_start or today_str + + await users_collection.update_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + { + "$set": { + "progress_tracking_enabled": True, + "progress_tracking_prompt_responded": True, + "progress_tracking_started_at": now_iso, + "progress_tracking_started_date": start_date, + "progress_tracking.enabled": True, + "progress_tracking.prompt_responded": True, + "progress_tracking.started_at": now_iso, + "progress_tracking.started_date": start_date, + "updated_at": now, + } + } + ) + return { + "status": "success", + "enabled": True, + "prompt_responded": True, + "started_at": now_iso, + "started_date": start_date, + } + elif act in ("later", "false"): + await users_collection.update_one( + {"$or": [{"uid": uid}, {"_id": uid}]}, + { + "$set": { + "progress_tracking_enabled": False, + "progress_tracking_prompt_responded": True, + "progress_tracking.enabled": False, + "progress_tracking.prompt_responded": True, + "updated_at": now, + } + } + ) + return { + "status": "success", + "enabled": False, + "prompt_responded": True, + "started_at": None, + "started_date": None, + } + else: + raise HTTPException(status_code=400, detail="Invalid action. Use 'enable' or 'later'.") + + +# ========================================================= +# 11b. TASBEEH & DHIKR LIFETIME ARCHITECTURE & CONSISTENCY +# ========================================================= + +def _sanitize_dhikr_key(name: str) -> str: + if not name: + return "General_Dhikr" + cleaned = re.sub(r'[\.\$\0]', '_', name.strip()) + return cleaned if cleaned else "General_Dhikr" + + +class TasbeehLogReq(BaseModel): + count: int = Field(0, ge=0, le=100000) + dhikr: str = Field("Subhan Allah", max_length=150) + arabic: Optional[str] = Field("", max_length=150) + laps: int = Field(0, ge=0) + target: int = Field(33, ge=0) + duration_seconds: int = Field(0, ge=0, le=86400) + date: Optional[str] = None # YYYY-MM-DD local date + tz: float = Field(default=0.0, ge=-12, le=14) + event_id: Optional[str] = Field(None, max_length=100) + + @model_validator(mode="after") + def _require_count_or_duration(self): + if self.count <= 0 and self.duration_seconds <= 0: + raise ValueError("Either count or duration_seconds must be greater than 0") + return self + + +class TasbeehBatchSyncReq(BaseModel): + sessions: List[TasbeehLogReq] = Field(..., min_length=1, max_length=500) + tz: float = Field(default=0.0, ge=-12, le=14) + + +def _tasbeeh_payload_hash( + date_str: str, + count: int, + dhikr: str, + arabic: str, + laps: int, + duration_seconds: int, +) -> str: + canonical = json.dumps( + { + "date": str(date_str), + "count": int(count), + "dhikr": str(dhikr), + "arabic": str(arabic or ""), + "laps": int(laps), + "duration_seconds": int(duration_seconds), + }, + sort_keys=True, + separators=(",", ":"), + ) + return hashlib.sha256(canonical.encode("utf-8")).hexdigest() + + +def _tasbeeh_existing_matches( + existing: Optional[Dict[str, Any]], + date_str: str, + count: int, + dhikr: str, + arabic: str, + laps: int, + duration_seconds: int, + req_hash: str, +) -> bool: + if not existing: + return True + stored_hash = existing.get("payload_hash") + if stored_hash is not None: + return stored_hash == req_hash + # Legacy/minimal session docs without payload_hash: check any canonical fields present + if existing.get("date") is not None and str(existing["date"]) != str(date_str): + return False + if existing.get("count") is not None and int(existing["count"]) != int(count): + return False + if existing.get("dhikr") is not None and str(existing["dhikr"]) != str(dhikr): + return False + if existing.get("laps") is not None and int(existing["laps"]) != int(laps): + return False + if existing.get("duration_seconds") is not None and int(existing["duration_seconds"]) != int(duration_seconds): + return False + return True + + +_TRANSIENT_MONGO_ERRORS = ( + AutoReconnect, + NetworkTimeout, + ConnectionFailure, + ServerSelectionTimeoutError, + ExecutionTimeout, +) + + +async def _retry_transient_mongo(operation, *, op_name: str, retries: int = 1): + """Executes an async Mongo write with single-retry recovery on transient + socket/failover blips and translates persistent driver failures into 503. + DuplicateKeyError and HTTPException always propagate untouched. + """ + last_exc: Optional[Exception] = None + for attempt in range(retries + 1): + try: + return await operation() + except (DuplicateKeyError, HTTPException): + raise + except _TRANSIENT_MONGO_ERRORS as exc: + last_exc = exc + logger.warning(f"[{op_name}] Transient MongoDB error (attempt {attempt + 1}/{retries + 1}): {exc}") + if attempt < retries: + await asyncio.sleep(0.15) + except PyMongoError as exc: + logger.error(f"[{op_name}] MongoDB write failed: {exc}") + raise HTTPException( + status_code=status.HTTP_503_SERVICE_UNAVAILABLE, + detail="Service temporarily busy. Your Tasbeeh is safely queued and will sync shortly.", + headers={"Retry-After": "5"}, + ) from exc + raise HTTPException( + status_code=status.HTTP_503_SERVICE_UNAVAILABLE, + detail="Service temporarily busy. Your Tasbeeh is safely queued and will sync shortly.", + headers={"Retry-After": "5"}, + ) from last_exc + + +def _extract_tasbeeh_canonical_fields( + existing: Optional[Dict[str, Any]], + *, + date_str: str, + count: int, + dhikr: str, + arabic: str, + laps: int, + duration_seconds: int, +) -> Tuple[str, int, str, str, int, int]: + """Resolves canonical Tasbeeh session values from an existing record safely.""" + if not existing: + return date_str, count, dhikr, arabic, laps, duration_seconds + return ( + existing.get("date") or date_str, + int(existing["count"]) if existing.get("count") is not None else count, + str(existing["dhikr"]) if existing.get("dhikr") is not None else dhikr, + str(existing.get("arabic") or "") if existing.get("arabic") is not None else arabic, + int(existing["laps"]) if existing.get("laps") is not None else laps, + int(existing["duration_seconds"]) if existing.get("duration_seconds") is not None else duration_seconds, + ) + + +async def _apply_tasbeeh_rollup( + uid: str, + date_str: str, + count: int, + dhikr: str, + arabic: str, + laps: int, + duration_seconds: int, + event_id: str, + now_utc: str, + session_state: Optional[Dict[str, Any]] = None, +) -> int: + """Atomically increments daily progress, daily rollup and lifetime stats. + Guarantees per-projection idempotent recovery across all three collections (M-04 / DATA-001). + """ + safe_key = _sanitize_dhikr_key(dhikr) + doc_id = f"tasbeeh:{uid}:{event_id}" + state = session_state or {} + + # 1. Update daily_progress_collection (unique index on (uid, date)) + new_daily_progress_count = count + if state.get("daily_progress_applied"): + existing_dp = await daily_progress_collection.find_one( + {"uid": uid, "date": date_str}, + {"_id": 0, "tasbeeh_count": 1}, + ) + if existing_dp: + new_daily_progress_count = int(existing_dp.get("tasbeeh_count", 0) or count) + elif count <= 0: + existing_dp = await daily_progress_collection.find_one( + {"uid": uid, "date": date_str}, + {"_id": 0, "tasbeeh_count": 1}, + ) + if existing_dp: + new_daily_progress_count = int(existing_dp.get("tasbeeh_count", 0) or 0) + await tasbeeh_collection.update_one( + {"_id": doc_id}, + {"$set": {"daily_progress_applied": True}}, + ) + else: + try: + dp_res = await daily_progress_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "applied_tasbeeh_events": {"$ne": event_id} + }, + { + "$inc": {"tasbeeh_count": count}, + "$push": {"applied_tasbeeh_events": {"$each": [event_id], "$slice": -500}}, + "$setOnInsert": { + "uid": uid, + "date": date_str, + "prayers": [], + "quran_secs": 0, + "hadith_secs": 0 + } + }, + upsert=True, + return_document=ReturnDocument.AFTER, + projection={"_id": 0, "tasbeeh_count": 1} + ) + if dp_res and dp_res.get("tasbeeh_count") is not None: + new_daily_progress_count = int(dp_res.get("tasbeeh_count", 0)) + except DuplicateKeyError: + existing_dp = await daily_progress_collection.find_one( + {"uid": uid, "date": date_str}, + {"_id": 0, "tasbeeh_count": 1, "applied_tasbeeh_events": 1}, + ) + if ( + existing_dp + and isinstance(existing_dp.get("applied_tasbeeh_events"), list) + and event_id not in existing_dp["applied_tasbeeh_events"] + ): + # Concurrent upsert by a different event won creation race; apply ours now + retry_dp = await daily_progress_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "applied_tasbeeh_events": {"$ne": event_id}, + }, + { + "$inc": {"tasbeeh_count": count}, + "$push": {"applied_tasbeeh_events": {"$each": [event_id], "$slice": -500}}, + }, + upsert=False, + return_document=ReturnDocument.AFTER, + projection={"_id": 0, "tasbeeh_count": 1}, + ) + if retry_dp and retry_dp.get("tasbeeh_count") is not None: + new_daily_progress_count = int(retry_dp.get("tasbeeh_count", 0)) + elif existing_dp: + new_daily_progress_count = int(existing_dp.get("tasbeeh_count", 0) or count) + except Exception as e: + logger.error(f"daily_progress update error: {e}") + raise e + + await tasbeeh_collection.update_one( + {"_id": doc_id}, + {"$set": {"daily_progress_applied": True}}, + ) + + # 2. Update tasbeeh_daily_collection (unique index on (uid, date)) + new_day_total = new_daily_progress_count + is_new_day = False + if state.get("tasbeeh_daily_applied"): + existing_daily = await tasbeeh_daily_collection.find_one( + {"uid": uid, "date": date_str}, + {"total_count": 1, "session_count": 1}, + ) + if existing_daily: + new_day_total = int(existing_daily.get("total_count", 0) or new_day_total) + is_new_day = count > 0 and (new_day_total - count) <= 0 + else: + try: + daily_res = await tasbeeh_daily_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "applied_events": {"$ne": event_id} + }, + { + "$inc": { + "total_count": count, + "total_seconds": duration_seconds, + "total_laps": laps, + "session_count": 1, + f"dhikrs.{safe_key}.count": count, + f"dhikrs.{safe_key}.seconds": duration_seconds, + f"dhikrs.{safe_key}.laps": laps, + }, + "$push": {"applied_events": {"$each": [event_id], "$slice": -500}}, + "$set": { + "uid": uid, + "date": date_str, + f"dhikrs.{safe_key}.dhikr": dhikr, + f"dhikrs.{safe_key}.arabic": arabic or "", + "updated_at": now_utc, + }, + "$setOnInsert": { + "_id": f"{uid}:{date_str}", + "created_at": now_utc, + } + }, + upsert=True, + return_document=ReturnDocument.AFTER + ) + if daily_res: + new_day_total = int(daily_res.get("total_count", 0) or new_day_total) + is_new_day = count > 0 and (new_day_total - count) <= 0 + except DuplicateKeyError: + existing_daily = await tasbeeh_daily_collection.find_one( + {"uid": uid, "date": date_str}, + {"total_count": 1, "session_count": 1, "applied_events": 1}, + ) + if ( + existing_daily + and isinstance(existing_daily.get("applied_events"), list) + and event_id not in existing_daily["applied_events"] + ): + retry_daily = await tasbeeh_daily_collection.find_one_and_update( + { + "uid": uid, + "date": date_str, + "applied_events": {"$ne": event_id}, + }, + { + "$inc": { + "total_count": count, + "total_seconds": duration_seconds, + "total_laps": laps, + "session_count": 1, + f"dhikrs.{safe_key}.count": count, + f"dhikrs.{safe_key}.seconds": duration_seconds, + f"dhikrs.{safe_key}.laps": laps, + }, + "$push": {"applied_events": {"$each": [event_id], "$slice": -500}}, + "$set": { + f"dhikrs.{safe_key}.dhikr": dhikr, + f"dhikrs.{safe_key}.arabic": arabic or "", + "updated_at": now_utc, + }, + }, + upsert=False, + return_document=ReturnDocument.AFTER, + ) + if retry_daily: + new_day_total = int(retry_daily.get("total_count", 0) or new_day_total) + is_new_day = count > 0 and (new_day_total - count) <= 0 + elif existing_daily: + new_day_total = int(existing_daily.get("total_count", 0) or new_day_total) + is_new_day = count > 0 and (new_day_total - count) <= 0 + except Exception as e: + logger.error(f"tasbeeh_daily update error: {e}") + raise e + + await tasbeeh_collection.update_one( + {"_id": doc_id}, + {"$set": {"tasbeeh_daily_applied": True}}, + ) + + # 3. Update tasbeeh_stats_collection (Lifetime user stats) independently using its own event guard + if not state.get("tasbeeh_stats_applied"): + try: + stats_inc = { + "lifetime_count": count, + "lifetime_seconds": duration_seconds, + "lifetime_laps": laps, + "lifetime_sessions": 1, + f"dhikrs.{safe_key}.count": count, + } + if is_new_day: + stats_inc["active_days_count"] = 1 + + stats_on_insert = { + "best_day": {"date": date_str, "count": new_day_total}, + "streak": { + "current": 1 if new_day_total >= PROGRESS_TASBEEH_GOAL else 0, + "longest": 1 if new_day_total >= PROGRESS_TASBEEH_GOAL else 0, + "last_active_date": date_str if new_day_total >= PROGRESS_TASBEEH_GOAL else "", + }, + "created_at": now_utc, + } + if "active_days_count" not in stats_inc: + stats_on_insert["active_days_count"] = 0 + + stats_doc = None + try: + stats_doc = await tasbeeh_stats_collection.find_one_and_update( + {"_id": uid, "applied_events": {"$ne": event_id}}, + { + "$inc": stats_inc, + "$push": {"applied_events": {"$each": [event_id], "$slice": -500}}, + "$set": { + "uid": uid, + f"dhikrs.{safe_key}.dhikr": dhikr, + f"dhikrs.{safe_key}.arabic": arabic or "", + "updated_at": now_utc, + }, + "$setOnInsert": stats_on_insert, + }, + upsert=True, + return_document=ReturnDocument.AFTER + ) + except DuplicateKeyError: + stats_doc = await tasbeeh_stats_collection.find_one({"_id": uid}) + + if stats_doc: + best_day_raw = stats_doc.get("best_day") + current_best = int((best_day_raw if isinstance(best_day_raw, dict) else {}).get("count", 0) or 0) + streak_raw = stats_doc.get("streak") + streak_data = streak_raw if isinstance(streak_raw, dict) else {} + curr_streak = int(streak_data.get("current", 0) or 0) + long_streak = int(streak_data.get("longest", 0) or 0) + last_date_str = str(streak_data.get("last_active_date") or "") + + updates = {} + if new_day_total > current_best: + updates["best_day"] = {"date": date_str, "count": new_day_total} + + # TDS-STREAK: Streak is strictly incremented ONLY when daily total reaches >= 297 beads + if new_day_total >= PROGRESS_TASBEEH_GOAL and last_date_str != date_str: + try: + dt_curr = datetime.date.fromisoformat(date_str) + if last_date_str: + dt_last = datetime.date.fromisoformat(last_date_str) + diff = (dt_curr - dt_last).days + if diff == 1: + curr_streak += 1 + long_streak = max(long_streak, curr_streak) + updates["streak"] = {"current": curr_streak, "longest": long_streak, "last_active_date": date_str} + elif diff > 1: + updates["streak"] = {"current": 1, "longest": max(long_streak, 1), "last_active_date": date_str} + else: + updates["streak"] = {"current": 1, "longest": max(long_streak, 1), "last_active_date": date_str} + except Exception: + pass + + if updates: + await tasbeeh_stats_collection.update_one({"_id": uid}, {"$set": updates}) + + await tasbeeh_collection.update_one( + {"_id": doc_id}, + {"$set": {"tasbeeh_stats_applied": True}}, + ) + except Exception as e: + logger.error(f"tasbeeh_stats update error: {e}") + raise e + + return max(new_day_total, new_daily_progress_count) + + +async def _calculate_tasbeeh_streak(uid: str, today: datetime.date) -> tuple[int, int, str]: + """Calculates current and longest consecutive day streak for Tasbeeh where daily count >= 297.""" + streak_qualifying_dates = [] + try: + today_str = today.isoformat() + # M-10: Exclude future dates from streak calculations + q_daily = await daily_progress_collection.distinct( + "date", {"uid": uid, "date": {"$lte": today_str}, "tasbeeh_count": {"$gte": PROGRESS_TASBEEH_GOAL}} + ) + q_tasbeeh = await tasbeeh_daily_collection.distinct( + "date", {"uid": uid, "date": {"$lte": today_str}, "total_count": {"$gte": PROGRESS_TASBEEH_GOAL}} + ) + streak_qualifying_dates = sorted([d for d in (set(q_daily or []) | set(q_tasbeeh or [])) if d <= today_str]) + except Exception: + pass + + current_streak = 0 + longest_streak = 0 + last_active_date = "" + + if streak_qualifying_dates: + last_active_date = streak_qualifying_dates[-1] + today_str = today.isoformat() + yesterday_str = (today - datetime.timedelta(days=1)).isoformat() + + if today_str in streak_qualifying_dates: + c = today + while c.isoformat() in streak_qualifying_dates: + current_streak += 1 + c -= datetime.timedelta(days=1) + elif yesterday_str in streak_qualifying_dates: + c = today - datetime.timedelta(days=1) + while c.isoformat() in streak_qualifying_dates: + current_streak += 1 + c -= datetime.timedelta(days=1) + else: + current_streak = 0 + + curr_run = 0 + prev_d = None + for d_str in streak_qualifying_dates: + try: + curr_d = datetime.date.fromisoformat(d_str) + if prev_d is None or (curr_d - prev_d).days == 1: + curr_run += 1 + else: + curr_run = 1 + longest_streak = max(longest_streak, curr_run) + prev_d = curr_d + except Exception: + pass + longest_streak = max(longest_streak, current_streak) + + return current_streak, longest_streak, last_active_date + + +async def _ensure_user_tasbeeh_stats(uid: str, today_str: Optional[str] = None) -> dict: + """Guarantees lifetime stats exist for user with self-healing backfill.""" + try: + stats = await tasbeeh_stats_collection.find_one({"_id": uid}) + if stats: + return stats + except Exception: + pass + + now_utc = datetime.datetime.utcnow().isoformat() + "Z" + + # One-time aggregation across historical records + sess_total = 0 + sess_seconds = 0 + sess_laps = 0 + sess_count = 0 + try: + all_sessions_agg = await tasbeeh_collection.aggregate([ + {"$match": {"uid": uid}}, + {"$group": { + "_id": None, + "total_beads": {"$sum": "$count"}, + "total_seconds": {"$sum": "$duration_seconds"}, + "total_laps": {"$sum": "$laps"}, + "total_sessions": {"$sum": 1} + }} + ]).to_list(length=1) + if all_sessions_agg: + sess_total = int(all_sessions_agg[0].get("total_beads", 0) or 0) + sess_seconds = int(all_sessions_agg[0].get("total_seconds", 0) or 0) + sess_laps = int(all_sessions_agg[0].get("total_laps", 0) or 0) + sess_count = int(all_sessions_agg[0].get("total_sessions", 0) or 0) + except Exception: + pass + + daily_all_total = 0 + try: + daily_all_agg = await daily_progress_collection.aggregate([ + {"$match": {"uid": uid, "tasbeeh_count": {"$gt": 0}}}, + {"$group": {"_id": None, "total": {"$sum": "$tasbeeh_count"}}} + ]).to_list(length=1) + if daily_all_agg: + daily_all_total = int(daily_all_agg[0]["total"]) + except Exception: + pass + + lifetime_count = max(sess_total, daily_all_total) + + active_dates = [] + try: + hist_tasbeeh_dates = await tasbeeh_collection.distinct("date", {"uid": uid, "count": {"$gt": 0}}) + hist_daily_dates = await daily_progress_collection.distinct("date", {"uid": uid, "tasbeeh_count": {"$gt": 0}}) + active_dates = sorted(list(set(hist_tasbeeh_dates) | set(hist_daily_dates))) + except Exception: + pass + + active_days_count = len(active_dates) + today_dt = datetime.date.today() + if today_str: + try: + today_dt = datetime.date.fromisoformat(today_str) + except Exception: + pass + current_streak, longest_streak, last_active_date = await _calculate_tasbeeh_streak(uid, today_dt) + + best_day_count = 0 + best_day_date = "" + try: + best_daily_doc = await daily_progress_collection.find_one( + {"uid": uid}, + {"_id": 0, "date": 1, "tasbeeh_count": 1}, + sort=[("tasbeeh_count", -1)] + ) + if best_daily_doc: + best_day_count = int(best_daily_doc.get("tasbeeh_count", 0) or 0) + best_day_date = best_daily_doc.get("date", "") + except Exception: + pass + + dhikrs_map = {} + try: + dhikrs_agg = await tasbeeh_collection.aggregate([ + {"$match": {"uid": uid, "count": {"$gt": 0}}}, + {"$group": {"_id": "$dhikr", "count": {"$sum": "$count"}}}, + {"$sort": {"count": -1}} + ]).to_list(length=20) + for it in dhikrs_agg: + d_name = it.get("_id") or "General Dhikr" + d_cnt = int(it.get("count", 0) or 0) + if d_cnt <= 0: + continue + s_key = _sanitize_dhikr_key(d_name) + dhikrs_map[s_key] = {"dhikr": d_name, "count": d_cnt} + except Exception: + pass + + stats_doc = { + "_id": uid, + "uid": uid, + "lifetime_count": lifetime_count, + "lifetime_seconds": sess_seconds, + "lifetime_laps": sess_laps, + "lifetime_sessions": sess_count, + "active_days_count": active_days_count, + "best_day": {"date": best_day_date, "count": best_day_count}, + "streak": { + "current": current_streak, + "longest": longest_streak, + "last_active_date": last_active_date, + }, + "dhikrs": dhikrs_map, + "created_at": now_utc, + "updated_at": now_utc, + } + + try: + await tasbeeh_stats_collection.update_one({"_id": uid}, {"$set": stats_doc}, upsert=True) + except Exception as e: + logger.warning(f"Error backfilling tasbeeh_stats for {uid}: {e}") + + return stats_doc + + +@router.post('/tasbeeh/log') +@limiter.limit("120/minute") +async def log_tasbeeh_session( + payload: TasbeehLogReq, + request: Request, + user: dict = Depends(get_current_user) +): + uid = user["uid"] + # M-05: Bound write dates: reject future dates and limit historical window to 30 days + date_str = _validate_progress_date(payload.date, payload.tz, max_past_days=30) + now_utc = datetime.datetime.utcnow().isoformat() + "Z" + event_id = payload.event_id or str(uuid.uuid4()) + doc_id = f"tasbeeh:{uid}:{event_id}" + req_hash = _tasbeeh_payload_hash( + date_str=date_str, + count=payload.count, + dhikr=payload.dhikr, + arabic=payload.arabic or "", + laps=payload.laps, + duration_seconds=payload.duration_seconds, + ) + + # TDS-001 / DATA-001 / NEW-DATA-001: Atomic event claim with immutable payload hash + session_doc = { + "_id": doc_id, + "uid": uid, + "event_id": event_id, + "date": date_str, + "timestamp": now_utc, + "count": payload.count, + "dhikr": payload.dhikr, + "arabic": payload.arabic or "", + "laps": payload.laps, + "target": payload.target, + "duration_seconds": payload.duration_seconds, + "payload_hash": req_hash, + "daily_progress_applied": False, + "tasbeeh_daily_applied": False, + "tasbeeh_stats_applied": False, + "aggregate_applied": False, + } + + is_duplicate = False + existing_state = None + eff_date = date_str + eff_count = payload.count + eff_dhikr = payload.dhikr + eff_arabic = payload.arabic or "" + eff_laps = payload.laps + eff_duration = payload.duration_seconds + try: + await _retry_transient_mongo( + lambda: tasbeeh_collection.insert_one(session_doc), + op_name="tasbeeh_log_insert", + ) + except DuplicateKeyError: + is_duplicate = True + existing = await _retry_transient_mongo( + lambda: tasbeeh_collection.find_one({"_id": doc_id}), + op_name="tasbeeh_log_find_existing", + ) + existing_state = existing + if existing: + if not _tasbeeh_existing_matches( + existing, + date_str=date_str, + count=payload.count, + dhikr=payload.dhikr, + arabic=payload.arabic or "", + laps=payload.laps, + duration_seconds=payload.duration_seconds, + req_hash=req_hash, + ): + raise HTTPException( + status_code=409, + detail="Idempotency conflict: event_id was already recorded with a different Tasbeeh payload.", + ) + eff_date, eff_count, eff_dhikr, eff_arabic, eff_laps, eff_duration = ( + _extract_tasbeeh_canonical_fields( + existing, + date_str=eff_date, + count=eff_count, + dhikr=eff_dhikr, + arabic=eff_arabic, + laps=eff_laps, + duration_seconds=eff_duration, + ) + ) + # If aggregate was already applied, return current daily progress idempotently + if existing and existing.get("aggregate_applied"): + daily_doc = await _retry_transient_mongo( + lambda: daily_progress_collection.find_one( + {"uid": uid, "date": eff_date}, + {"_id": 0, "tasbeeh_count": 1}, + ), + op_name="tasbeeh_log_read_daily", + ) or {} + curr_c = int(daily_doc.get("tasbeeh_count", 0) or eff_count) + return { + "status": "success", + "logged_count": eff_count, + "tasbeeh_count": curr_c, + "date": eff_date, + "idempotent": True, + } + + # Atomically apply aggregate to daily_progress, tasbeeh_daily, and tasbeeh_stats using canonical session fields + new_tasbeeh_count = await _retry_transient_mongo( + lambda: _apply_tasbeeh_rollup( + uid=uid, + date_str=eff_date, + count=eff_count, + dhikr=eff_dhikr, + arabic=eff_arabic, + laps=eff_laps, + duration_seconds=eff_duration, + event_id=event_id, + now_utc=now_utc, + session_state=existing_state, + ), + op_name="tasbeeh_log_rollup", + ) + + # Mark aggregate_applied True on the session document + await _retry_transient_mongo( + lambda: tasbeeh_collection.update_one( + {"_id": doc_id}, + {"$set": {"aggregate_applied": True}}, + ), + op_name="tasbeeh_log_mark_applied", + ) + + realtime.publish(uid, "prayer_progress_updated", { + "date": eff_date, + "tasbeeh_count": new_tasbeeh_count, + }) + + _invalidate_all_progress_caches(uid) + + return { + "status": "success", + "logged_count": eff_count, + "tasbeeh_count": new_tasbeeh_count, + "date": eff_date, + "idempotent": is_duplicate, + } + + +@router.post('/tasbeeh/batch-sync') +@limiter.limit("60/minute") +async def batch_sync_tasbeeh( + payload: TasbeehBatchSyncReq, + request: Request, + user: dict = Depends(get_current_user) +): + """Batched offline synchronization endpoint for digital Tasbeeh. + + Accepts up to 500 queued offline sessions in a single network round-trip. + Deduplicates sessions via unique event IDs and updates lifetime rollups. + """ + uid = user["uid"] + now_utc = datetime.datetime.utcnow().isoformat() + "Z" + today_str = _progress_local_date(payload.tz).isoformat() + synced_count = 0 + duplicate_count = 0 + conflict_event_ids = [] + today_tasbeeh_count = 0 + + for item in payload.sessions: + date_str = _validate_progress_date(item.date, payload.tz, max_past_days=30) + event_id = item.event_id or str(uuid.uuid4()) + doc_id = f"tasbeeh:{uid}:{event_id}" + req_hash = _tasbeeh_payload_hash( + date_str=date_str, + count=item.count, + dhikr=item.dhikr, + arabic=item.arabic or "", + laps=item.laps, + duration_seconds=item.duration_seconds, + ) + + session_doc = { + "_id": doc_id, + "uid": uid, + "event_id": event_id, + "date": date_str, + "timestamp": now_utc, + "count": item.count, + "dhikr": item.dhikr, + "arabic": item.arabic or "", + "laps": item.laps, + "target": item.target, + "duration_seconds": item.duration_seconds, + "payload_hash": req_hash, + "daily_progress_applied": False, + "tasbeeh_daily_applied": False, + "tasbeeh_stats_applied": False, + "aggregate_applied": False, + } + + try: + await _retry_transient_mongo( + lambda: tasbeeh_collection.insert_one(session_doc), + op_name="tasbeeh_batch_insert", + ) + synced_count += 1 + new_c = await _retry_transient_mongo( + lambda: _apply_tasbeeh_rollup( + uid=uid, + date_str=date_str, + count=item.count, + dhikr=item.dhikr, + arabic=item.arabic or "", + laps=item.laps, + duration_seconds=item.duration_seconds, + event_id=event_id, + now_utc=now_utc, + ), + op_name="tasbeeh_batch_rollup", + ) + await _retry_transient_mongo( + lambda: tasbeeh_collection.update_one({"_id": doc_id}, {"$set": {"aggregate_applied": True}}), + op_name="tasbeeh_batch_mark_applied", + ) + if date_str == today_str: + today_tasbeeh_count = max(today_tasbeeh_count, new_c) + except DuplicateKeyError: + existing = await _retry_transient_mongo( + lambda: tasbeeh_collection.find_one({"_id": doc_id}), + op_name="tasbeeh_batch_find_existing", + ) + if existing and not _tasbeeh_existing_matches( + existing, + date_str=date_str, + count=item.count, + dhikr=item.dhikr, + arabic=item.arabic or "", + laps=item.laps, + duration_seconds=item.duration_seconds, + req_hash=req_hash, + ): + conflict_event_ids.append(event_id) + continue + duplicate_count += 1 + if existing and not existing.get("aggregate_applied"): + eff_date, eff_count, eff_dhikr, eff_arabic, eff_laps, eff_duration = ( + _extract_tasbeeh_canonical_fields( + existing, + date_str=date_str, + count=item.count, + dhikr=item.dhikr, + arabic=item.arabic or "", + laps=item.laps, + duration_seconds=item.duration_seconds, + ) + ) + new_c = await _retry_transient_mongo( + lambda: _apply_tasbeeh_rollup( + uid=uid, + date_str=eff_date, + count=eff_count, + dhikr=eff_dhikr, + arabic=eff_arabic, + laps=eff_laps, + duration_seconds=eff_duration, + event_id=event_id, + now_utc=now_utc, + session_state=existing, + ), + op_name="tasbeeh_batch_dup_rollup", + ) + await _retry_transient_mongo( + lambda: tasbeeh_collection.update_one({"_id": doc_id}, {"$set": {"aggregate_applied": True}}), + op_name="tasbeeh_batch_dup_mark_applied", + ) + if eff_date == today_str: + today_tasbeeh_count = max(today_tasbeeh_count, new_c) + + if conflict_event_ids and synced_count == 0 and duplicate_count == 0: + raise HTTPException( + status_code=409, + detail=f"Idempotency conflict: conflicting payload replayed for event_id(s): {', '.join(conflict_event_ids)}", + ) + + if today_tasbeeh_count > 0: + realtime.publish(uid, "prayer_progress_updated", { + "date": today_str, + "tasbeeh_count": today_tasbeeh_count, + }) + + _invalidate_all_progress_caches(uid) + + return { + "status": "success", + "total_received": len(payload.sessions), + "synced_count": synced_count, + "duplicates_ignored": duplicate_count, + "conflicts_rejected": len(conflict_event_ids), + "conflict_event_ids": conflict_event_ids, + "today_count": today_tasbeeh_count, + "date": today_str, + } + + +_TASBEEH_SUMMARY_TTL = 15.0 + +@router.get('/tasbeeh/summary') +@limiter.limit("1200/minute") +async def get_tasbeeh_summary( + request: Request, + tz: float = 0.0, + fajr: Optional[str] = None, + date: Optional[str] = None, + month: Optional[str] = None, + user: dict = Depends(get_current_user) +): + uid = user["uid"] + if isinstance(date, str) and date.strip(): + try: + today = datetime.date.fromisoformat(date.strip()) + except Exception: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + else: + today = _progress_local_date(tz, fajr if isinstance(fajr, str) else None) + today_str = today.isoformat() + month_start = today.replace(day=1).isoformat() + month_elapsed_days = max(1, today.day) + + # Resolve target month range (TDS-002: historical month support) + if month: + try: + parts = [int(p) for p in month.strip().split("-")] + target_start = datetime.date(parts[0], parts[1], 1) + if parts[1] == 12: + next_m = datetime.date(parts[0] + 1, 1, 1) + else: + next_m = datetime.date(parts[0], parts[1] + 1, 1) + target_end = next_m - datetime.timedelta(days=1) + is_target_current = (target_start.year == today.year and target_start.month == today.month) + eff_month_start = target_start.isoformat() + eff_month_end = today_str if is_target_current else target_end.isoformat() + eff_elapsed_days = today.day if is_target_current else target_end.day + except Exception: + eff_month_start = month_start + eff_month_end = today_str + eff_elapsed_days = month_elapsed_days + else: + eff_month_start = month_start + eff_month_end = today_str + eff_elapsed_days = month_elapsed_days + + cache_key = f"{uid}:{tz}:{today_str}:{eff_month_start}" + ent = _TASBEEH_SUMMARY_CACHE.get(cache_key) + if ent and (time.monotonic() - ent[0]) < _TASBEEH_SUMMARY_TTL: + return Response(content=ent[1], media_type="application/json") + + # 1. Fetch or initialize lifetime stats (O(1)) + stats = await _ensure_user_tasbeeh_stats(uid, today_str) + + # 2. Try fast-path via tasbeeh_daily_collection (O(31)) + month_daily_sums = {} + month_active_dates = set() + month_seconds = 0 + month_laps = 0 + month_dhikr_totals = {} + + daily_docs = [] + try: + daily_docs = await tasbeeh_daily_collection.find( + {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}} + ).to_list(length=35) + except Exception: + daily_docs = [] + + covered_dates = set() + if daily_docs: + for d in daily_docs: + dt = d.get("date") + cnt = int(d.get("total_count", 0) or 0) + if dt: + covered_dates.add(dt) + month_daily_sums[dt] = cnt + if cnt > 0: + month_active_dates.add(dt) + month_seconds += int(d.get("total_seconds", 0) or 0) + month_laps += int(d.get("total_laps", 0) or 0) + for dk, dv in (d.get("dhikrs") or {}).items(): + d_name = dv.get("dhikr") or dk + d_c = int(dv.get("count", 0) or 0) + month_dhikr_totals[d_name] = month_dhikr_totals.get(d_name, 0) + d_c + + # M-08: Reconcile any missing month dates from raw tasbeeh_collection sessions + try: + match_query: dict = {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}} + if covered_dates: + match_query["date"] = {"$gte": eff_month_start, "$lte": eff_month_end, "$nin": list(covered_dates)} + + daily_session_sums = await tasbeeh_collection.aggregate([ + {"$match": match_query}, + {"$group": { + "_id": "$date", + "count": {"$sum": "$count"}, + "seconds": {"$sum": "$duration_seconds"}, + "laps": {"$sum": "$laps"} + }} + ]).to_list(length=35) + for ds in daily_session_sums: + dt = ds.get("_id") + c = int(ds.get("count", 0) or 0) + if dt: + if c > 0: + month_active_dates.add(dt) + month_daily_sums[dt] = c + month_seconds += int(ds.get("seconds", 0) or 0) + month_laps += int(ds.get("laps", 0) or 0) + + # Also aggregate dhikr totals for missing days + if daily_session_sums: + month_dhikr_agg = await tasbeeh_collection.aggregate([ + {"$match": match_query}, + {"$group": {"_id": "$dhikr", "count": {"$sum": "$count"}}}, + {"$sort": {"count": -1}}, + {"$limit": 8} + ]).to_list(length=8) + for item in month_dhikr_agg: + dn = item.get("_id") or "General Dhikr" + month_dhikr_totals[dn] = month_dhikr_totals.get(dn, 0) + int(item.get("count", 0) or 0) + except Exception as e: + logger.warning(f"Tasbeeh summary month reconciliation error: {e}") + + # Cross-check month days with daily_progress_collection + try: + daily_month_docs = await daily_progress_collection.find( + {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}, + {"_id": 0, "date": 1, "tasbeeh_count": 1} + ).to_list(length=35) + for dm in daily_month_docs: + dt = dm.get("date") + tc = int(dm.get("tasbeeh_count", 0) or 0) + if tc > 0: + month_active_dates.add(dt) + month_daily_sums[dt] = max(month_daily_sums.get(dt, 0), tc) + except Exception: + pass + + month_total = sum(month_daily_sums.values()) if month_daily_sums else 0 + today_count = month_daily_sums.get(today_str, 0) + month_best_day = max(month_daily_sums.values()) if month_daily_sums else today_count + month_avg_daily = round(month_total / float(eff_elapsed_days)) if month_total > 0 else 0 + + sorted_dhikrs = sorted(month_dhikr_totals.items(), key=lambda x: x[1], reverse=True)[:8] + month_top_dhikrs = [] + for dn, cnt in sorted_dhikrs: + if cnt <= 0: + continue + pct = round((cnt / float(month_total)) * 100, 1) if month_total > 0 else 0.0 + month_top_dhikrs.append({"dhikr": dn, "count": cnt, "pct": pct}) + + # Streak calculation (TDS-STREAK: strictly requires >= 297 beads a day) + current_tasbeeh_streak, longest_tasbeeh_streak, last_act = await _calculate_tasbeeh_streak(uid, today) + streak = current_tasbeeh_streak + try: + await tasbeeh_stats_collection.update_one( + {"_id": uid}, + { + "$set": { + "streak.current": current_tasbeeh_streak, + "streak.last_active_date": last_act, + }, + "$max": { + "streak.longest": longest_tasbeeh_streak, + } + } + ) + except Exception: + pass + + # All-time statistics from tasbeeh_stats + all_time_beads = max((stats or {}).get("lifetime_count", 0), month_total) + all_time_seconds = max((stats or {}).get("lifetime_seconds", 0), month_seconds) + all_time_laps = max((stats or {}).get("lifetime_laps", 0), month_laps) + all_time_active_days = max((stats or {}).get("active_days_count", 0), len(month_active_dates)) + all_time_avg_daily = round(all_time_beads / float(max(1, all_time_active_days))) if all_time_active_days > 0 else 0 + all_time_best_day = max((stats or {}).get("best_day", {}).get("count", 0), month_best_day) + + # Top Dhikrs across lifetime + all_dhikrs_dict = (stats or {}).get("dhikrs", {}) + all_time_top_dhikrs = [] + if all_dhikrs_dict: + sorted_all_dhikrs = sorted( + all_dhikrs_dict.values(), + key=lambda x: x.get("count", 0), + reverse=True + )[:8] + for it in sorted_all_dhikrs: + dn = it.get("dhikr") or "General Dhikr" + cnt = int(it.get("count", 0) or 0) + if cnt <= 0: + continue + pct = round((cnt / float(all_time_beads)) * 100, 1) if all_time_beads > 0 else 0.0 + all_time_top_dhikrs.append({"dhikr": dn, "count": cnt, "pct": pct}) + if not all_time_top_dhikrs: + all_time_top_dhikrs = month_top_dhikrs + + # Recent 25 sessions (indexed lookup) + recent_sessions = [] + try: + recent_sessions = await tasbeeh_collection.find( + {"uid": uid, "date": {"$gte": eff_month_start, "$lte": eff_month_end}}, + {"_id": 0} + ).sort("timestamp", -1).to_list(length=25) + if not recent_sessions: + recent_sessions = await tasbeeh_collection.find({"uid": uid}, {"_id": 0}).sort("timestamp", -1).to_list(length=25) + except Exception: + recent_sessions = [] + + res = { + "today_count": today_count, + "month_total": month_total, + "month_elapsed_days": eff_elapsed_days, + "month_active_days": len(month_active_dates), + "active_days": len(month_active_dates), + "streak_days": streak, + "best_day_count": month_best_day, + "avg_daily": month_avg_daily, + "total_seconds": month_seconds, + "total_laps": month_laps, + "top_dhikrs": month_top_dhikrs, + + "all_time_total": all_time_beads, + "all_time_active_days": all_time_active_days, + "all_time_avg_daily": all_time_avg_daily, + "all_time_best_day": all_time_best_day, + "all_time_total_seconds": all_time_seconds, + "all_time_total_laps": all_time_laps, + "all_time_top_dhikrs": all_time_top_dhikrs, + + "recent_sessions": recent_sessions, + "date": today_str, + } + raw_bytes = json.dumps(res, default=str).encode("utf-8") + _sweep_cache_if_needed(_TASBEEH_SUMMARY_CACHE) + _TASBEEH_SUMMARY_CACHE[cache_key] = (time.monotonic(), raw_bytes) + return Response(content=raw_bytes, media_type="application/json") + + +@router.get('/tasbeeh/day') +@limiter.limit("600/minute") +async def get_tasbeeh_day( + request: Request, + date: str, + tz: float = 0.0, + skip: int = Query(0, ge=0), + limit: int = Query(100, ge=1, le=500), + user: dict = Depends(get_current_user) +): + """Retrieves full Tasbeeh breakdown and sessions for a specific day. + + Protected under Progress Plus / Pro tier for previous historical months. + Uses user-local timezone for boundary checks (TDS-013). + Supports cursor pagination via skip and limit (TDS-004). + """ + uid = user["uid"] + date_clean = (date or "").strip() + try: + parts = [int(p) for p in date_clean.split("-")] + day_date = datetime.date(parts[0], parts[1], parts[2]) + except Exception: + raise HTTPException(status_code=400, detail="Invalid date format, expected YYYY-MM-DD") + + today = _progress_local_date(tz) + is_current_month = (day_date.year == today.year and day_date.month == today.month) + if not is_current_month: + from app.auth_deps import has_user_historical_progress + if not await has_user_historical_progress(user): + return { + "locked": True, + "message": "Historical progress is locked for previous months. Upgrade to Progress Plus (₹60/mo) or Sahih Pro to view your saved history.", + "date": date_clean, + "total_count": 0, + "total_seconds": 0, + "sessions": [], + "dhikr_breakdown": [], + "total_sessions": 0, + "skip": skip, + "limit": limit, + "has_more": False, + } + + total_count = 0 + total_seconds = 0 + top_dhikrs = [] + + # 1. Try reading from tasbeeh_daily_collection + day_doc = None + try: + day_doc = await tasbeeh_daily_collection.find_one({"_id": f"{uid}:{date_clean}"}) + except Exception: + day_doc = None + + if day_doc: + total_count = int(day_doc.get("total_count", 0) or 0) + total_seconds = int(day_doc.get("total_seconds", 0) or 0) + dhikrs_map = day_doc.get("dhikrs", {}) or {} + sorted_d = sorted(dhikrs_map.values(), key=lambda x: x.get("count", 0), reverse=True) + for it in sorted_d: + dn = it.get("dhikr") or "General Dhikr" + ar = it.get("arabic") or "" + c = int(it.get("count", 0) or 0) + if c <= 0: + continue + pct = round((c / float(total_count) * 100), 1) if total_count > 0 else 0.0 + top_dhikrs.append({"dhikr": dn, "arabic": ar, "count": c, "pct": pct}) + else: + # Fallback to session aggregate (for unmigrated records or test mocks) + try: + day_agg = await tasbeeh_collection.aggregate([ + {"$match": {"uid": uid, "date": date_clean}}, + {"$group": { + "_id": None, + "total_count": {"$sum": "$count"}, + "total_seconds": {"$sum": "$duration_seconds"} + }} + ]).to_list(length=1) + total_count = int(day_agg[0]["total_count"]) if day_agg else 0 + total_seconds = int(day_agg[0]["total_seconds"]) if day_agg else 0 + + day_dhikr_agg = await tasbeeh_collection.aggregate([ + {"$match": {"uid": uid, "date": date_clean}}, + {"$group": { + "_id": "$dhikr", + "arabic": {"$first": "$arabic"}, + "count": {"$sum": "$count"} + }}, + {"$sort": {"count": -1}} + ]).to_list(length=50) + for item in day_dhikr_agg: + d_name = item.get("_id") or "General Dhikr" + d_arabic = item.get("arabic") or "" + c = int(item.get("count", 0) or 0) + if c <= 0: + continue + pct = round((c / float(total_count) * 100), 1) if total_count > 0 else 0.0 + top_dhikrs.append({"dhikr": d_name, "arabic": d_arabic, "count": c, "pct": pct}) + except Exception as e: + logger.warning(f"Error aggregating tasbeeh day: {e}") + + # Cross-check with daily_progress + try: + daily_doc = await daily_progress_collection.find_one({"uid": uid, "date": date_clean}, {"_id": 0, "tasbeeh_count": 1}) or {} + daily_t = int(daily_doc.get("tasbeeh_count", 0) or 0) + total_count = max(total_count, daily_t) + except Exception: + pass + + # Paged session retrieval with total count & has_more (TDS-004) + try: + skip_int = int(skip if isinstance(skip, (int, float, str)) else 0) + except Exception: + skip_int = 0 + try: + limit_int = int(limit if isinstance(limit, (int, float, str)) else 100) + except Exception: + limit_int = 100 + limit_clamped = min(max(1, limit_int), 500) + total_sessions_count = 0 + sessions = [] + try: + total_sessions_count = await tasbeeh_collection.count_documents({"uid": uid, "date": date_clean}) + sessions = await tasbeeh_collection.find( + {"uid": uid, "date": date_clean}, + {"_id": 0} + ).sort("timestamp", -1).skip(skip_int).to_list(length=limit_clamped) + except Exception as e: + logger.warning(f"Error fetching tasbeeh sessions: {e}") + + has_more = (skip_int + len(sessions)) < total_sessions_count + + return { + "locked": False, + "date": date_clean, + "total_count": total_count, + "total_seconds": total_seconds, + "sessions": sessions, + "dhikr_breakdown": top_dhikrs, + "total_sessions": total_sessions_count, + "skip": skip_int, + "limit": limit_clamped, + "has_more": has_more, + } + + +class RequestEmailChangeReq(BaseModel): + new_email: str = Field(..., min_length=5, max_length=150) + + +class VerifyEmailChangeReq(BaseModel): + new_email: str = Field(..., min_length=5, max_length=150) + otp: str = Field(..., min_length=4, max_length=10) + + +@router.post('/request-email-change') +@limiter.limit("5/minute") +async def request_email_change(payload: RequestEmailChangeReq, request: Request, user: dict = Depends(get_current_user)): + import random + from app.database import db + uid = user["uid"] + primary_email = user.get("email") or user.get("primary_email") + if not primary_email: + raise HTTPException(status_code=400, detail="Primary account email not found.") + + new_email_clean = payload.new_email.strip().lower() + if not new_email_clean or "@" not in new_email_clean: + raise HTTPException(status_code=400, detail="Invalid new email format.") + + if new_email_clean == primary_email.lower(): + raise HTTPException(status_code=400, detail="New email is identical to current primary email.") + + # Prevent duplicate / conflicting email ownership in MongoDB + conflict_user = await users_collection.find_one({ + "$or": [{"email": new_email_clean}, {"primary_email": new_email_clean}], + "uid": {"$ne": uid} + }) + if conflict_user: + raise HTTPException(status_code=409, detail="An account with this email address already exists.") + + # Check collision in Firebase Auth if available + try: + fb_conflict = firebase_auth.get_user_by_email(new_email_clean) + if fb_conflict and fb_conflict.uid != uid: + raise HTTPException(status_code=409, detail="This email is already registered with another account in Firebase.") + except HTTPException: + raise + except Exception: + pass + + import secrets + otp = f"{secrets.randbelow(900000) + 100000}" + now = datetime.datetime.utcnow() + expires_at = now + datetime.timedelta(minutes=15) + + from app.database import email_change_requests_collection + await email_change_requests_collection.update_one( + {"uid": uid}, + {"$set": { + "uid": uid, + "primary_email": primary_email, + "new_email": new_email_clean, + "otp": otp, + "attempts": 0, + "max_attempts": 5, + "created_at": now, + "expires_at": expires_at, + }}, + upsert=True + ) + + # Dispatch email to PRIMARY email via SMTP or Resend + email_sent = False + send_error = "" + + # 1. Check Resend API first if configured + resend_api_key = os.getenv("RESEND_API_KEY") + if resend_api_key: + try: + import urllib.request + import json as py_json + req_data = { + "from": os.getenv("EMAIL_FROM", "Sahih Islam "), + "to": [primary_email], + "subject": "Sahih Islam - Confirm Email Change Authorization", + "html": f""" +
+

Sahih Islam

+

Email Change Authorization

+

+ Assalamu Alaikum,

+ A request was made to update your Sahih Islam login email from {primary_email} to {new_email_clean}. +

+
+ 6-Digit Authorization Code + {otp} +
+

+ • Enter this code into the Sahih Islam app to authorize the transfer.
+ • This code is valid for 15 minutes.
+ • If you did not make this request, simply ignore this email. Your primary login remains unchanged and secure. +

+
+ """ + } + import httpx + async with httpx.AsyncClient(timeout=8.0) as client: + resp = await client.post( + "https://api.resend.com/emails", + json=req_data, + headers={ + "Authorization": f"Bearer {resend_api_key}", + "Content-Type": "application/json" + } + ) + if resp.status_code in (200, 201): + email_sent = True + except Exception as ex: + logger.warning(f"Resend email dispatch failed: {ex}") + send_error = str(ex) + + # 2. Check SMTP if not yet sent (executed in threadpool to prevent event loop freeze) + if not email_sent: + smtp_host = os.getenv("SMTP_HOST") + smtp_port = int(os.getenv("SMTP_PORT", 587)) + smtp_user = os.getenv("SMTP_USER") + smtp_pass = os.getenv("SMTP_PASS") + if smtp_host and smtp_user and smtp_pass: + try: + import smtplib + from email.mime.multipart import MIMEMultipart + from email.mime.text import MIMEText + + msg = MIMEMultipart("alternative") + msg['Subject'] = "Sahih Islam - Confirm Email Change Authorization" + msg['From'] = smtp_user + msg['To'] = primary_email + + text_content = ( + f"Assalamu Alaikum,\n\n" + f"A request was made to update your Sahih Islam account email to: {new_email_clean}\n\n" + f"Your 6-digit authorization code is: {otp}\n\n" + f"This code will expire in 15 minutes.\n" + f"Enter this code in the Sahih Islam app to authorize the update.\n\n" + f"If you did NOT request this, ignore this email. Your primary email remains active and secure." + ) + html_content = f""" +
+

Sahih Islam

+

Email Change Authorization

+

+ Assalamu Alaikum,

+ A request was made to update your Sahih Islam login email from {primary_email} to {new_email_clean}. +

+
+ 6-Digit Authorization Code + {otp} +
+

+ • Enter this code into the Sahih Islam app to authorize the transfer.
+ • This code is valid for 15 minutes.
+ • If you did not make this request, simply ignore this email. Your primary login remains unchanged and secure. +

+
+ """ + + msg.attach(MIMEText(text_content, "plain")) + msg.attach(MIMEText(html_content, "html")) + + def _send_smtp_sync(): + if smtp_port == 465: + with smtplib.SMTP_SSL(smtp_host, smtp_port, timeout=12) as s: + s.login(smtp_user, smtp_pass) + s.send_message(msg) + else: + with smtplib.SMTP(smtp_host, smtp_port, timeout=12) as s: + s.starttls() + s.login(smtp_user, smtp_pass) + s.send_message(msg) + + await asyncio.to_thread(_send_smtp_sync) + email_sent = True + except Exception as ex: + logger.warning(f"SMTP send failed: {ex}") + send_error = str(ex) + + if not email_sent: + logger.warning(f"Email could not be dispatched to {primary_email}. Error: {send_error}") + raise HTTPException( + status_code=503, + detail=f"Unable to send authorization code to {primary_email}. SMTP server is not configured or encountered an error. Please check server SMTP configuration." + ) + + realtime.publish(uid, "email_change_requested", { + "primary_email": primary_email, + "new_email": new_email_clean, + }) + + return { + "status": "ok", + "message": f"Authorization code sent to your primary email ({primary_email}).", + "primary_email": primary_email, + "email_sent": True + } + + +@router.post('/verify-email-change') +@limiter.limit("10/minute") +async def verify_email_change(payload: VerifyEmailChangeReq, request: Request, user: dict = Depends(get_current_user)): + from app.database import email_change_requests_collection + uid = user["uid"] + req_doc = await email_change_requests_collection.find_one({"uid": uid}) + if not req_doc: + raise HTTPException(status_code=400, detail="No pending email change request found.") + + import hmac + expires_at = req_doc.get("expires_at") + if isinstance(expires_at, datetime.datetime) and expires_at < datetime.datetime.utcnow(): + await email_change_requests_collection.delete_one({"uid": uid}) + raise HTTPException(status_code=400, detail="Verification code has expired. Please request a new one.") + + attempts = int(req_doc.get("attempts", 0)) + 1 + max_attempts = int(req_doc.get("max_attempts", 5)) + if attempts > max_attempts: + await email_change_requests_collection.delete_one({"uid": uid}) + raise HTTPException( + status_code=429, + detail="Too many failed verification attempts. Request invalidated. Please request a new code." + ) + + try: + upd = email_change_requests_collection.update_one({"uid": uid}, {"$set": {"attempts": attempts}}) + if asyncio.iscoroutine(upd) or hasattr(upd, "__await__"): + await upd + except Exception as upd_err: + logger.debug(f"Attempt counter update note: {upd_err}") + + expected_otp = str(req_doc.get("otp", "")).strip() + provided_otp = str(payload.otp).strip() + if not (expected_otp and hmac.compare_digest(expected_otp, provided_otp)): + remaining = max(0, max_attempts - attempts) + raise HTTPException( + status_code=400, + detail=f"Invalid verification code. Authorization denied. Primary email remains unchanged. ({remaining} attempts remaining)" + ) + + new_email_clean = payload.new_email.strip().lower() + if req_doc.get("new_email") != new_email_clean: + raise HTTPException(status_code=400, detail="The email being verified does not match the requested address.") + + # Re-check collision in MongoDB before finalizing + conflict_user = await users_collection.find_one({ + "$or": [{"email": new_email_clean}, {"primary_email": new_email_clean}], + "uid": {"$ne": uid} + }) + if conflict_user: + raise HTTPException(status_code=409, detail="This email is already registered with another account.") + + # 1. Update Firebase Auth user (fail-closed so Mongo and Firebase cannot diverge) + try: + firebase_auth.update_user(uid, email=new_email_clean) + except Exception as fb_err: + logger.warning(f"Firebase update_user error: {fb_err}") + err_msg = str(fb_err).lower() + if "already exists" in err_msg or "already in use" in err_msg: + raise HTTPException(status_code=409, detail="This email is already registered with another account in Firebase.") + raise HTTPException(status_code=503, detail="Failed to synchronize email change with identity provider.") + + # 2. Update MongoDB document + now = datetime.datetime.utcnow() + await users_collection.update_one( + {"uid": uid}, + {"$set": { + "email": new_email_clean, + "primary_email": new_email_clean, + "updated_at": now, + "analytics.updated_at": now, + }} + ) + + # 3. Permanent Audit Log + try: + from app.database import email_change_logs_collection + await email_change_logs_collection.insert_one({ + "uid": uid, + "old_email": req_doc.get("primary_email"), + "new_email": new_email_clean, + "changed_at": now, + "status": "COMPLETED", + "method": "VERIFIED_OTP_FLOW" + }) + except Exception as log_err: + logger.warning(f"Failed to record email_change_logs: {log_err}") + + # 4. Clean up request and invalidate cache + await email_change_requests_collection.delete_one({"uid": uid}) + await invalidate_user_cache(uid) + + realtime.publish(uid, "email_changed", {"email": new_email_clean}) + + return { + "status": "ok", + "message": "Email verified and updated successfully. New email is now your primary login.", + "email": new_email_clean, + } + + +class RecordEmailChangeReq(BaseModel): + old_email: str = Field(..., max_length=150) + new_email: str = Field(..., max_length=150) + uid: Optional[str] = Field(None, max_length=128) + + +@router.post('/record-email-change') +async def record_email_change( + payload: RecordEmailChangeReq, + request: Request, + current_user: dict = Depends(get_current_user), +): + """ + Records email change post-verification. + NEW-005 / SI-SEC-015 Hardening: + - Strictly bound to authenticated current_user. NEVER trusts caller-supplied payload.uid! + - Fail-closed if Firebase identity verification cannot be confirmed. + - Prevents takeover of arbitrary user accounts. + """ + uid = current_user["uid"] + clean_new = payload.new_email.strip().lower() + clean_old = payload.old_email.strip().lower() + + if not clean_new or "@" not in clean_new: + raise HTTPException(status_code=400, detail="Invalid new email address.") + + # Require authoritative verification: identity provider MUST have new_email as verified email + try: + fb_user = firebase_auth.get_user(uid) + if not (fb_user and fb_user.email and fb_user.email.lower() == clean_new): + raise HTTPException( + status_code=400, + detail="Firebase email does not match requested new email. Verification in identity provider required." + ) + except HTTPException: + raise + except Exception as ex: + logger.error(f"[record_email_change] Firebase verification error for {uid}: {ex}") + raise HTTPException(status_code=503, detail=f"Cannot verify email with identity provider: {ex}") + + # Collision check in MongoDB + conflict_user = await users_collection.find_one({ + "$or": [{"email": clean_new}, {"primary_email": clean_new}], + "uid": {"$ne": uid} + }) + if conflict_user: + raise HTTPException(status_code=409, detail="An account with this email address already exists.") + + from app.database import email_change_logs_collection + now = datetime.datetime.utcnow() + + # 1. Permanent Audit Log in MongoDB + try: + await email_change_logs_collection.insert_one({ + "uid": uid, + "old_email": clean_old, + "new_email": clean_new, + "changed_at": now, + "status": "COMPLETED", + "method": "FIREBASE_DUAL_LINK_VERIFICATION" + }) + except Exception as ex: + logger.warning(f"Failed to record email_change_logs: {ex}") + + # 2. Update user document email fields in MongoDB + # Only email and primary_email are changed. + # All other user fields (favorites, masjids, subscriptions, pro status, etc.) remain 100% intact! + await users_collection.update_one( + {"uid": uid}, + {"$set": { + "email": clean_new, + "primary_email": clean_new, + "updated_at": now, + "analytics.updated_at": now, + }} + ) + await invalidate_user_cache(uid) + + realtime.publish(uid, "email_changed", {"email": clean_new}) + + return { + "status": "ok", + "message": f"Email successfully updated to {clean_new}. Change recorded in audit log.", + "email": clean_new, + } + + +# ========================================================= +# WORLDWIDE CLIENT RUNTIME ERROR TELEMETRY +# ========================================================= +import hashlib +import uuid +import json + +def _redact_sensitive_text(text: str) -> str: + """Scrub sensitive tokens, secrets, credentials, and emails from client error reports.""" + if not text: + return "" + # Redact Bearer tokens + text = re.sub(r'Bearer\s+[A-Za-z0-9_\-\.]+', 'Bearer [REDACTED_TOKEN]', text, flags=re.IGNORECASE) + # Redact JWTs (three base64 segments separated by dots) + text = re.sub(r'\beyJ[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\b', '[REDACTED_JWT]', text) + # Redact API keys / secrets / password patterns + text = re.sub(r'(api[_\-]?key|secret|password|passwd|auth[_\-]?token|private[_\-]?key)\s*[:=]\s*\S+', r'\1=[REDACTED]', text, flags=re.IGNORECASE) + # Mask email addresses + text = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b', '[REDACTED_EMAIL]', text) + return text + +def _mask_ip(ip: str) -> str: + """Mask client IP address to preserve privacy while retaining geographic routing telemetry.""" + if not ip or ip in ("unknown", "127.0.0.1", "localhost", "testclient"): + return ip or "unknown" + if "." in ip: + parts = ip.split(".") + if len(parts) == 4: + return f"{parts[0]}.{parts[1]}.{parts[2]}.xxx" + if ":" in ip: + parts = ip.split(":") + if len(parts) >= 4: + return f"{':'.join(parts[:3])}::xxxx" + return "masked" + +class ClientErrorReportReq(BaseModel): + message: Optional[str] = Field(None, max_length=5000) + error_type: Optional[str] = Field("UnhandledError", max_length=150) + stack_trace: Optional[str] = Field("", max_length=20000) + route: Optional[str] = Field("/unknown", max_length=250) + context: Optional[str] = Field("", max_length=500) + platform: Optional[str] = Field("unknown", max_length=50) + device_model: Optional[str] = Field("unknown", max_length=150) + os_version: Optional[str] = Field("unknown", max_length=100) + app_version: Optional[str] = Field("1.0.0", max_length=50) + event_id: Optional[str] = Field(None, max_length=64) + # Legacy / client mismatch aliases accepted safely without 422 errors + error_message: Optional[str] = Field(None, max_length=5000) + screen_route: Optional[str] = Field(None, max_length=250) + device_info: Optional[Union[Dict[str, Any], str]] = None + context_data: Optional[Union[Dict[str, Any], str]] = None + # Client-supplied identity fields are accepted in payload to avoid 422, but STRICTLY DISCARDED by auth logic + user_id: Optional[str] = Field(None, max_length=128) + user_email: Optional[str] = Field(None, max_length=150) + user_role: Optional[str] = Field(None, max_length=50) + is_pro: Optional[bool] = None + metadata: Optional[Dict[str, Any]] = None + + @model_validator(mode='before') + @classmethod + def harmonize_payload(cls, data: Any) -> Any: + if not isinstance(data, dict): + return data + # 1. Map error_message -> message + if not data.get("message") and data.get("error_message"): + data["message"] = data["error_message"] + elif not data.get("message") and data.get("error"): + data["message"] = str(data["error"]) + elif not data.get("message"): + data["message"] = "Unknown Application Error" + + # 2. Map screen_route -> route + if not data.get("route") and data.get("screen_route"): + data["route"] = data["screen_route"] + + # 3. Map context_data -> context + if not data.get("context") and data.get("context_data"): + ctx = data["context_data"] + if isinstance(ctx, dict): + data["context"] = json.dumps(ctx)[:500] + else: + data["context"] = str(ctx)[:500] + + # 4. Map device_info -> device_model / os_version + dev = data.get("device_info") + if isinstance(dev, dict): + if not data.get("device_model") or data.get("device_model") == "unknown": + data["device_model"] = str(dev.get("targetPlatform") or dev.get("model") or "unknown")[:150] + if not data.get("os_version") or data.get("os_version") == "unknown": + data["os_version"] = str(dev.get("osVersion") or dev.get("version") or "unknown")[:100] + return data + +def _categorize_error(msg: str, err_type: str, stack: str) -> str: + core_text = (f"{err_type} {msg}").lower() + full_text = (f"{err_type} {msg} {stack}").lower() + + # Network / API + if any(k in full_text for k in ["socketexception", "httpexception", "handshakeexception", "timeoutexception", "failed host lookup", "clientexception", "connection refused", "network", "dioerror"]): + return "Network / API" + # Auth & Security + if any(k in full_text for k in ["firebaseauth", "unauthorized", "token", "forbidden", "invalid-credential", "jwt", "permission-denied", "401", "403"]): + return "Auth & Security" + # State & Logic + if any(k in full_text for k in ["rangeerror", "formatstatus", "formatexception", "type 'null'", "nosuchmethoderror", "typeerror", "cast error", "null check operator"]): + return "State & Logic" + # Storage & Cache + if any(k in full_text for k in ["database", "mongo", "hive", "sharedpreferences", "indexeddb", "sqlite", "cache"]): + return "Storage & Cache" + # UI / Rendering + if any(k in core_text for k in ["renderflex", "overflowed", "pageview", "positions.isnotempty", "unbounded", "renderbox", "paint", "setstate", "widget", "viewport", "flex"]): + return "UI / Rendering" + if any(k in full_text for k in ["renderflex", "overflowed", "renderbox", "viewport was given unbounded"]): + return "UI / Rendering" + return "General / System" + +def _compute_fingerprint(msg: str, err_type: str, stack: str) -> tuple[str, str]: + norm_msg = re.sub(r'0x[0-9a-fA-F]+', '0x...', msg or "") + norm_msg = re.sub(r'\b\d{4,5}\b', 'PORT', norm_msg) + norm_msg = re.sub(r'\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b', 'UUID', norm_msg) + norm_msg = re.sub(r'\b[0-9a-fA-F]{24}\b', 'HEXID', norm_msg) + norm_msg = norm_msg.strip() + + first_line = norm_msg.split('\n')[0].strip() + if len(first_line) > 160: + first_line = first_line[:157] + '...' + title = f"{err_type}: {first_line}" if err_type and err_type not in first_line else first_line + if not title: + title = "Unknown Application Error" + + stack_sig = "" + if stack: + frames = [] + for line in stack.split('\n'): + sline = line.strip() + if not sline or sline.startswith('package:flutter/') or sline.startswith('dart-sdk/'): + continue + frames.append(re.sub(r':\d+:\d+', ':#:#', sline)) + if not frames: + frames = [re.sub(r':\d+:\d+', ':#:#', l.strip()) for l in stack.split('\n') if l.strip()] + stack_sig = " | ".join(frames[:2]) + + raw_sig = f"{(err_type or '').strip().lower()}::{norm_msg[:120].lower()}::{stack_sig.lower()}" + fp = hashlib.sha256(raw_sig.encode('utf-8')).hexdigest()[:16] + return fp, title + +ALLOWED_TELEMETRY_PLATFORMS = {"Android", "iOS", "Web", "Windows", "macOS", "Linux", "Other"} + +@router.post('/report-error') +@limiter.limit("20/minute") +async def report_client_error(payload: ClientErrorReportReq, request: Request): + """ + Public telemetry endpoint for worldwide client runtime errors. + Automatically categorizes and groups identical errors by fingerprint. + Stores up to 50 recent occurrences per error category with bounded schema. + Eliminates forged identities: verifies Firebase token when present; treats unauthenticated as anonymous. + Automatically reopens resolved signatures upon recurrence. + """ + try: + from app.database import client_errors_collection + now_dt = datetime.datetime.utcnow() + now = now_dt.isoformat() + + # 1. Input sanitization & platform whitelisting + raw_platform = (payload.platform or "Other").strip().title() + clean_platform = raw_platform if raw_platform in ALLOWED_TELEMETRY_PLATFORMS else "Other" + + raw_ver = (payload.app_version or "1.0.0").strip()[:30] + clean_ver = re.sub(r'[^a-zA-Z0-9_\-\.]', '', raw_ver) or "1.0.0" + + raw_message = (payload.message or "")[:2000] + raw_stack = (payload.stack_trace or "")[:5000] + safe_message = _redact_sensitive_text(raw_message) + safe_stack = _redact_sensitive_text(raw_stack) + safe_route = (payload.route or "/unknown")[:250] + safe_context = _redact_sensitive_text((payload.context or "")[:500]) + safe_device = (payload.device_model or "unknown")[:100] + safe_os = (payload.os_version or "unknown")[:100] + + fingerprint, title = _compute_fingerprint(safe_message, payload.error_type or "UnknownError", safe_stack) + category = _categorize_error(safe_message, payload.error_type or "", safe_stack) + + # ELOG-03: Eliminate Identity Forging. Never trust payload user_id/email/role/is_pro! + uid = None + email = None + role = "ANONYMOUS" + is_pro = False + + auth_header = request.headers.get("Authorization", "") + if auth_header.lower().startswith("bearer "): + try: + token = auth_header.split(" ", 1)[1].strip() + decoded = verify_firebase_token(token) + if decoded and isinstance(decoded, dict): + uid = decoded.get("uid") + email = decoded.get("email") + role = decoded.get("role") or "USER" + is_pro = bool(decoded.get("is_pro", False)) + except Exception: + uid = None + email = None + role = "ANONYMOUS" + is_pro = False + + occurrence_id = f"err_{uuid.uuid4().hex[:12]}" + from app.core.rate_limits import get_client_ip + raw_ip = get_client_ip(request) or (request.client.host if request.client else "unknown") + client_ip = _mask_ip(raw_ip) + user_agent = (request.headers.get("user-agent") or "unknown")[:250] + + # Bound metadata to small primitive dict (max 10 keys) + safe_metadata = {} + if isinstance(payload.metadata, dict): + for k, v in list(payload.metadata.items())[:10]: + safe_metadata[str(k)[:50]] = _redact_sensitive_text(str(v)[:200]) + + occurrence = { + "occurrence_id": occurrence_id, + "event_id": (payload.event_id or occurrence_id)[:64], + "timestamp": now, + "user_id": (uid or "anonymous")[:128], + "user_email": (email or "anonymous")[:150], + "user_role": (role or "ANONYMOUS")[:50], + "is_pro": bool(is_pro), + "platform": clean_platform, + "device_model": safe_device, + "os_version": safe_os, + "app_version": clean_ver, + "route": safe_route, + "context": safe_context, + "message": safe_message, + "stack_trace": safe_stack, + "ip": client_ip, + "user_agent": user_agent, + "metadata": safe_metadata, + "is_test": False, + } + + # 2. Check for duplicate or resolved recurrence + existing_status = None + is_duplicate = False + try: + if hasattr(client_errors_collection, "find_one"): + existing_doc = await client_errors_collection.find_one( + {"fingerprint": fingerprint}, + {"status": 1, "recent_occurrences.event_id": 1, "reopened_count": 1} + ) + if existing_doc: + existing_status = existing_doc.get("status") + if payload.event_id: + recent = existing_doc.get("recent_occurrences") or [] + if any(isinstance(occ, dict) and occ.get("event_id") == payload.event_id for occ in recent): + is_duplicate = True + except Exception as ex: + logger.debug(f"Telemetry existing doc probe skipped: {ex}") + + if is_duplicate: + return { + "status": "ok", + "fingerprint": fingerprint, + "category": category, + "occurrence_id": payload.event_id or occurrence_id, + "duplicate": True, + "message": "Occurrence already recorded" + } + + set_fields = { + "last_seen": now, + "last_seen_date": now_dt, + "latest_route": safe_route, + "latest_app_version": clean_ver, + "latest_stack_trace": safe_stack, + "updated_at": now, + } + inc_fields = { + "total_occurrences": 1, + f"platforms.{clean_platform}": 1, + } + + # ELOG-07: Automatically reopen resolved errors upon recurrence + if existing_status == "RESOLVED": + set_fields["status"] = "UNRESOLVED" + set_fields["reopened_at"] = now + set_fields["reopened_reason"] = "New client occurrence observed after resolution" + inc_fields["reopened_count"] = 1 + + update_query = { + "$setOnInsert": { + "fingerprint": fingerprint, + "title": title[:200], + "error_type": (payload.error_type or "UnknownError")[:100], + "category": category, + "first_seen": now, + "first_seen_date": now_dt, + "status": "UNRESOLVED", + "created_at": now, + "is_test": False, + }, + "$set": set_fields, + "$inc": inc_fields, + "$push": { + "recent_occurrences": { + "$each": [occurrence], + "$slice": -50 + } + } + } + + # ELOG-04: Only populate affected_user_ids if authenticated via verified Firebase token + if uid: + add_to_set = {"affected_user_ids": uid} + if email: + add_to_set["affected_user_emails"] = email + update_query["$addToSet"] = add_to_set + + # 3. Bounded document update (prevents 16MB BSON DoS) + await client_errors_collection.update_one( + {"fingerprint": fingerprint}, + update_query, + upsert=True + ) + + return { + "status": "ok", + "fingerprint": fingerprint, + "category": category, + "occurrence_id": occurrence_id, + "reopened": existing_status == "RESOLVED" + } + except Exception as e: + logger.error(f"Failed to record client error telemetry: {e}") + raise HTTPException(status_code=500, detail="Failed to ingest error telemetry") + + +# ========================================================= +# ACCOUNT DELETION LIFECYCLE (7-DAY GRACE PERIOD & REAUTH) +# ========================================================= + +async def verify_user_credentials(user: dict, password: Optional[str], reauth_token: Optional[str]) -> bool: + """ + Verifies that the caller has re-authenticated before initiating account deletion: + 1. If user provided a fresh reauth_token (Firebase ID Token), verify that token's auth_time is within 10 minutes. + 2. If user provided a password, verify against Firebase Identity Toolkit signInWithPassword API. + 3. In non-prod / test / synthetic environment, allows synthetic passwords gracefully. + """ + uid = user.get("uid", "") + if getattr(settings, "ALLOW_MOCK_AUTH", False) and (any(uid.lower().startswith(p) for p in ("mock", "test", "locust", "dev", "sim")) or password in ("test_password_pass", "password123")): + return True + + # 1. Fresh Firebase ID Token verification + if reauth_token and reauth_token.strip(): + try: + from app.infrastructure.firebase import verify_firebase_token + decoded = verify_firebase_token(reauth_token.strip()) + if decoded.get("uid") == uid: + auth_time = decoded.get("auth_time", 0) + now_ts = time.time() + # Must be authenticated within last 10 minutes + if (now_ts - auth_time) <= 600: + return True + else: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Re-authentication token has expired. Please sign in again." + ) + else: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Re-authentication token does not match the active account." + ) + except HTTPException: + raise + except Exception as e: + logger.warning(f"Reauth token verification failed: {e}") + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Invalid re-authentication token." + ) + + # 2. Email & Password verification via Google Identity Toolkit + if password and password.strip(): + email = user.get("email") or user.get("primary_email") or (user.get("profile", {}) or {}).get("email") + if not email: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="User email not found for password verification." + ) + web_api_key = (settings.firebase.web_api_key or os.getenv("FIREBASE_WEB_API_KEY") or "").strip() + if not web_api_key: + raise HTTPException( + status_code=status.HTTP_503_SERVICE_UNAVAILABLE, + detail="Password verification is temporarily unavailable: FIREBASE_WEB_API_KEY is not configured." + ) + url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={web_api_key}" + try: + async with httpx.AsyncClient(timeout=10.0) as http_client: + res = await http_client.post(url, json={"email": email, "password": password, "returnSecureToken": True}) + if res.status_code == 200: + data = res.json() + if data.get("localId") == uid or data.get("email", "").lower() == email.lower(): + return True + else: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Password verified for a different account ID." + ) + else: + err_json = res.json().get("error", {}) + err_msg = err_json.get("message", "INVALID_PASSWORD") + if "INVALID_PASSWORD" in err_msg or "INVALID_LOGIN_CREDENTIALS" in err_msg: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Incorrect password. Account deletion aborted." + ) + elif "EMAIL_NOT_FOUND" in err_msg: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="No password configured for this Google account. Please authenticate via Google." + ) + else: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=f"Authentication verification failed: {err_msg}" + ) + except HTTPException: + raise + except Exception as e: + logger.error(f"Error during Google Identity Toolkit verification: {e}") + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="Error verifying password with authentication service." + ) + + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Password or re-authentication credential is required to delete your account." + ) + + +@router.post("/account/delete-request") +async def request_account_deletion( + payload: AccountDeleteRequest, + current_user: dict = Depends(get_current_user), +): + """ + Initiates account deletion with a mandatory 7-day grace period. + Requires server-side verification of account password or Google OAuth re-authentication. + Immediately disables active sessions and marks status to 'deletion_pending'. + """ + uid = current_user.get("uid") + if not uid: + raise HTTPException(status_code=400, detail="Invalid user identifier") + + if current_user.get("account_status") == "deletion_pending": + sched_at = current_user.get("deletion_scheduled_at") + return { + "status": "pending", + "message": f"Account deletion is already scheduled for {sched_at}.", + "account_status": "deletion_pending", + "deletion_scheduled_at": sched_at, + } + + # Verify credentials + await verify_user_credentials(current_user, payload.password, payload.reauth_token) + + now = datetime.datetime.utcnow() + scheduled_at = now + datetime.timedelta(days=7) + now_iso = now.isoformat() + scheduled_iso = scheduled_at.isoformat() + + # Update database with monotonic deletion_op_version + await users_collection.update_one( + {"uid": uid}, + { + "$set": { + "account_status": "deletion_pending", + "deletion_requested_at": now_iso, + "deletion_scheduled_at": scheduled_iso, + "deletion_reason": payload.reason, + "deletion_feedback": payload.feedback, + "updated_at": now_iso, + }, + "$inc": {"deletion_op_version": 1}, + } + ) + + # Invalidate cached user profile immediately + await invalidate_user_cache(uid) + + # Revoke Firebase tokens + try: + firebase_auth.revoke_refresh_tokens(uid) + except Exception as e: + logger.warning(f"Could not revoke Firebase refresh tokens for {uid}: {e}") + + # Audit logging + try: + from app.database import audit_logs_collection + await audit_logs_collection.insert_one({ + "action": "ACCOUNT_DELETION_SCHEDULED", + "user_id": uid, + "email": current_user.get("email"), + "timestamp": now_iso, + "created_at": now, # Native BSON Date for 90-day MongoDB TTL Index Auto-Purge + "scheduled_deletion_date": scheduled_iso, + "reason": payload.reason, + "feedback": payload.feedback, + }) + except Exception as e: + logger.warning(f"Failed to record deletion audit log: {e}") + + return { + "status": "success", + "message": "Account deletion scheduled. Your account and all personal data will be permanently deleted after 7 days.", + "account_status": "deletion_pending", + "deletion_requested_at": now_iso, + "deletion_scheduled_at": scheduled_iso, + "grace_period_days": 7, + } + + +@router.get("/account/deletion-status") +async def get_account_deletion_status( + current_user: dict = Depends(get_current_user), +): + """ + Returns the account deletion status and remaining grace period. + Exempt from 403 suspension so the user can inspect their status. + """ + uid = current_user.get("uid") + db_user = await users_collection.find_one({"uid": uid}) or current_user + status_str = db_user.get("account_status", "active") + is_pending = status_str == "deletion_pending" + sched_at = db_user.get("deletion_scheduled_at") + req_at = db_user.get("deletion_requested_at") + + days_rem = None + hours_rem = None + if is_pending and sched_at: + try: + sched_dt = datetime.datetime.fromisoformat(str(sched_at).replace("Z", "+00:00")).replace(tzinfo=None) + diff = sched_dt - datetime.datetime.utcnow() + total_sec = max(0, int(diff.total_seconds())) + days_rem = total_sec // 86400 + hours_rem = (total_sec % 86400) // 3600 + except Exception: + pass + + return { + "uid": uid, + "account_status": status_str, + "is_deletion_pending": is_pending, + "deletion_requested_at": req_at, + "deletion_scheduled_at": sched_at, + "days_remaining": days_rem, + "hours_remaining": hours_rem, + } + + +@router.post("/account/cancel-deletion") +async def cancel_account_deletion( + payload: Optional[AccountCancelDeleteRequest] = None, + current_user: dict = Depends(get_current_user), +): + """ + Atomically cancels a pending account deletion during the 7-day grace period. + Mutually exclusive with the background deletion worker claim on + (account_status == 'deletion_pending', deletion_scheduled_at > now) and deletion_op_version. + """ + uid = current_user.get("uid") + if not uid: + raise HTTPException(status_code=400, detail="Invalid user identifier") + + now_iso = datetime.datetime.utcnow().isoformat() + claim_filter: Dict[str, Any] = { + "uid": uid, + "account_status": "deletion_pending", + "deletion_scheduled_at": {"$gt": now_iso}, + } + if current_user.get("deletion_op_version") is not None: + claim_filter["deletion_op_version"] = current_user.get("deletion_op_version") + + upd_res = await users_collection.update_one( + claim_filter, + { + "$set": { + "account_status": "active", + "deletion_cancelled_at": now_iso, + "updated_at": now_iso, + }, + "$inc": {"deletion_op_version": 1}, + "$unset": { + "deletion_requested_at": "", + "deletion_scheduled_at": "", + "deletion_reason": "", + "deletion_feedback": "", + "processing_started_at": "", + }, + }, + ) + + if getattr(upd_res, "modified_count", 0) == 0: + db_user = await users_collection.find_one({"uid": uid}) + if not db_user: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Account deletion has already completed and cannot be cancelled.", + ) + curr_status = db_user.get("account_status", "active") + sched_at = str(db_user.get("deletion_scheduled_at") or "") + if curr_status in ("deletion_processing", "deletion_failed_retryable"): + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Account deletion is already in progress and cannot be cancelled.", + ) + if curr_status == "deletion_pending" and sched_at and sched_at <= now_iso: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Account deletion grace period has expired and cannot be cancelled.", + ) + return { + "status": "success", + "message": "Account is not scheduled for deletion.", + "account_status": curr_status, + } + + await invalidate_user_cache(uid) + + try: + from app.database import audit_logs_collection + await audit_logs_collection.insert_one({ + "action": "ACCOUNT_DELETION_CANCELLED", + "user_id": uid, + "email": current_user.get("email"), + "timestamp": now_iso, + "created_at": datetime.datetime.utcnow(), + "cancel_reason": payload.reason if payload else None, + }) + except Exception as e: + logger.warning(f"Failed to record cancel deletion audit log: {e}") + + return { + "status": "success", + "message": "Account deletion successfully cancelled. Your account has been restored to active status.", + "account_status": "active", + } + +@router.post('/rag-disclaimer/ack') +@router.post('/api/rag-disclaimer/ack') +async def acknowledge_rag_disclaimer(user: dict = Depends(get_current_user)): + """Permanently records that the user has completed the 60-second theological RAG disclaimer.""" + uid = user.get("uid") or user.get("user_id") + if not uid: + raise HTTPException(status_code=401, detail="Unauthorized") + now_iso = datetime.datetime.utcnow().isoformat() + await users_collection.update_one( + {"uid": uid}, + { + "$set": { + "has_acknowledged_rag_theology_disclaimer": True, + "rag_theology_acknowledged_at": now_iso, + "updated_at": now_iso, + } + } + ) + await invalidate_user_cache(uid) + return {"status": "success", "has_acknowledged_rag": True} + + +@router.get('/rag-disclaimer/status') +@router.get('/api/rag-disclaimer/status') +async def get_rag_disclaimer_status(user: dict = Depends(get_current_user)): + """Retrieves lifetime acknowledgment status of the 60-second theological RAG disclaimer.""" + uid = user.get("uid") or user.get("user_id") + if not uid: + raise HTTPException(status_code=401, detail="Unauthorized") + db_user = await users_collection.find_one( + {"uid": uid}, + {"has_acknowledged_rag_theology_disclaimer": 1, "_id": 0} + ) + is_ack = bool(db_user and db_user.get("has_acknowledged_rag_theology_disclaimer")) + return {"status": "success", "has_acknowledged_rag": is_ack} + + +class GuestMediaTokenRequest(BaseModel): + device_id: Optional[str] = None + + +@router.get('/guest-token') +@router.post('/guest-token') +@router.get('/media-token') +@router.post('/media-token') +@limiter.limit("60/minute") +async def issue_guest_or_user_media_token( + request: Request, + payload: Optional[GuestMediaTokenRequest] = None, + x_device_id: Optional[str] = Header(default=None, alias="X-Device-Id"), +): + """ + Issues a short-lived HS256 capability JWT (`scope: "media:read"`) for + Cloudflare Worker edge authentication on R2 content & audio CDN routes. + Accepts optional Firebase Bearer auth (`tier="user"`) or anonymous free + guest app launches (`tier="guest"`). + """ + from app.core.media_token import issue_media_jwt + + user = None + try: + user = await get_optional_user( + request=request, + authorization=request.headers.get("authorization"), + x_firebase_token=request.headers.get("x-firebase-token"), + x_user_id=request.headers.get("x-user-id"), + ) + except Exception: + user = None + + if user and (user.get("uid") or user.get("user_id")): + subject = str(user.get("uid") or user.get("user_id")).strip() + tier = "user" + else: + raw_device = (payload.device_id if payload else None) or x_device_id or "" + clean_device = re.sub(r"[^a-zA-Z0-9_\-]", "", raw_device)[:64] + if not clean_device: + clean_device = uuid.uuid4().hex[:16] + subject = f"guest:{clean_device}" + tier = "guest" + + try: + token_data = issue_media_jwt(subject=subject, tier=tier) + except RuntimeError as e: + raise HTTPException(status_code=503, detail=str(e)) + + return JSONResponse( + content={"status": "success", **token_data}, + headers={"Cache-Control": "no-store"}, + ) + +