from datetime import datetime
from typing import Optional, List, Dict, Any
from dataclasses import dataclass, field
from enum import Enum

class EventType(Enum):
    """License event types"""
    ACTIVATION = "activation"
    DEACTIVATION = "deactivation"
    REACTIVATION = "reactivation"
    TRANSFER = "transfer"

class AnomalyType(Enum):
    """Anomaly detection types"""
    MULTIPLE_IPS = "multiple_ips"
    RAPID_HARDWARE_CHANGE = "rapid_hardware_change"
    SUSPICIOUS_PATTERN = "suspicious_pattern"
    CONCURRENT_USE = "concurrent_use"
    GEO_ANOMALY = "geo_anomaly"

class Severity(Enum):
    """Anomaly severity levels"""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    CRITICAL = "critical"

@dataclass
class License:
    """License domain model"""
    id: str
    license_key: str
    customer_id: str
    max_devices: int
    is_active: bool
    is_test: bool
    created_at: datetime
    updated_at: datetime
    expires_at: Optional[datetime] = None
    features: List[str] = field(default_factory=list)
    metadata: Dict[str, Any] = field(default_factory=dict)

@dataclass
class LicenseToken:
    """Offline validation token"""
    id: str
    license_id: str
    token: str
    hardware_id: str
    valid_until: datetime
    created_at: datetime
    last_validated: Optional[datetime] = None
    validation_count: int = 0

@dataclass
class Heartbeat:
    """License heartbeat"""
    id: int
    license_id: str
    hardware_id: str
    ip_address: Optional[str]
    user_agent: Optional[str]
    app_version: Optional[str]
    timestamp: datetime
    session_data: Dict[str, Any] = field(default_factory=dict)

@dataclass
class ActivationEvent:
    """License activation event"""
    id: str
    license_id: str
    event_type: EventType
    hardware_id: Optional[str]
    previous_hardware_id: Optional[str]
    ip_address: Optional[str]
    user_agent: Optional[str]
    success: bool
    error_message: Optional[str]
    metadata: Dict[str, Any] = field(default_factory=dict)
    created_at: datetime

@dataclass
class AnomalyDetection:
    """Detected anomaly"""
    id: str
    license_id: str
    anomaly_type: AnomalyType
    severity: Severity
    details: Dict[str, Any]
    detected_at: datetime
    resolved: bool = False
    resolved_at: Optional[datetime] = None
    resolved_by: Optional[str] = None
    action_taken: Optional[str] = None

@dataclass
class Session:
    """Active session"""
    id: str
    license_id: str
    hardware_id: str
    session_token: str
    ip_address: Optional[str]
    started_at: datetime
    last_seen: datetime
    expires_at: datetime

@dataclass
class ValidationRequest:
    """License validation request"""
    license_key: str
    hardware_id: str
    app_version: Optional[str] = None
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None

@dataclass
class ValidationResponse:
    """License validation response"""
    valid: bool
    license_id: Optional[str] = None
    token: Optional[str] = None
    expires_at: Optional[datetime] = None
    features: List[str] = field(default_factory=list)
    limits: Dict[str, Any] = field(default_factory=dict)
    error: Optional[str] = None
    error_code: Optional[str] = None