import json
import pytest
from notifiedby.exceptions import (
    NotifiedByError,
    NotifiedByAPIError,
    NotifiedByConfigError,
    NotifiedByValidationError,
    NotifiedByAuthError,
    NotifiedByNotFoundError,
)


def test_notifiedby_error():
    """Test base NotifiedByError exception."""
    error = NotifiedByError("Test error")
    assert str(error) == "Test error"
    assert isinstance(error, Exception)


def test_notifiedby_config_error():
    """Test NotifiedByConfigError exception."""
    error = NotifiedByConfigError("API key missing")
    assert str(error) == "API key missing"
    assert isinstance(error, NotifiedByError)


def test_notifiedby_api_error_basic():
    """Test NotifiedByAPIError with basic message."""
    error = NotifiedByAPIError(500, "Server error", endpoint="/test/")
    assert error.status_code == 500
    assert error.response_text == "Server error"
    assert error.endpoint == "/test/"
    assert "HTTP 500" in str(error)
    assert "/test/" in str(error)
    assert "Server error" in str(error)


def test_notifiedby_api_error_json_detail():
    """Test NotifiedByAPIError parsing JSON error detail."""
    error_json = json.dumps({"detail": "Invalid flow trigger"})
    error = NotifiedByAPIError(400, error_json, endpoint="/flows/subscribe/")
    assert "Invalid flow trigger" in str(error)
    assert "HTTP 400" in str(error)


def test_notifiedby_api_error_invalid_json():
    """Test NotifiedByAPIError with non-JSON response."""
    error = NotifiedByAPIError(500, "Plain text error", endpoint="/test/")
    assert "Plain text error" in str(error)


def test_notifiedby_validation_error():
    """Test NotifiedByValidationError inherits from NotifiedByAPIError."""
    error = NotifiedByValidationError(400, "Bad request", endpoint="/test/")
    assert isinstance(error, NotifiedByAPIError)
    assert error.status_code == 400


def test_notifiedby_auth_error():
    """Test NotifiedByAuthError inherits from NotifiedByAPIError."""
    error = NotifiedByAuthError(401, "Unauthorized", endpoint="/test/")
    assert isinstance(error, NotifiedByAPIError)
    assert error.status_code == 401


def test_notifiedby_not_found_error():
    """Test NotifiedByNotFoundError inherits from NotifiedByAPIError."""
    error = NotifiedByNotFoundError(404, "Not found", endpoint="/test/")
    assert isinstance(error, NotifiedByAPIError)
    assert error.status_code == 404
