"""Tests for named-dashboards CRUD + slug uniqueness."""

from __future__ import annotations

from pathlib import Path

from fastapi.testclient import TestClient

from media_library_viewer_api.dependencies import get_settings_store
from media_library_viewer_api.main import app
from media_library_viewer_api.services.settings_store import SettingsStore


def _client(tmp_path: Path) -> TestClient:
    store = SettingsStore(tmp_path / "settings.sqlite")
    app.dependency_overrides[get_settings_store] = lambda: store
    client = TestClient(app)
    client.store = store  # type: ignore[attr-defined]
    return client


def test_create_and_list_dashboards(tmp_path: Path):
    client = _client(tmp_path)
    try:
        resp = client.post(
            "/api/dashboards",
            json={"label": "Storage Overview", "payload": {"widgets": []}},
        )
        assert resp.status_code == 200
        created = resp.json()
        assert created["label"] == "Storage Overview"
        assert created["slug"] == "storage-overview"
        assert created["payload"] == {"widgets": []}

        listed = client.get("/api/dashboards").json()
        assert len(listed) == 1
        assert listed[0]["id"] == created["id"]
    finally:
        app.dependency_overrides.clear()


def test_update_dashboard(tmp_path: Path):
    client = _client(tmp_path)
    try:
        created = client.post("/api/dashboards", json={"label": "First"}).json()
        updated = client.put(
            f"/api/dashboards/{created['id']}",
            json={"label": "Renamed", "payload": {"widgets": ["w1"]}},
        ).json()
        assert updated["label"] == "Renamed"
        assert updated["payload"] == {"widgets": ["w1"]}
        assert updated["slug"] == "renamed"
    finally:
        app.dependency_overrides.clear()


def test_delete_dashboard(tmp_path: Path):
    client = _client(tmp_path)
    try:
        created = client.post("/api/dashboards", json={"label": "Temp"}).json()
        resp = client.delete(f"/api/dashboards/{created['id']}")
        assert resp.status_code == 200
        assert client.get("/api/dashboards").json() == []
    finally:
        app.dependency_overrides.clear()


def test_slug_collision_appends_suffix(tmp_path: Path):
    client = _client(tmp_path)
    try:
        first = client.post("/api/dashboards", json={"label": "Overview"}).json()
        second = client.post("/api/dashboards", json={"label": "Overview"}).json()
        assert first["slug"] == "overview"
        assert second["slug"] == "overview-2"
    finally:
        app.dependency_overrides.clear()


def test_explicit_slug_respected(tmp_path: Path):
    client = _client(tmp_path)
    try:
        created = client.post(
            "/api/dashboards",
            json={"label": "My Dashboard", "slug": "custom-slug"},
        ).json()
        assert created["slug"] == "custom-slug"
    finally:
        app.dependency_overrides.clear()


def test_update_nonexistent_returns_404(tmp_path: Path):
    client = _client(tmp_path)
    try:
        resp = client.put("/api/dashboards/nope", json={"label": "X"})
        assert resp.status_code == 404
    finally:
        app.dependency_overrides.clear()
