from collections.abc import Generator
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

from pygitweb.auth_config import AuthConfig, LocalUser
from pygitweb.main import app
from pygitweb.permissions import PERMISSION_SETTINGS, permissions_with_defaults

_SETTINGS_AUTH = AuthConfig(
    auth_mode="local",
    local_users=[LocalUser(user="admin", password="secret"), LocalUser(user="viewer", password="secret")],
    oauth_permissions=permissions_with_defaults({
        PERMISSION_SETTINGS: ["admin"],
    }),
)


@pytest.fixture
def client() -> Generator[TestClient, None, None]:
    with TestClient(app) as c:
        yield c


@pytest.fixture
def settings_auth() -> Generator[AuthConfig, None, None]:
    with (
        patch("pygitweb.auth_config.auth_config", _SETTINGS_AUTH),
        patch("pygitweb.auth.auth_config", _SETTINGS_AUTH),
        patch("pygitweb.main.auth_config", _SETTINGS_AUTH),
    ):
        yield _SETTINGS_AUTH


def _login(client: TestClient, username: str) -> None:
    client.post(
        "/login",
        data={"username": username, "password": "secret", "next": "/"},
        follow_redirects=False,
    )


def test_settings_open_when_auth_disabled(client: TestClient) -> None:
    from pygitweb.config import settings

    with patch.object(settings, "AUTH", False):
        r = client.get("/settings/pygitweb")
    assert r.status_code == 200


def test_settings_forbidden_without_login(client: TestClient, settings_auth: AuthConfig) -> None:
    from pygitweb.config import settings

    with patch.object(settings, "AUTH", True):
        r = client.get("/settings/pygitweb")
    assert r.status_code == 401


def test_settings_allowed_with_grant(client: TestClient, settings_auth: AuthConfig) -> None:
    from pygitweb.config import settings

    with patch.object(settings, "AUTH", True):
        _login(client, "admin")
        r = client.get("/settings/pygitweb")
    assert r.status_code == 200


def test_settings_forbidden_without_grant(client: TestClient, settings_auth: AuthConfig) -> None:
    from pygitweb.config import settings

    with patch.object(settings, "AUTH", True):
        _login(client, "viewer")
        r = client.get("/settings/pygit2")
    assert r.status_code == 403


def test_shutdown_forbidden_without_grant(client: TestClient, settings_auth: AuthConfig) -> None:
    from pygitweb.config import settings

    with patch.object(settings, "AUTH", True):
        _login(client, "viewer")
        r = client.post("/settings/pygitweb/shutdown")
    assert r.status_code == 403


def test_shutdown_allowed_with_grant(client: TestClient, settings_auth: AuthConfig) -> None:
    from pygitweb.config import settings

    app.state.can_signal_shutdown = True
    with patch.object(settings, "AUTH", True), patch("pygitweb.shutdown.os.kill") as kill:
        _login(client, "admin")
        r = client.post("/settings/pygitweb/shutdown")
    assert r.status_code == 200
    assert "Shutting down" in r.text
    assert app.state.shutting_down is True
    kill.assert_called_once()