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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22947a0
 
 
 
 
 
 
 
 
 
 
 
2985cd5
 
 
22947a0
2985cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import JSONResponse
from fastapi.security import HTTPBearer
from typing import Optional
from datetime import timedelta, datetime
from sqlmodel import select
from pydantic import BaseModel

from ...models.user import User, UserCreate, UserRead
from ...services.auth_service import AuthService, AuthException
from ...database.database import get_async_session
from sqlmodel.ext.asyncio.session import AsyncSession
from ...utils.jwt_utils import create_access_token, decode_access_token, get_password_hash, verify_password
from ...config import settings
from ...utils.date_validator import validate_signup_date
from ..deps import get_current_user

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

# Initialize the auth service
auth_service = AuthService()

class SignupRequest(BaseModel):
    email: str
    password: str
    name: Optional[str] = None
    date_of_birth: Optional[str] = None
    signup_date: Optional[str] = None  # Allow client to send signup date (will be validated and potentially overridden)

class SigninRequest(BaseModel):
    email: str
    password: str

class ErrorResponse(BaseModel):
    error: str
    message: str

@router.post("/signup", response_model=dict, responses={
    400: {"model": ErrorResponse, "description": "Invalid request format or validation error"},
    409: {"model": ErrorResponse, "description": "Email already exists"}
})
async def signup_user(
    signup_request: SignupRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Create new user account with signup date validation.
    """
    try:
        # Validate and set signup date
        validated_signup_date = validate_signup_date(signup_request.signup_date)

        # Hash the password before storing
        hashed_password = get_password_hash(signup_request.password)

        # Create new user with signup date validation
        db_user = User(
            email=signup_request.email,
            password=hashed_password,
            name=signup_request.name,
            date_of_birth=signup_request.date_of_birth,
            signup_date=validated_signup_date  # Non-nullable field, enforced here
        )
        session.add(db_user)
        await session.commit()
        await session.refresh(db_user)

        # Create access token
        access_token_expires = timedelta(minutes=settings.access_token_expire_minutes)
        access_token = create_access_token(
            data={"sub": str(db_user.id)}, expires_delta=access_token_expires
        )

        response = JSONResponse(content={
            "success": True,
            "data": {
                "user": UserRead.model_validate(db_user),
                "access_token": access_token,
                "token_type": "bearer"
            }
        })

        # Set JWT as an HTTP-only cookie
        response.set_cookie(
            key="access_token",
            value=access_token,
            expires=access_token_expires.total_seconds(),
            httponly=True,
            samesite="lax",
            # secure=True # Use secure=True in production with HTTPS
        )
        return response
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Signup failed: {str(e)}"
        )

@router.post("/signin", response_model=dict, responses={
    400: {"model": ErrorResponse, "description": "Invalid request format"},
    401: {"model": ErrorResponse, "description": "Authentication failed"}
})
async def signin_user(
    signin_request: SigninRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Authenticate user with enhanced error handling to differentiate between email and password errors.
    """
    try:
        # Find user by email
        statement = select(User).where(User.email == signin_request.email)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

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

        # Verify the provided password against the stored hash
        if not verify_password(signin_request.password, user.password):
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Invalid email or password"
            )

        # Create access token
        access_token_expires = timedelta(minutes=settings.access_token_expire_minutes)
        access_token = create_access_token(
            data={"sub": str(user.id)}, expires_delta=access_token_expires
        )

        response = JSONResponse(content={
            "success": True,
            "data": {
                "user": UserRead.model_validate(user),
                "access_token": access_token,
                "token_type": "bearer"
            }
        })

        # Set JWT as an HTTP-only cookie
        response.set_cookie(
            key="access_token",
            value=access_token,
            expires=access_token_expires.total_seconds(),
            httponly=True,  # HttpOnly flag prevents client-side access
            samesite="lax", # Lax for CSRF protection
            # secure=True # Use secure=True in production with HTTPS
        )
        return response
    except AuthException as e:
        # Map our custom exceptions to appropriate HTTP responses
        status_code = status.HTTP_401_UNAUTHORIZED
        if e.error_type == "EMAIL_NOT_FOUND":
            status_code = status.HTTP_401_UNAUTHORIZED
        elif e.error_type == "INVALID_PASSWORD":
            status_code = status.HTTP_401_UNAUTHORIZED

        raise HTTPException(
            status_code=status_code,
            detail=e.message
        )
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Signin failed: {str(e)}"
        )


class LoginRequest(BaseModel):
    email: str
    password: str

class ForgotPasswordRequest(BaseModel):
    email: str
    date_of_birth: str  # Format: YYYY-MM-DD

