"""Tests for followup_research routes."""

import pytest
from flask import Flask


@pytest.fixture
def app():
    """Create a Flask test app with followup blueprint."""
    app = Flask(__name__)
    app.config["TESTING"] = True
    app.config["WTF_CSRF_ENABLED"] = False
    app.secret_key = "test-secret-key"

    from local_deep_research.followup_research.routes import followup_bp

    app.register_blueprint(followup_bp)
    return app


@pytest.fixture
def client(app):
    """Create test client."""
    return app.test_client()


class TestBlueprintConfiguration:
    """Tests for blueprint configuration."""

    def test_blueprint_url_prefix(self):
        """Blueprint has correct URL prefix."""
        from local_deep_research.followup_research.routes import followup_bp

        assert followup_bp.url_prefix == "/api/followup"

    def test_blueprint_name(self):
        """Blueprint has correct name."""
        from local_deep_research.followup_research.routes import followup_bp

        assert followup_bp.name == "followup"


class TestPrepareRouteRegistration:
    """Tests for /api/followup/prepare route registration."""

    def test_prepare_route_exists(self, app):
        """Prepare route is registered."""
        rules = [rule.rule for rule in app.url_map.iter_rules()]
        assert "/api/followup/prepare" in rules

    def test_prepare_route_methods(self, app):
        """Prepare route accepts POST only."""
        for rule in app.url_map.iter_rules():
            if rule.rule == "/api/followup/prepare":
                assert "POST" in rule.methods
                assert "GET" not in rule.methods or rule.methods == {
                    "GET",
                    "HEAD",
                    "OPTIONS",
                    "POST",
                }


class TestStartRouteRegistration:
    """Tests for /api/followup/start route registration."""

    def test_start_route_exists(self, app):
        """Start route is registered."""
        rules = [rule.rule for rule in app.url_map.iter_rules()]
        assert "/api/followup/start" in rules

    def test_start_route_methods(self, app):
        """Start route accepts POST only."""
        for rule in app.url_map.iter_rules():
            if rule.rule == "/api/followup/start":
                assert "POST" in rule.methods


class TestPrepareRouteValidation:
    """Tests for prepare endpoint input validation."""

    def test_prepare_requires_json(self, client):
        """Prepare endpoint requires JSON content type."""
        response = client.post(
            "/api/followup/prepare",
            data="not json",
            content_type="text/plain",
        )
        # Should fail somehow (either 400 or 415 or auth error)
        assert response.status_code == 401, response.status_code

    def test_prepare_empty_json(self, client):
        """Prepare with empty JSON body."""
        response = client.post(
            "/api/followup/prepare",
            json={},
        )
        # Should fail with validation or auth error
        assert response.status_code == 401, response.status_code


class TestStartRouteValidation:
    """Tests for start endpoint input validation."""

    def test_start_requires_json(self, client):
        """Start endpoint requires JSON content type."""
        response = client.post(
            "/api/followup/start",
            data="not json",
            content_type="text/plain",
        )
        # Should fail somehow
        assert response.status_code == 401, response.status_code

    def test_start_empty_json(self, client):
        """Start with empty JSON body."""
        response = client.post(
            "/api/followup/start",
            json={},
        )
        # Should fail with validation or auth error
        assert response.status_code == 401, response.status_code


class TestRouteAuthentication:
    """Tests for route authentication requirements."""

    def test_prepare_requires_login(self, client):
        """Prepare endpoint requires authentication."""
        response = client.post(
            "/api/followup/prepare",
            json={
                "parent_research_id": "test-123",
                "question": "Test question",
            },
        )
        # Should redirect to login or return 401
        assert response.status_code == 401, response.status_code

    def test_start_requires_login(self, client):
        """Start endpoint requires authentication."""
        response = client.post(
            "/api/followup/start",
            json={
                "parent_research_id": "test-123",
                "question": "Test question",
            },
        )
        # Should redirect to login or return 401
        assert response.status_code == 401, response.status_code
