"""
GuardrailX Python SDK - Type Definitions

Type hints and data classes for policy decisions and configurations.
"""

from dataclasses import dataclass, field
from typing import Any, Dict, List, Literal, Optional


@dataclass
class MaskConfig:
    """Configuration for masking sensitive data."""
    strategy: Optional[Literal["redact", "hash", "partial"]] = None
    patterns: Optional[List[str]] = None
    fields: Optional[List[str]] = None


@dataclass
class PolicyDecision:
    """Policy decision from policy-core."""
    action: Literal["allow", "mask", "block", "log"]
    rule_id: Optional[str] = None
    policy_id: Optional[str] = None
    policy_version: Optional[str] = None
    matched_rules: Optional[List[str]] = None
    tenant_id: Optional[str] = None
    mask: Optional[MaskConfig] = None
    explanation: Optional[str] = None


@dataclass
class GatewayDecision:
    """Gateway decision from gateway proxy."""
    action: Literal["allow", "mask", "block", "deny", "error"]
    rule_id: Optional[str] = None
    policy_id: Optional[str] = None
    policy_version: Optional[str] = None
    matched_rules: Optional[List[str]] = None
    tenant_id: Optional[str] = None
    trace_id: Optional[str] = None
    masked: Optional[Any] = None
    request: Optional[Any] = None
    reason: Optional[str] = None
    message: Optional[str] = None
    explanation: Optional[str] = None


@dataclass
class GuardrailxClientOptions:
    """Client configuration options."""
    base_url: str
    token: Optional[str] = None
    api_key: Optional[str] = None
    tenant_id: Optional[str] = None
    user_id: Optional[str] = None
    timeout_ms: int = 8000
    retries: int = 1
    retry_delay_ms: int = 250
    headers: Optional[Dict[str, str]] = field(default_factory=dict)


@dataclass
class SanitizeRequest:
    """Request payload for sanitizing messages."""
    messages: List[Dict[str, str]]
    metadata: Optional[Dict[str, Any]] = None


@dataclass
class SanitizeResponse:
    """Response from sanitize operation."""
    sanitized: List[Dict[str, str]]
    session_id: str
    detections: List[Dict[str, str]]
    tokens_used: int


@dataclass
class RehydrateRequest:
    """Request payload for rehydrating messages."""
    messages: List[Dict[str, str]]
    session_id: str


@dataclass
class RehydrateResponse:
    """Response from rehydrate operation."""
    original: List[Dict[str, str]]
    tokens_restored: int


@dataclass
class LLMRequest:
    """Request payload for LLM calls."""
    prompt: Optional[str] = None
    messages: Optional[List[Dict[str, str]]] = None
    model: Optional[str] = None
    temperature: Optional[float] = None
    max_tokens: Optional[int] = None
    extra: Optional[Dict[str, Any]] = field(default_factory=dict)


class GuardrailxError(Exception):
    """Error from GuardrailX API."""
    
    def __init__(self, message: str, status: Optional[int] = None, response: Optional[Any] = None):
        super().__init__(message)
        self.status = status
        self.response = response
