"""Route-level tests for the events blueprint.

Full HTTP integration tests require a live DB (not available in unit test
environment). These tests cover structural correctness and the route
module's public interface; ownership enforcement is covered by the service
tests in test_events_service.py.
"""


def test_events_blueprint_registered():
    """events_bp must be importable and have the correct name."""
    from fabledassistant.routes.events import events_bp
    assert events_bp.name == "events"
    assert events_bp.url_prefix == "/api/events"


def test_events_blueprint_has_five_routes():
    """Blueprint must declare routes for GET/POST '' and GET/PATCH/DELETE '/<id>'."""
    from fabledassistant.routes.events import events_bp
    methods_by_rule: dict[str, set[str]] = {}
    for rule in events_bp.deferred_functions:
        pass  # deferred; inspect via url_map after binding
    # Import routes module to confirm all 5 view functions exist
    from fabledassistant.routes import events as events_module
    assert callable(events_module.list_events)
    assert callable(events_module.create_event)
    assert callable(events_module.get_event)
    assert callable(events_module.update_event)
    assert callable(events_module.delete_event)


def test_events_blueprint_registered_in_app():
    """events_bp must be registered in the app factory."""
    from fabledassistant.app import create_app
    app = create_app()
    # Check the blueprint is present in the app's blueprints dict
    assert "events" in app.blueprints


def test_events_service_ownership_enforced_on_get():
    """get_event returns None for a different user — route will 404."""
    # Ownership is enforced by the service filtering by user_id.
    # The service returns None when the event belongs to a different user,
    # and the route converts that to a 404 response.
    import inspect
    from fabledassistant.services import events as events_svc
    sig = inspect.signature(events_svc.get_event)
    assert "user_id" in sig.parameters
    assert "event_id" in sig.parameters


def test_events_service_ownership_enforced_on_update():
    """update_event takes user_id — route passes current user's id."""
    import inspect
    from fabledassistant.services import events as events_svc
    sig = inspect.signature(events_svc.update_event)
    assert "user_id" in sig.parameters
    assert "event_id" in sig.parameters


def test_events_service_ownership_enforced_on_delete():
    """delete_event takes user_id — route verifies ownership before deleting."""
    import inspect
    from fabledassistant.services import events as events_svc
    sig = inspect.signature(events_svc.delete_event)
    assert "user_id" in sig.parameters
    assert "event_id" in sig.parameters
