from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker

from packages.adapters.sqlalchemy_impl.models import Base
from packages.adapters.sqlalchemy_impl.project_repository import (
    SQLAlchemyProjectRepository,
)
from packages.application import CreateProjectCommand, CreateProjectUseCase


def test_sqlalchemy_project_repository():
    """Test SQLAlchemy project repository with in-memory SQLite."""
    # Create in-memory SQLite database
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    SessionLocal = sessionmaker(bind=engine)
    session: Session = SessionLocal()

    try:
        # Create repository and use case
        repository = SQLAlchemyProjectRepository(session)
        use_case = CreateProjectUseCase(repository)

        # Create project
        project = use_case.execute(
            CreateProjectCommand(
                name="Test Project",
                description="Test description",
            ),
            owner_user_id="user-1",
        )

        assert project.name == "Test Project"

        # List projects by owner
        projects = repository.find_by_owner_user_id("user-1")
        assert len(projects) == 1
        assert projects[0].id == project.id
        assert projects[0].name == "Test Project"
    finally:
        session.close()
