import sys
from pathlib import Path

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

from worker_app.core.asset_usage import mark_asset_used_for_generation

from packages.domain import Asset, AssetStatus


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


def test_mark_asset_used_for_generation_sets_first_review_state():
    asset = _asset()

    mark_asset_used_for_generation(asset)

    assert asset.metadata["generation_use_count"] == 1
    assert asset.metadata["review_status"] == "pending_review"
    assert asset.metadata["last_used_at"]


def test_mark_asset_used_for_generation_increments_without_overwriting_review_state():
    asset = _asset()
    asset.metadata = {"generation_use_count": 2, "review_status": "approved"}

    mark_asset_used_for_generation(asset)

    assert asset.metadata["generation_use_count"] == 3
    assert asset.metadata["review_status"] == "approved"
