"""One-time import: `fxhnt unlock-calendar-import --json PATH` loads the existing unlock_calendar.json into
the operational `unlock_events` table. No network."""
import json

from typer.testing import CliRunner

from fxhnt.adapters.persistence.unlock_calendar_repo import UnlockCalendarRepo
from fxhnt.cli import app

_EVENTS = [
    {"sym": "AAVE", "cliff_day": 20100, "n": 5.0e7, "cat": "insiders"},
    {"sym": "ARB", "cliff_day": 20150, "n": 1.2e8, "cat": "privateSale"},
]


def test_import_cli_populates_table(tmp_path, monkeypatch):
    dsn = f"sqlite:///{tmp_path / 'op.db'}"
    monkeypatch.setenv("FXHNT_OPERATIONAL_DSN", dsn)
    # bust the cached settings so the env DSN is picked up
    from fxhnt.config import get_settings
    get_settings.cache_clear()

    json_path = tmp_path / "unlock_calendar.json"
    json_path.write_text(json.dumps(_EVENTS))

    result = CliRunner().invoke(app, ["unlock-calendar-import", "--json", str(json_path)])
    assert result.exit_code == 0, result.output

    repo = UnlockCalendarRepo(dsn)
    got = repo.read_events()
    assert sorted(got, key=lambda e: e["sym"]) == sorted(_EVENTS, key=lambda e: e["sym"])
    get_settings.cache_clear()
