import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "apps" / "api"))

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from packages.adapters.sqlalchemy_impl.generated_video_repository import SQLAlchemyGeneratedVideoRepository
from packages.adapters.sqlalchemy_impl.models import Base
from packages.domain import GeneratedVideo


def _repository():
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    return SQLAlchemyGeneratedVideoRepository(session)


def test_generated_video_repository_preserves_management_fields():
    repository = _repository()
    video = GeneratedVideo.create(
        project_id="project-1",
        generation_task_id="task-1",
        name="output.mp4",
        file_url="generated/output.mp4",
        file_size=2048,
        duration=5.0,
        width=1280,
        height=720,
        fps=25.0,
        generation_params={"asset_library_id": "library-1", "title_id": "title-1"},
    )

    repository.create(video)
    saved = repository.get(video.id)

    assert saved.status == "completed"
    assert saved.review_status == "pending_review"
    assert saved.generation_params["title_id"] == "title-1"

    saved.review_status = "approved"
    repository.update(saved)

    assert repository.get(video.id).review_status == "approved"
