"""
Тесты профилей и глаголов запуска генератора.
"""

from datetime import datetime, timezone


def test_daily_wave_is_the_default_for_api_and_cli(monkeypatch):
    """Ручные точки входа без профиля выбирают daily-wave."""
    from clickstream_generator.launch import _parse_args, build_launch_env

    monkeypatch.delenv("PROFILE", raising=False)
    monkeypatch.delenv("GEN_LAUNCH_PROFILE", raising=False)

    assert build_launch_env("backfill")["GEN_LAUNCH_PROFILE"] == "daily-wave"
    assert _parse_args(["backfill"]).profile == "daily-wave"


def test_duration_2d_sets_model_t_end_from_t0():
    """Длительность 2d считается от T0 без ручного GEN_MODEL_T_END."""
    from clickstream_generator.launch import build_launch_env

    env = build_launch_env("backfill", profile_name="ci", duration="2d")

    assert env["GEN_MODEL_T0"] == "2026-01-01T00:00:00+00:00"
    assert env["GEN_MODEL_T_END"] == "2026-01-03T00:00:00+00:00"


def test_daily_wave_profile_uses_three_days_by_default():
    """Профиль daily-wave даёт историю с суточной волной."""
    from clickstream_generator.launch import build_launch_env

    env = build_launch_env("backfill", profile_name="daily-wave")

    assert env["GEN_LAUNCH_PROFILE"] == "daily-wave"
    assert env["GEN_HISTORY_DURATION"] == "3d"
    assert env["GEN_MODEL_T_END"] == "2026-01-04T00:00:00+00:00"
    assert env["GEN_MODEL_TIME_SPEED"] == "60"
    assert env["GEN_TICK_SECONDS"] == "1"


def test_backfill_verb_maps_to_existing_low_level_flags():
    """Глагол backfill оставляет прежнюю механику запуска под капотом."""
    from clickstream_generator.launch import build_launch_env

    env = build_launch_env("backfill", profile_name="ci")

    assert env["GEN_RUN_MODE"] == "backfill"
    assert env["GEN_STATE_RESET"] == "true"


def test_continue_and_reset_verbs_map_to_existing_low_level_flags():
    """Глаголы continue и reset задают только старый контракт state."""
    from clickstream_generator.launch import build_launch_env

    continue_env = build_launch_env("continue", profile_name="ci")
    reset_env = build_launch_env(
        "reset",
        profile_name="ci",
        overrides={"GEN_MODEL_T_END": "2026-01-09T00:00:00+00:00"},
    )

    assert continue_env["GEN_RUN_MODE"] == "live"
    assert continue_env["GEN_STATE_RESET"] == "false"
    assert continue_env["GEN_MODEL_T_END"] == "2026-01-01T06:00:00+00:00"
    assert reset_env["GEN_RUN_MODE"] == "live"
    assert reset_env["GEN_STATE_RESET"] == "true"
    assert "GEN_MODEL_T_END" not in reset_env


def test_next_day_verb_restores_snapshot_without_reset():
    """Глагол next-day включает отдельный ограниченный режим от текущей границы."""
    from clickstream_generator.launch import build_launch_env

    env = build_launch_env(
        "next-day",
        profile_name="ci",
        overrides={
            "GEN_MODEL_T0": "2026-01-01T00:00:00+00:00",
            "GEN_MODEL_T_END": "2026-01-03T00:00:00+00:00",
        },
    )

    assert env["GEN_RUN_MODE"] == "next-day"
    assert env["GEN_STATE_RESET"] == "false"
    assert env["GEN_MODEL_T_END"] == "2026-01-03T00:00:00+00:00"


def test_ci_profile_keeps_previous_default_backfill_window():
    """CI-профиль сохраняет прежний 6-часовой проверочный запуск."""
    from clickstream_generator.launch import build_launch_env

    env = build_launch_env("backfill", profile_name="ci")

    t0 = datetime.fromisoformat(env["GEN_MODEL_T0"])
    t_end = datetime.fromisoformat(env["GEN_MODEL_T_END"])
    assert t0 == datetime(2026, 1, 1, 0, 0, tzinfo=timezone.utc)
    assert (t_end - t0).total_seconds() == 6 * 60 * 60
    assert env["GEN_MODEL_TIME_SPEED"] == "1"
    assert env["GEN_TICK_SECONDS"] == "60"
    assert env["GEN_LAMBDA_BASE_PER_MIN"] == "60"
    assert env["GEN_JITTER_PCT"] == "0"
