#!/usr/bin/env python3
"""
Check Person data for User 28.
"""
import asyncio
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import AsyncSessionLocal
from app.models.identity.identity import User, Person

async def check_person():
    async with AsyncSessionLocal() as db:
        # Get User 28
        user_stmt = select(User).where(User.id == 28)
        user_result = await db.execute(user_stmt)
        test_user = user_result.scalar_one_or_none()
        
        if not test_user:
            print("❌ User 28 not found")
            return
        
        print(f"✅ User 28:")
        print(f"   - User ID: {test_user.id}")
        print(f"   - Person ID: {test_user.person_id}")
        print(f"   - Email: {test_user.email}")
        
        # Get the Person record
        if test_user.person_id:
            person_stmt = select(Person).where(Person.id == test_user.person_id)
            person_result = await db.execute(person_stmt)
            person = person_result.scalar_one_or_none()
            
            if person:
                print(f"\n✅ Person {person.id}:")
                print(f"   - First name: {person.first_name}")
                print(f"   - Last name: {person.last_name}")
                print(f"   - Date of birth: {person.date_of_birth}")
                print(f"   - Gender: {person.gender}")
            else:
                print(f"\n❌ Person with ID {test_user.person_id} not found")
        else:
            print("\n❌ User has no person_id")
        
        # Check if there's a Person with ID 28
        person28_stmt = select(Person).where(Person.id == 28)
        person28_result = await db.execute(person28_stmt)
        person28 = person28_result.scalar_one_or_none()
        
        if person28:
            print(f"\n⚠️  Person with ID 28 exists:")
            print(f"   - First name: {person28.first_name}")
            print(f"   - Last name: {person28.last_name}")
            print(f"   - Date of birth: {person28.date_of_birth}")
            print(f"   - Gender: {person28.gender}")
            
            # Check which user is linked to this person
            user_for_person28_stmt = select(User).where(User.person_id == 28)
            user_for_person28_result = await db.execute(user_for_person28_stmt)
            user_for_person28 = user_for_person28_result.scalar_one_or_none()
            
            if user_for_person28:
                print(f"   - Linked to User: {user_for_person28.email} (ID: {user_for_person28.id})")
            else:
                print(f"   - Not linked to any user")

if __name__ == "__main__":
    asyncio.run(check_person())