#!/usr/bin/env python3
"""
Check the specific user causing the 404 error
"""

import os
import sys
import django
from pathlib import Path

# Setup Django
sys.path.append('.')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from users.models import CustomUser

def main():
    print("🔍 Checking for user with the problematic file...")
    
    # Look for the specific file mentioned in the error
    problem_file = "kyc/selfies/WhatsApp_Image_2024-10-25_at_09_inyZyhk.41.21.jpeg"
    
    print(f"Looking for file: {problem_file}")
    
    # Check if file exists
    file_path = Path('media') / problem_file
    print(f"File exists: {file_path.exists()}")
    
    # Find users with this file
    users_with_file = CustomUser.objects.filter(selfie=problem_file)
    print(f"Users with this selfie: {users_with_file.count()}")
    
    for user in users_with_file:
        print(f"  - {user.username} ({user.first_name} {user.last_name})")
        print(f"    ID: {user.id}")
        print(f"    Selfie: {user.selfie}")
    
    # Also check for the user ID from the SQL dump
    user_id = "0d19fac990364dad9d10771270cfb439"
    try:
        user = CustomUser.objects.get(id=user_id)
        print(f"\nFound user by ID: {user.username}")
        print(f"  Name: {user.first_name} {user.last_name}")
        print(f"  Selfie: {user.selfie}")
        print(f"  ID Document: {user.id_document}")
        print(f"  Utility Bill: {user.utility_bill}")
        print(f"  Bank Statement: {user.bank_statement}")
    except CustomUser.DoesNotExist:
        print(f"\nUser with ID {user_id} not found in current database")
    
    # Check all users with phone number from SQL
    phone_users = CustomUser.objects.filter(phone_number="0719140590")
    print(f"\nUsers with phone 0719140590: {phone_users.count()}")
    for user in phone_users:
        print(f"  - {user.username} ({user.first_name} {user.last_name})")
        print(f"    Selfie: {user.selfie}")
    
    # List all files in selfies directory
    selfies_dir = Path('media/kyc/selfies')
    if selfies_dir.exists():
        print(f"\nFiles in {selfies_dir}:")
        for file in selfies_dir.iterdir():
            if file.is_file():
                print(f"  - {file.name}")
    else:
        print(f"\nDirectory {selfies_dir} does not exist")

if __name__ == '__main__':
    main()