#!/usr/bin/env python3
"""
Script to clear all data from PostgreSQL database.
"""

from src.db.session import get_sync_session
from src.db.models import Document, ExtractedRecord


def clear_database():
    """Clear all documents and extracted records from database."""

    print("=" * 70)
    print("Clearing PostgreSQL Database")
    print("=" * 70)

    with get_sync_session() as db:
        # Count before deletion
        doc_count = db.query(Document).count()
        record_count = db.query(ExtractedRecord).count()

        print(f"\n📊 Current data:")
        print(f"   Documents: {doc_count}")
        print(f"   Extracted Records: {record_count}")

        # Delete all extracted records first (foreign key constraint)
        print(f"\n🗑️  Deleting extracted records...")
        db.query(ExtractedRecord).delete()
        db.commit()
        print(f"✅ Deleted {record_count} extracted records")

        # Delete all documents
        print(f"🗑️  Deleting documents...")
        db.query(Document).delete()
        db.commit()
        print(f"✅ Deleted {doc_count} documents")

    print("\n" + "=" * 70)
    print("Database Cleared Successfully!")
    print("=" * 70)


if __name__ == "__main__":
    clear_database()
