"""Pre-split regression tests for auth/register/invites routes."""
from __future__ import annotations

import pytest
from fastapi.testclient import TestClient


class TestLogin:
    def test_missing_body_returns_422(self, client: TestClient):
        r = client.post("/api/auth/login", json={})
        assert r.status_code == 422

    def test_wrong_credentials_returns_401(self, client: TestClient):
        r = client.post("/api/auth/login", json={"handle": "nobody", "password": "x"})
        assert r.status_code == 401

    def test_valid_login_sets_cookie(self, user_client: TestClient, tmp_data):
        from bincio.serve.db import open_db, authenticate
        db = open_db(tmp_data)
        assert authenticate(db, "alice", "alicepass1") is not None
        r = user_client.post("/api/auth/login",
                             json={"handle": "alice", "password": "alicepass1"})
        assert r.status_code == 200
        assert r.json()["handle"] == "alice"


class TestToken:
    def test_missing_body_returns_422(self, client: TestClient):
        r = client.post("/api/auth/token", json={})
        assert r.status_code == 422

    def test_wrong_credentials_returns_401(self, client: TestClient):
        r = client.post("/api/auth/token", json={"handle": "nobody", "password": "x"})
        assert r.status_code == 401

    def test_valid_token_in_body(self, user_client: TestClient):
        r = user_client.post("/api/auth/token",
                             json={"handle": "alice", "password": "alicepass1"})
        assert r.status_code == 200
        assert "token" in r.json()


class TestLogout:
    def test_logout_unauthenticated_returns_200(self, client: TestClient):
        r = client.post("/api/auth/logout")
        assert r.status_code == 200

    def test_logout_authenticated_returns_200(self, user_client: TestClient):
        r = user_client.post("/api/auth/logout")
        assert r.status_code == 200


class TestResetPassword:
    def test_missing_body_returns_422(self, client: TestClient):
        r = client.post("/api/auth/reset-password", json={})
        assert r.status_code == 422

    def test_invalid_code_returns_400(self, client: TestClient):
        r = client.post("/api/auth/reset-password",
                        json={"handle": "nobody", "code": "BADCODE", "password": "newpass123"})
        assert r.status_code == 400

    def test_short_password_returns_400(self, client: TestClient):
        r = client.post("/api/auth/reset-password",
                        json={"handle": "nobody", "code": "BADCODE", "password": "short"})
        assert r.status_code == 400


class TestRegister:
    def test_missing_body_returns_422(self, client: TestClient):
        r = client.post("/api/register", json={})
        assert r.status_code == 422

    def test_invalid_invite_returns_400(self, client: TestClient):
        r = client.post("/api/register",
                        json={"code": "BADCODE", "handle": "bob", "password": "bobpass123"})
        assert r.status_code == 400

    def test_invalid_handle_returns_400(self, client: TestClient):
        r = client.post("/api/register",
                        json={"code": "BADCODE", "handle": "BOB INVALID", "password": "bobpass123"})
        assert r.status_code in (400, 422)


class TestInvites:
    def test_unauthenticated_returns_401(self, client: TestClient):
        assert client.get("/api/invites").status_code == 401
        assert client.post("/api/invites", json={}).status_code == 401

    def test_authenticated_get_returns_list(self, user_client: TestClient):
        r = user_client.get("/api/invites")
        assert r.status_code == 200
        assert isinstance(r.json(), list)

    def test_create_invite(self, user_client: TestClient):
        r = user_client.post("/api/invites", json={"grants_activity": False})
        assert r.status_code == 200
        assert "code" in r.json()
