from __future__ import annotations

from app.modules.agent.story_session_recorder import StorySessionRecorder
from app.schemas.changeset import ChangeItem, ChangeOp


class FakeStoryRepo:
    def __init__(self) -> None:
        self.calls: list[dict] = []

    def add_session_artifact(self, **kwargs) -> None:
        self.calls.append(kwargs)


def test_record_run_stores_attachment_and_changeset_artifacts() -> None:
    repo = FakeStoryRepo()
    recorder = StorySessionRecorder(repo)

    recorder.record_run(
        dialog_session_id="dialog-1",
        rag_session_id="rag-1",
        scenario="docs_from_analytics",
        attachments=[
            {"type": "confluence_url", "value": "https://example.org/doc"},
            {"type": "file_ref", "value": "local.md"},
        ],
        answer="Generated docs update summary",
        changeset=[
            ChangeItem(
                op=ChangeOp.UPDATE,
                path="docs/api.md",
                base_hash="abc",
                proposed_content="new",
                reason="sync endpoint section",
            )
        ],
    )

    assert len(repo.calls) == 3
    assert repo.calls[0]["artifact_role"] == "analysis"
    assert repo.calls[0]["source_ref"] == "https://example.org/doc"

    assert repo.calls[1]["artifact_role"] == "doc_change"
    assert repo.calls[1]["summary"] == "Generated docs update summary"

    assert repo.calls[2]["artifact_role"] == "doc_change"
    assert repo.calls[2]["path"] == "docs/api.md"
    assert repo.calls[2]["change_type"] == "updated"
