"""persist-all Wave 1 — annotate_generated_asset 결정론 테스트."""

import json

from app.services.image_capture.annotate import annotate_generated_asset


class _FakeAsset:
    """ImageAsset 컬럼만 흉내내는 plain 객체 (DB 불필요)."""

    def __init__(self):
        self.pipeline_role = None
        self.stage = None
        self.input_image_ids = None
        self.generation_call_id = None
        self.pipeline_metadata_json = None
        self.is_intermediate = False
        self.asset_type = "reference"


def test_sets_role_and_stage_defaults_to_role():
    a = annotate_generated_asset(_FakeAsset(), pipeline_role="reference_composite")
    assert a.pipeline_role == "reference_composite"
    assert a.stage == "reference_composite"  # stage 미지정 → role 재사용


def test_explicit_stage_overrides():
    a = annotate_generated_asset(
        _FakeAsset(), pipeline_role="scene_still", stage="scene_image"
    )
    assert a.stage == "scene_image"


def test_input_image_ids_uuid_only_json_and_cleans_empty():
    a = annotate_generated_asset(
        _FakeAsset(),
        pipeline_role="reference_composite",
        input_image_ids=["face-uuid", None, "", "  ", "outfit-uuid"],
    )
    assert json.loads(a.input_image_ids) == ["face-uuid", "outfit-uuid"]


def test_none_input_leaves_unset_but_empty_list_records_known_no_input():
    a1 = annotate_generated_asset(_FakeAsset(), pipeline_role="reference_face")
    assert a1.input_image_ids is None  # None=미상(루트)
    a2 = annotate_generated_asset(
        _FakeAsset(), pipeline_role="reference_face", input_image_ids=[]
    )
    assert json.loads(a2.input_image_ids) == []  # []=입력없음 명시


def test_metadata_merges_with_existing():
    fake = _FakeAsset()
    fake.pipeline_metadata_json = json.dumps({"keep": 1})
    a = annotate_generated_asset(
        fake, pipeline_role="scene_still", pipeline_metadata={"add": 2}
    )
    parsed = json.loads(a.pipeline_metadata_json)
    assert parsed == {"keep": 1, "add": 2}


def test_does_not_touch_is_intermediate_or_asset_type():
    fake = _FakeAsset()
    annotate_generated_asset(fake, pipeline_role="reference_composite")
    assert fake.is_intermediate is False  # 최종물 유지
    assert fake.asset_type == "reference"


def test_generation_call_id_set():
    a = annotate_generated_asset(
        _FakeAsset(), pipeline_role="scene_still", generation_call_id="call-123"
    )
    assert a.generation_call_id == "call-123"
