from __future__ import annotations

from pathlib import Path

from fastapi.testclient import TestClient

from argus.service.app import create_app


def _write_config(tmp_path: Path) -> Path:
    p = tmp_path / "cfg.yaml"
    p.write_text(
        """
ray:
  address: "http://127.0.0.1:8265"
  shared_root: "/private"
  entrypoint_num_cpus: 1
  entrypoint_resources: { worker_node: 1 }
  runtime_env: { env_vars: { PYTHONUNBUFFERED: "1" } }
service:
  api: { host: "127.0.0.1", port: 8080 }
  auth: { token_env: "MVP_INTERNAL_TOKEN" }
  sqlite: { db_path: "%(db)s" }
data:
  user_root: "%(users)s"
  sftpgo: { enabled: false }
  retention: { jobs_trash_after_days: 3, jobs_purge_after_days: 7, janitor_interval_s: 3600 }
"""
        % {"db": str(tmp_path / "mvp.sqlite3"), "users": str(tmp_path / "users")}
    )
    return p


def test_ui_routes_render_200(tmp_path, monkeypatch):
    cfg = _write_config(tmp_path)
    monkeypatch.setenv("MVP_INTERNAL_TOKEN", "admin-token")
    app = create_app(str(cfg))
    c = TestClient(app)

    for path in (
        "/ui",
        "/ui/login",
        "/ui/tasks",
        "/ui/tasks/new",
        "/ui/data",
        "/ui/admin",
        "/ui/tasks/any-task-id",
        "/ui/tasks/any-task-id/logs",
    ):
        r = c.get(path, allow_redirects=True)
        assert r.status_code == 200
        assert "<html" in r.text.lower()


def test_ui_contains_sidebar_links(tmp_path, monkeypatch):
    cfg = _write_config(tmp_path)
    monkeypatch.setenv("MVP_INTERNAL_TOKEN", "admin-token")
    app = create_app(str(cfg))
    c = TestClient(app)

    r = c.get("/ui/tasks")
    assert r.status_code == 200
    for link in ("/ui/tasks", "/ui/tasks/new", "/ui/data", "/ui/login", "/ui/admin"):
        assert link in r.text
    assert "Ray Dashboard" in r.text


def test_ui_task_detail_shows_ids(tmp_path, monkeypatch):
    cfg = _write_config(tmp_path)
    monkeypatch.setenv("MVP_INTERNAL_TOKEN", "admin-token")
    app = create_app(str(cfg))
    c = TestClient(app)

    task_id = "mvp3-ppo-20250101-000000-aaaa"
    r = c.get(f"/ui/tasks/{task_id}")
    assert r.status_code == 200
    assert task_id in r.text
    assert f"/ui/tasks/{task_id}/logs" in r.text
