import sys
from pathlib import Path

import pytest
from fastapi import HTTPException

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

from app.api.routes.generation_tasks import _ensure_library_has_ready_video_assets

from packages.domain import Asset, AssetStatus


def _asset(name: str, mime_type: str, status: AssetStatus) -> Asset:
    return Asset.create(
        project_id="project-1",
        library_id="library-1",
        name=name,
        storage_key=f"uploads/{name}",
        mime_type=mime_type,
        file_size=1024,
        status=status,
    )


def test_generation_preflight_rejects_library_without_ready_video():
    with pytest.raises(HTTPException) as exc_info:
        _ensure_library_has_ready_video_assets(
            [
                _asset("image.jpg", "image/jpeg", AssetStatus.READY),
                _asset("video.mp4", "video/mp4", AssetStatus.UPLOADING),
            ]
        )

    assert exc_info.value.status_code == 422
    assert "ready 状态的视频素材" in exc_info.value.detail


def test_generation_preflight_accepts_ready_video():
    _ensure_library_has_ready_video_assets(
        [
            _asset("video.mp4", "video/mp4", AssetStatus.READY),
        ]
    )
