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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""
Authentication API routes — register, login, refresh, me.
"""

from __future__ import annotations

import asyncio
import logging
import os
import secrets
from datetime import datetime, timezone
from typing import Annotated, Any

import requests
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel, Field
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.database import get_db
from app.dependencies import get_current_user
from app.models.user import User
from app.utils.security import (
    create_access_token,
    create_refresh_token,
    decode_token,
    hash_password,
    verify_password,
)

log = logging.getLogger("anemialens.auth")

router = APIRouter(prefix="/api/auth", tags=["auth"])

# SECURITY: Google OAuth config must be set via environment variables
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_TOKENINFO_URL = os.getenv("GOOGLE_TOKENINFO_URL", "https://oauth2.googleapis.com/tokeninfo")


# ---------------------------------------------------------------------------
# Schemas
# ---------------------------------------------------------------------------

class RegisterRequest(BaseModel):
    email: str = Field(min_length=3, max_length=255)
    password: str = Field(min_length=8, max_length=128)
    full_name: str | None = Field(default=None, max_length=255)


class LoginRequest(BaseModel):
    email: str = Field(min_length=3, max_length=255)
    password: str = Field(min_length=1, max_length=128)


class GoogleLoginRequest(BaseModel):
    credential: str = Field(min_length=20, max_length=4096)


class TokenResponse(BaseModel):
    access_token: str
    refresh_token: str
    token_type: str = "bearer"
    expires_in: int


class RefreshRequest(BaseModel):
    refresh_token: str


class UserProfile(BaseModel):
    uid: str
    email: str
    full_name: str | None
    role: str
    subscription_tier: str
    scan_count: int
    created_at: str
    last_login_at: str | None


class GoogleIdentity(BaseModel):
    email: str
    email_verified: bool
    full_name: str | None = None


def _parse_google_bool(value: Any) -> bool:
    if isinstance(value, bool):
        return value
    if isinstance(value, str):
        return value.strip().lower() == "true"
    if isinstance(value, (int, float)):
        return bool(value)
    return False


def _token_response_for_user(user: User) -> TokenResponse:
    access_token = create_access_token({"sub": user.uid, "role": user.role})
    refresh_token = create_refresh_token({"sub": user.uid})
    return TokenResponse(
        access_token=access_token,
        refresh_token=refresh_token,
        expires_in=3600,
    )


def _google_http_error(
    detail: str,
    status_code: int = status.HTTP_401_UNAUTHORIZED,
) -> HTTPException:
    return HTTPException(status_code=status_code, detail=detail)


def _verify_google_id_token(credential: str) -> GoogleIdentity:
    google_client_id = (
        os.getenv("ANEMIALENS_GOOGLE_CLIENT_ID")
        or os.getenv("GOOGLE_CLIENT_ID")
        or os.getenv("ANEMIALENS_GMAIL_CLIENT_ID")
        or os.getenv("VITE_GOOGLE_CLIENT_ID")
        or GOOGLE_CLIENT_ID
    )
    google_tokeninfo_url = (
        os.getenv("ANEMIALENS_GOOGLE_TOKENINFO_URL")
        or os.getenv("GOOGLE_TOKENINFO_URL")
        or GOOGLE_TOKENINFO_URL
    )

    if not google_client_id:
        raise _google_http_error(
            "Google sign-in is not configured for this deployment.",
            status.HTTP_503_SERVICE_UNAVAILABLE,
        )

    response = None
    last_exc = None
    for attempt in range(3):
        try:
            response = requests.get(
                google_tokeninfo_url,
                params={"id_token": credential},
                timeout=20 if attempt > 0 else 12,
            )
            break
        except requests.RequestException as exc:
            log.warning("Google token info fetch attempt %d failed: %s", attempt + 1, exc)
            last_exc = exc
            if attempt < 2:
                import time
                time.sleep(0.5 * (attempt + 1))

    if response is None:
        log.exception("Google token verification request failed completely after 3 attempts")
        raise _google_http_error(
            "Google sign-in is temporarily unavailable. Please try again.",
            status.HTTP_502_BAD_GATEWAY,
        ) from last_exc

    try:
        raw_payload: dict[str, Any] = response.json()
    except ValueError as exc:
        raise _google_http_error(
            "Google sign-in returned an invalid response.",
            status.HTTP_502_BAD_GATEWAY,
        ) from exc

    if response.status_code >= 400:
        provider_message = raw_payload.get("error_description") or raw_payload.get("error")
        message = (
            str(provider_message).strip()
            if isinstance(provider_message, str) and provider_message.strip()
            else "Google sign-in failed. Please try again."
        )
        raise _google_http_error(message)

    audience = raw_payload.get("aud")
    if audience != google_client_id:
        raise _google_http_error("Google credential is not valid for this app.")

    email = raw_payload.get("email")
    if not isinstance(email, str) or not email.strip():
        raise _google_http_error("Google sign-in did not return an email address.")

    if not _parse_google_bool(raw_payload.get("email_verified")):
        raise _google_http_error("Google account email is not verified.")

    full_name = raw_payload.get("name")
    if not isinstance(full_name, str):
        full_name = None

    return GoogleIdentity(
        email=email.strip().lower(),
        email_verified=True,
        full_name=full_name.strip() if full_name else None,
    )


# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------

@router.post(
    "/register",
    response_model=TokenResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Create a new user account",
)
async def register(
    body: RegisterRequest,
    db: Annotated[AsyncSession, Depends(get_db)],
) -> TokenResponse:
    log.info("Received registration request for email: %s", body.email)
    try:
        # Check if email already exists
        result = await db.execute(select(User).where(User.email == body.email.lower().strip()))
        if result.scalar_one_or_none() is not None:
            log.warning("Registration failed: Email already exists - %s", body.email)
            raise HTTPException(
                status_code=status.HTTP_409_CONFLICT,
                detail="An account with this email already exists.",
            )

        user = User(
            email=body.email.lower().strip(),
            hashed_password=hash_password(body.password),
            full_name=body.full_name,
            role="user",
        )
        db.add(user)
        await db.flush()
        await db.refresh(user)

        log.info("User registered successfully: %s (uid=%s)", user.email, user.uid)

        return _token_response_for_user(user)
    except HTTPException:
        raise
    except Exception as exc:
        log.exception("Registration failed for %s", body.email)
        # SECURITY: Don't leak internal error details to clients
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Registration failed. Please try again or contact support if the problem persists.",
        )


@router.post(
    "/login",
    response_model=TokenResponse,
    summary="Authenticate and get tokens",
)
async def login(
    body: LoginRequest,
    db: Annotated[AsyncSession, Depends(get_db)],
) -> TokenResponse:
    result = await db.execute(
        select(User).where(User.email == body.email.lower().strip())
    )
    user = result.scalar_one_or_none()

    if user is None or not verify_password(body.password, user.hashed_password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid email or password.",
        )

    if not user.is_active:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Account is deactivated.",
        )

    user.last_login_at = datetime.now(timezone.utc)
    await db.flush()

    log.info("User login: %s (uid=%s)", user.email, user.uid)

    return _token_response_for_user(user)


@router.post(
    "/google",
    response_model=TokenResponse,
    summary="Authenticate with Google Sign-In",
)
async def login_with_google(
    body: GoogleLoginRequest,
    db: Annotated[AsyncSession, Depends(get_db)],
) -> TokenResponse:
    identity = await asyncio.to_thread(_verify_google_id_token, body.credential)
    normalized_email = str(identity.email).lower().strip()

    result = await db.execute(select(User).where(User.email == normalized_email))
    user = result.scalar_one_or_none()

    if user is None:
        user = User(
            email=normalized_email,
            hashed_password=hash_password(secrets.token_urlsafe(32)),
            full_name=identity.full_name,
            role="user",
        )
        db.add(user)
        await db.flush()
        await db.refresh(user)
        log.info("Created account from Google sign-in: %s (uid=%s)", user.email, user.uid)
    elif identity.full_name and not user.full_name:
        user.full_name = identity.full_name

    if not user.is_active:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Account is deactivated.",
        )

    user.last_login_at = datetime.now(timezone.utc)
    await db.flush()

    log.info("Google login: %s (uid=%s)", user.email, user.uid)
    return _token_response_for_user(user)


@router.post(
    "/refresh",
    response_model=TokenResponse,
    summary="Refresh an expired access token",
)
async def refresh(
    body: RefreshRequest,
    db: Annotated[AsyncSession, Depends(get_db)],
) -> TokenResponse:
    payload = decode_token(body.refresh_token)
    if payload is None or payload.get("type") != "refresh":
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid or expired refresh token.",
        )

    user_uid = payload.get("sub")
    result = await db.execute(select(User).where(User.uid == user_uid))
    user = result.scalar_one_or_none()
    if user is None or not user.is_active:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="User not found.",
        )

    return _token_response_for_user(user)


@router.get(
    "/me",
    response_model=UserProfile,
    summary="Get current user profile",
)
async def me(
    user: Annotated[User, Depends(get_current_user)],
) -> UserProfile:
    return UserProfile(
        uid=user.uid,
        email=user.email,
        full_name=user.full_name,
        role=user.role,
        subscription_tier=user.subscription_tier or "free",
        scan_count=user.scan_count,
        created_at=user.created_at.isoformat(),
        last_login_at=user.last_login_at.isoformat() if user.last_login_at else None,
    )


# ---------------------------------------------------------------------------
# Personal stats
# ---------------------------------------------------------------------------

from sqlalchemy import func as sa_func
from app.models.screening import Screening


class UserStatsResponse(BaseModel):
    total_scans: int
    scans_this_month: int
    avg_risk: float | None
    avg_hemoglobin: float | None
    high_concern_count: int
    low_risk_count: int
    last_scan_at: str | None


@router.get(
    "/me/stats",
    response_model=UserStatsResponse,
    summary="Get personal screening statistics",
)
async def me_stats(
    user: Annotated[User, Depends(get_current_user)],
    db: Annotated[AsyncSession, Depends(get_db)],
) -> UserStatsResponse:
    from datetime import datetime, timezone
    from sqlalchemy import select, func, and_

    # Total scans
    total = await db.scalar(
        select(func.count()).where(Screening.user_id == user.id)
    ) or 0

    # This month
    now = datetime.now(timezone.utc)
    month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
    this_month = await db.scalar(
        select(func.count()).where(
            and_(Screening.user_id == user.id, Screening.created_at >= month_start)
        )
    ) or 0

    # Averages
    avg_risk = await db.scalar(
        select(func.avg(Screening.anemia_risk)).where(
            and_(Screening.user_id == user.id, Screening.anemia_risk.isnot(None))
        )
    )
    avg_hb = await db.scalar(
        select(func.avg(Screening.predicted_hemoglobin)).where(
            and_(Screening.user_id == user.id, Screening.predicted_hemoglobin.isnot(None))
        )
    )

    # Band counts
    high = await db.scalar(
        select(func.count()).where(
            and_(Screening.user_id == user.id, Screening.triage_band == "high_concern")
        )
    ) or 0
    low = await db.scalar(
        select(func.count()).where(
            and_(Screening.user_id == user.id, Screening.triage_band == "low_risk")
        )
    ) or 0

    # Last scan
    last = await db.scalar(
        select(Screening.created_at).where(Screening.user_id == user.id)
        .order_by(Screening.created_at.desc()).limit(1)
    )

    return UserStatsResponse(
        total_scans=total,
        scans_this_month=this_month,
        avg_risk=round(float(avg_risk), 4) if avg_risk is not None else None,
        avg_hemoglobin=round(float(avg_hb), 2) if avg_hb is not None else None,
        high_concern_count=high,
        low_risk_count=low,
        last_scan_at=last.isoformat() if last else None,
    )