from __future__ import annotations

import json

from krim_sdk import Agent, AgentOptions, Model, ModelResponse
from krim_sdk.session import load_session


class NoopModel(Model):
    def chat(self, messages, tools, stream_callback=None):
        return ModelResponse(text="done", tool_calls=[], stop=True)


def test_load_session_reads_saved_metadata(tmp_path) -> None:
    session_dir = tmp_path / "session"
    session_dir.mkdir()
    (session_dir / "messages.json").write_text(json.dumps([
        {"role": "system", "content": "sys"},
        {"role": "user", "content": "hello"},
    ]))
    (session_dir / "meta.json").write_text(json.dumps({
        "provider": "openai",
        "model": "gpt-5.2",
    }))

    snapshot = load_session(session_dir)

    assert snapshot.path == session_dir.resolve()
    assert snapshot.provider == "openai"
    assert snapshot.model == "gpt-5.2"
    assert snapshot.system_prompt == "sys"
    assert snapshot.messages[1]["content"] == "hello"


def test_agent_accepts_initial_messages_without_dup_system() -> None:
    initial_messages = [
        {"role": "system", "content": "sys"},
        {"role": "user", "content": "previous"},
    ]

    agent = Agent(
        NoopModel(),
        "claude",
        "fresh sys",
        [],
        options=AgentOptions(initial_messages=initial_messages),
    )

    assert agent.messages == initial_messages
    assert agent._system_prompt == "sys"