@router.post("/forgot-password", response_model=dict)
async def forgot_password(
    request_data: ForgotPasswordRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Verify user identity using email and date of birth for password reset.
    """
    try:
        # Find user by email
        statement = select(User).where(User.email == request_data.email)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

        if not user:
            # Return the same error message even if email doesn't exist to prevent email enumeration
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="If an account with that email exists, a password reset link has been sent."
            )

        # Verify the date of birth matches
        print(f"DEBUG: Looking up user by email: {request_data.email}")
        print(f"DEBUG: Found user date_of_birth: {user.date_of_birth} (type: {type(user.date_of_birth)})")

        if not user.date_of_birth:
            print(f"DEBUG: User has no date of birth set in database")
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Date of birth verification failed. Please contact support."
            )

        # Parse the provided date of birth
        from datetime import datetime
        try:
            provided_dob = datetime.strptime(request_data.date_of_birth, '%Y-%m-%d')
        except ValueError:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Invalid date format. Please use YYYY-MM-DD format."
            )

        # Compare dates (ignoring time component)
        print(f"DEBUG: Comparing user.date_of_birth: {user.date_of_birth} with provided: {provided_dob.date()}")

        if user.date_of_birth != provided_dob.date():  # Both should be date objects now
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Date of birth verification failed. Please check your date of birth and try again."
            )

        # In a real app, you'd send a password reset link to the user's email
        # For demo purposes, we'll just return success

        return {
            "success": True,
            "data": {
                "message": "Identity verified successfully. Please set your new password.",
                "email": request_data.email
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Forgot password request failed: {str(e)}"
        )


class SetNewPasswordRequest(BaseModel):
    email: str
    new_password: str

@router.post("/set-new-password", response_model=dict)
async def set_new_password(
    request_data: SetNewPasswordRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Set new password after successful verification.
    """
    try:
        # Find user by email
        statement = select(User).where(User.email == request_data.email)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

        if not user:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="User not found"
            )

        # Hash the new password before storing
        hashed_new_password = get_password_hash(request_data.new_password)
        user.password = hashed_new_password
        user.updated_at = datetime.utcnow()

        session.add(user)
        await session.commit()

        return {
            "success": True,
            "data": {
                "message": "Password changed successfully"
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Password change failed: {str(e)}"
        )


class ResetPasswordRequest(BaseModel):
    email: str
    new_password: str

@router.post("/reset-password", response_model=dict)
async def reset_password(
    request_data: ResetPasswordRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Reset user password directly (for admin or special cases).
    """
    try:
        # Find user by email
        statement = select(User).where(User.email == request_data.email)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

        if not user:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="User not found"
            )

        # Hash the new password before storing
        hashed_new_password = get_password_hash(request_data.new_password)
        user.password = hashed_new_password
        user.updated_at = datetime.utcnow()

        session.add(user)
        await session.commit()

        return {
            "success": True,
            "data": {
                "message": "Password reset successfully"
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Password reset failed: {str(e)}"
        )


class ChangePasswordRequest(BaseModel):
    current_password: str
    new_password: str

@router.post("/change-password", response_model=dict)
async def change_password(
    request_data: ChangePasswordRequest,
    current_user: UserRead = Depends(get_current_user),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Change user's current password after verifying the current password.
    """
    try:
        # Verify the current password
        statement = select(User).where(User.id == current_user.id)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

        if not user:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="User not found"
            )

        # Verify the provided current password against the stored hash
        if not verify_password(request_data.current_password, user.password):
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Current password is incorrect"
            )

        # Hash the new password before storing
        hashed_new_password = get_password_hash(request_data.new_password)
        user.password = hashed_new_password
        user.updated_at = datetime.utcnow()

        # Reset the force_password_change flag if set
        user.force_password_change = False

        session.add(user)
        await session.commit()

        return {
            "success": True,
            "data": {
                "message": "Password changed successfully"
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Password change failed: {str(e)}"
        )


@router.post("/token", response_model=dict)
async def login_for_access_token(
    login_request: LoginRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    OAuth2 compatible token login, get an access token for future requests.
    """
    try:
        # Find user by email
        statement = select(User).where(User.email == login_request.email)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

        if not user:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Incorrect email or password",
                headers={"WWW-Authenticate": "Bearer"},
            )

        # Verify the provided password against the stored hash
        if not verify_password(login_request.password, user.password):
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Incorrect email or password",
                headers={"WWW-Authenticate": "Bearer"},
            )

        # Create access token
        access_token_expires = timedelta(minutes=settings.access_token_expire_minutes)
        access_token = create_access_token(
            data={"sub": str(user.id)}, expires_delta=access_token_expires
        )

        return {
            "access_token": access_token,
            "token_type": "bearer",
            "user": UserRead.model_validate(user)
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Token creation failed: {str(e)}"
        )


# Also add a test endpoint that doesn't require auth to verify the token works
@router.get("/test-token", response_model=dict)
async def test_token():
    """
    Test endpoint to verify authentication is working.
    """
    return {
        "message": "Token is valid!",
        "success": True
    }


@router.post("/register", response_model=dict)
async def register_user(
    user_create: UserCreate,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Register a new user.
    """
    try:
        # Check if user already exists
        existing_user_statement = select(User).where(User.email == user_create.email)
        existing_user_result = await session.execute(existing_user_statement)
        existing_user = existing_user_result.scalar_one_or_none()

        if existing_user:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="User with this email already exists"
            )

        # Hash the password before storing
        hashed_password = get_password_hash(user_create.password)

        # Create new user with hashed password, optional name and date of birth
        from datetime import datetime, date
        date_of_birth_obj = None

        # Debug: Print incoming date of birth for troubleshooting
        print(f"DEBUG: Incoming date_of_birth value: {user_create.date_of_birth}, type: {type(user_create.date_of_birth)}")

        # Handle date of birth conversion - supports string, date, or datetime objects
        if user_create.date_of_birth is not None:
            dob_input = user_create.date_of_birth

            print(f"DEBUG: Processing date of birth input: {dob_input}, type: {type(dob_input)}")

            if isinstance(dob_input, str):
                # Handle string input - strip whitespace and parse
                dob_str = dob_input.strip()
                print(f"DEBUG: String date stripped: '{dob_str}'")
                if dob_str:  # Only process if not empty after stripping
                    try:
                        # Parse string to date object (to match database schema)
                        date_of_birth_obj = datetime.strptime(dob_str, '%Y-%m-%d').date()
                        print(f"DEBUG: Successfully parsed date: {date_of_birth_obj}")
                    except ValueError as e:
                        # Handle invalid date format
                        print(f"Date of birth parsing error: {e}")
                        raise HTTPException(
                            status_code=status.HTTP_400_BAD_REQUEST,
                            detail="Invalid date of birth format. Please use YYYY-MM-DD format."
                        )
            elif isinstance(dob_input, date):
                # If it's already a date object, use it as is
                date_of_birth_obj = dob_input
                print(f"DEBUG: Using date object directly: {date_of_birth_obj}")
            elif isinstance(dob_input, datetime):
                # If it's a datetime object, convert to date to match database schema
                date_of_birth_obj = dob_input.date()
                print(f"DEBUG: Converted datetime to date: {date_of_birth_obj}")
        else:
            print("DEBUG: No date of birth provided in request")

        print(f"DEBUG: Final date_of_birth_obj value: {date_of_birth_obj}")

        # Use current time as signup_date if not provided in the request
        current_time = datetime.utcnow()

        db_user = User(
            email=user_create.email,
            password=hashed_password,
            name=user_create.name,
            date_of_birth=date_of_birth_obj,
            signup_date=current_time,  # Non-nullable field, enforced here
            force_password_change=False  # New users don't need to change password immediately
        )
        session.add(db_user)
        await session.commit()
        await session.refresh(db_user)

        # Additional debug: Check what was actually saved
        print(f"DEBUG: User saved with date_of_birth: {db_user.date_of_birth}, signup_date: {db_user.signup_date}")

        # Create access token
        access_token_expires = timedelta(minutes=settings.access_token_expire_minutes)
        access_token = create_access_token(
            data={"sub": str(db_user.id)}, expires_delta=access_token_expires
        )

        # Create a UserRead instance manually to avoid serialization issues
        user_read = UserRead(
            id=db_user.id,
            email=db_user.email,
            name=db_user.name,
            date_of_birth=db_user.date_of_birth,
            signup_date=db_user.signup_date,
            force_password_change=db_user.force_password_change,
            created_at=db_user.created_at,
            updated_at=db_user.updated_at
        )

        return {
            "success": True,
            "data": {
                "user": user_read,
                "access_token": access_token,
                "token_type": "bearer"
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Registration failed: {str(e)}"
        )


@router.post("/login", response_model=dict)
async def login_user(
    login_request: LoginRequest,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Authenticate user and return access token.
    """
    try:
        # Find user by email
        statement = select(User).where(User.email == login_request.email)
        result = await session.execute(statement)
        user = result.scalar_one_or_none()

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

        # Verify the provided password against the stored hash
        if not verify_password(login_request.password, user.password):
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Invalid email or password"
            )

        # Create access token
        access_token_expires = timedelta(minutes=settings.access_token_expire_minutes)
        access_token = create_access_token(
            data={"sub": str(user.id)}, expires_delta=access_token_expires
        )

        return {
            "success": True,
            "data": {
                "user": UserRead.model_validate(user),
                "access_token": access_token,
                "token_type": "bearer"
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Login failed: {str(e)}"
        )