from pathlib import Path

from scripts import demo_seed, local_stack


def test_local_stack_gateway_env_contains_expected_urls():
    env = local_stack.build_gateway_env()

    assert env["AUTH_SERVICE_URL"] == "http://127.0.0.1:8001"
    assert env["INTERACTION_SERVICE_URL"] == "http://127.0.0.1:8004"
    assert env["SUPERVISOR_SERVICE_URL"] == "http://127.0.0.1:8010"
    assert env["RECORDING_SERVICE_URL"] == "http://127.0.0.1:8013"
    assert env["IVR_SERVICE_URL"] == "http://127.0.0.1:8014"
    assert env["WEBCHAT_ADAPTER_SERVICE_URL"] == "http://127.0.0.1:8011"
    assert env["EMAIL_ADAPTER_SERVICE_URL"] == "http://127.0.0.1:8012"
    assert env["AI_VOICE_RUNTIME_SERVICE_URL"] == "http://127.0.0.1:8018"
    assert env["SALES_SERVICE_URL"] == "http://127.0.0.1:8020"


def test_local_stack_service_specs_include_ai_voice_runtime():
    names = {spec["name"] for spec in local_stack.build_service_specs()}

    assert "ai" in names
    assert "ai-voice-runtime" in names
    assert "asterisk-bridge" in names
    assert "sales" in names


def test_local_stack_manifest_payload_has_core_fields(tmp_path: Path):
    services = [{"name": "gateway", "port": 8080, "pid": 12345}]

    payload = local_stack.build_manifest_payload(tmp_path / "runtime", tmp_path / "data", services)

    assert payload["base_url"] == "http://127.0.0.1:8080"
    assert payload["services"] == services
    assert payload["runtime_dir"] == str(tmp_path / "runtime")
    assert payload["data_dir"] == str(tmp_path / "data")


def test_demo_seed_plan_contains_demo_keyword():
    plan = demo_seed.build_seed_plan("TAG123")

    assert plan["queue_name"] == "Demo Queue"
    assert plan["kb_category"] == "Demo Knowledge"
    assert plan["kb_keyword"] == "demo-showcase"
    assert plan["telegram_text"] == "Demo inbound Telegram message"
    assert plan["webchat_text"] == "Demo webchat request from the website"
    assert plan["email_subject"] == "Demo email request about account access"
    assert plan["recording_file_name"] == "demo-call.wav"
    assert (
        plan["ivr_root_prompt"]
        == "Здравствуйте! Добро пожаловать в контакт-центр. Для русского языка нажмите цифру два. "
        "Саламатсыз ба! Қазақ тілін таңдау үшін бір цифрын теріңіз."
    )
    assert plan["ivr_ru_menu_prompt"] == "Для отдела продаж нажмите 1. Для службы поддержки нажмите 2."
    assert plan["ivr_kz_menu_prompt"] == "Сату бөлімі үшін 1 басыңыз. Қолдау қызметі үшін 2 басыңыз."


def test_demo_ivr_flow_routes_language_to_voice_start_queues():
    plan = demo_seed.build_seed_plan("TAG123")
    flow = demo_seed._demo_ivr_flow_document(
        plan,
        voice_start_kz_queue_id="que_kz",
        voice_start_ru_queue_id="que_ru",
    )

    root = flow["nodes"][0]
    assert root["options"] == [
        {"digit": "1", "target_node_id": "voice_start_kz"},
        {"digit": "2", "target_node_id": "voice_start_ru"},
    ]
    assert flow["nodes"][1]["resolved_queue_code"] == "voice_start_ru"
    assert flow["nodes"][2]["resolved_queue_code"] == "voice_start_kz"


def test_prepare_demo_script_resets_local_data_and_prints_backstage_hints():
    script = (Path(__file__).resolve().parents[1] / "scripts" / "prepare_demo.ps1").read_text(encoding="utf-8")

    assert "python scripts\\local_stack.py stop" in script
    assert "Remove-Item -Path $dataDir -Recurse -Force" in script
    assert "Login: admin / admin123" in script
    assert "KB keyword: demo-showcase" in script
