"""Unit tests for ConfigStore.

All tests use pytest's tmp_path fixture so they never touch the real
~/.skilark directory on the developer's machine.
"""

import pytest

from skilark_cli.config_store import ConfigStore, DEFAULT_API_URL


class TestIsFirstRun:
    def test_first_run_detected_when_no_file(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        assert store.is_first_run()

    def test_not_first_run_after_save(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(user_id="abc-123", topics=["python"])
        assert not store.is_first_run()


class TestSaveAndLoad:
    def test_save_and_load_roundtrip(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(
            user_id="abc-123",
            topics=["python", "kubernetes"],
            api_url="https://api.skilark.com",
        )
        config = store.load()
        assert config["user_id"] == "abc-123"
        assert config["topics"] == ["python", "kubernetes"]
        assert config["api_url"] == "https://api.skilark.com"

    def test_save_uses_default_api_url_when_omitted(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(user_id="abc-123", topics=["python"])
        config = store.load()
        assert config["api_url"] == DEFAULT_API_URL

    def test_save_creates_config_dir_if_missing(self, tmp_path):
        nested = tmp_path / "deep" / "nested"
        store = ConfigStore(config_dir=nested)
        store.save(user_id="x", topics=[])
        assert store.config_file.exists()

    def test_save_overwrites_existing_config(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(user_id="old-id", topics=["java"])
        store.save(user_id="new-id", topics=["go"])
        config = store.load()
        assert config["user_id"] == "new-id"
        assert config["topics"] == ["go"]

    def test_load_raises_when_no_file(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        with pytest.raises(FileNotFoundError):
            store.load()

    def test_save_empty_topics_list(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(user_id="abc-123", topics=[])
        config = store.load()
        assert config["topics"] == []


class TestUpdateTopics:
    def test_update_topics_replaces_list(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(user_id="abc-123", topics=["python"])
        store.update_topics(["python", "kubernetes", "spark"])
        config = store.load()
        assert config["topics"] == ["python", "kubernetes", "spark"]

    def test_update_topics_preserves_other_fields(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        store.save(
            user_id="abc-123",
            topics=["python"],
            api_url="https://api.skilark.com",
        )
        store.update_topics(["go"])
        config = store.load()
        # Ensure user_id and api_url are untouched
        assert config["user_id"] == "abc-123"
        assert config["api_url"] == "https://api.skilark.com"

    def test_update_topics_raises_when_no_file(self, tmp_path):
        store = ConfigStore(config_dir=tmp_path)
        with pytest.raises(FileNotFoundError):
            store.update_topics(["python"])
