#!/usr/bin/env python3
"""
Simple integration test for ManyToManyField.
This demonstrates that the implementation works correctly.
"""

from atomsql.core.db import Database
from atomsql.orm import Model, CharField, ManyToManyField


# Test models
class Author(Model):
    name = CharField(max_length=100)

    def __repr__(self):
        return f"<Author: {self.name}>"


class Book(Model):
    title = CharField(max_length=200)
    authors = ManyToManyField(Author, related_name="books")

    def __repr__(self):
        return f"<Book: {self.title}>"


def run_integration_test():
    """Run a comprehensive integration test."""
    print("🧪 Running ManyToManyField Integration Test")
    print("-" * 50)

    # Setup
    db = Database("sqlite:///:memory:")
    db.register(Author)
    db.register(Book)

    # Test 1: Create instances
    print("✅ Test 1: Creating instances...")
    author1 = Author.create(name="J.K. Rowling")
    author2 = Author.create(name="Stephen King")
    book1 = Book.create(title="Harry Potter")
    book2 = Book.create(title="The Shining")
    print(f"   Created: {author1}, {author2}")
    print(f"   Created: {book1}, {book2}")

    # Test 2: Add relationships
    print("\n✅ Test 2: Adding relationships...")
    book1.authors.add(author1)
    book2.authors.add(author2)
    print(f"   {book1.title} authors: {book1.authors.all()}")
    print(f"   {book2.title} authors: {book2.authors.all()}")

    # Test 3: Reverse relationships
    print("\n✅ Test 3: Checking reverse relationships...")
    print(f"   {author1.name} books: {author1.books.all()}")
    print(f"   {author2.name} books: {author2.books.all()}")

    # Test 4: Multiple authors per book
    print("\n✅ Test 4: Multiple authors per book...")
    book1.authors.add(author2)  # Collaboration book
    print(f"   {book1.title} authors: {book1.authors.all()}")
    print(f"   {author2.name} books: {author2.books.all()}")

    # Test 5: Count method
    print("\n✅ Test 5: Count method...")
    print(f"   {book1.title} has {book1.authors.count()} authors")
    print(f"   {author2.name} has {author2.books.count()} books")

    # Test 6: Remove method
    print("\n✅ Test 6: Remove method...")
    book1.authors.remove(author2)
    print(f"   After removing {author2.name} from {book1.title}:")
    print(f"   {book1.title} authors: {book1.authors.all()}")
    print(f"   {author2.name} books: {author2.books.all()}")

    # Test 7: Set method
    print("\n✅ Test 7: Set method...")
    book1.authors.set([author1, author2])
    print(f"   After setting {book1.title} authors to [author1, author2]:")
    print(f"   {book1.title} authors: {book1.authors.all()}")

    # Test 8: Clear method
    print("\n✅ Test 8: Clear method...")
    book1.authors.clear()
    print(f"   After clearing {book1.title} authors:")
    print(f"   {book1.title} authors: {book1.authors.all()}")
    print(f"   {author1.name} books: {author1.books.all()}")
    print(f"   {author2.name} books: {author2.books.all()}")

    # Test 9: Through table verification
    print("\n✅ Test 9: Through table verification...")
    tables = db.backend.get_tables()
    print(f"   Database tables: {tables}")
    assert "author_book" in tables, "Through table should exist"

    print("\n🎉 All tests passed! ManyToManyField implementation is working correctly.")


if __name__ == "__main__":
    run_integration_test()
