#!/usr/bin/env python3
"""
Script to check what documents are currently in the system
and identify any test/fake data that needs to be cleaned up.
"""

import os
import sys
import django

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from utils.models import Document
from users.models import CustomUser

def check_documents():
    print("=== DOCUMENT ANALYSIS ===")
    print()
    
    # Check Document model
    print("1. Documents from Document model:")
    documents = Document.objects.all().order_by('-created_at')
    print(f"   Total documents: {documents.count()}")
    
    if documents.exists():
        print("   Recent documents:")
        for doc in documents[:10]:
            print(f"   - {doc.name} ({doc.document_type}) by {doc.uploaded_by} - {doc.created_at}")
    
    print()
    
    # Check for test/fake data patterns
    print("2. Checking for test/fake data:")
    test_patterns = ['test', 'fake', 'sample', 'demo', 'county']
    
    for pattern in test_patterns:
        test_docs = documents.filter(name__icontains=pattern)
        if test_docs.exists():
            print(f"   Found {test_docs.count()} documents with '{pattern}' in name:")
            for doc in test_docs:
                print(f"   - ID: {doc.id}, Name: {doc.name}, Type: {doc.document_type}")
    
    print()
    
    # Check KYC documents from CustomUser model
    print("3. KYC Documents from CustomUser model:")
    clients = CustomUser.objects.filter(role='borrower')
    print(f"   Total borrowers: {clients.count()}")
    
    kyc_counts = {
        'id_document': clients.exclude(id_document='').count(),
        'selfie': clients.exclude(selfie='').count(),
        'utility_bill': clients.exclude(utility_bill='').count(),
        'bank_statement': clients.exclude(bank_statement='').count(),
        'business_license': clients.exclude(business_license='').count(),
    }
    
    print("   KYC document counts:")
    for doc_type, count in kyc_counts.items():
        print(f"   - {doc_type}: {count}")
    
    print()
    
    # Check for clients with documents
    print("4. Recent clients with KYC documents:")
    clients_with_docs = clients.filter(
        models.Q(id_document__isnull=False) |
        models.Q(selfie__isnull=False) |
        models.Q(utility_bill__isnull=False) |
        models.Q(bank_statement__isnull=False) |
        models.Q(business_license__isnull=False)
    ).order_by('-created_at')[:5]
    
    for client in clients_with_docs:
        docs = []
        if client.id_document: docs.append('ID')
        if client.selfie: docs.append('Selfie')
        if client.utility_bill: docs.append('Utility')
        if client.bank_statement: docs.append('Bank')
        if client.business_license: docs.append('Business')
        
        print(f"   - {client.get_full_name()}: {', '.join(docs)}")

def clean_test_documents():
    """Remove test/fake documents"""
    print("\n=== CLEANING TEST DOCUMENTS ===")
    
    test_patterns = ['test', 'fake', 'sample', 'demo', 'county']
    
    for pattern in test_patterns:
        test_docs = Document.objects.filter(name__icontains=pattern)
        if test_docs.exists():
            print(f"Deleting {test_docs.count()} documents with '{pattern}' in name...")
            deleted_count = test_docs.delete()[0]
            print(f"Deleted {deleted_count} documents")
    
    print("Test document cleanup complete!")

if __name__ == '__main__':
    from django.db import models
    
    check_documents()
    
    # Ask if user wants to clean test documents
    if len(sys.argv) > 1 and sys.argv[1] == '--clean':
        clean_test_documents()
    else:
        print("\nTo clean test documents, run: python check_documents_data.py --clean")