"""
Synor Hosting SDK Types
"""

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


class Network(str, Enum):
    """Network environment."""
    MAINNET = "mainnet"
    TESTNET = "testnet"
    DEVNET = "devnet"


class DomainStatus(str, Enum):
    """Domain status."""
    PENDING = "pending"
    ACTIVE = "active"
    EXPIRED = "expired"
    SUSPENDED = "suspended"


class DeploymentStatus(str, Enum):
    """Deployment status."""
    PENDING = "pending"
    BUILDING = "building"
    DEPLOYING = "deploying"
    ACTIVE = "active"
    FAILED = "failed"
    INACTIVE = "inactive"


class CertificateStatus(str, Enum):
    """Certificate status."""
    PENDING = "pending"
    ISSUED = "issued"
    EXPIRED = "expired"
    REVOKED = "revoked"


class DnsRecordType(str, Enum):
    """DNS record type."""
    A = "A"
    AAAA = "AAAA"
    CNAME = "CNAME"
    TXT = "TXT"
    MX = "MX"
    NS = "NS"
    SRV = "SRV"
    CAA = "CAA"


@dataclass
class HostingConfig:
    """Hosting configuration."""
    api_key: str
    endpoint: str = "https://hosting.synor.io/v1"
    network: Network = Network.MAINNET
    timeout: float = 60.0
    retries: int = 3
    debug: bool = False


# ==================== Domain Types ====================

@dataclass
class DomainRecord:
    """Domain record for resolution."""
    cid: Optional[str] = None
    ipv4: Optional[List[str]] = None
    ipv6: Optional[List[str]] = None
    cname: Optional[str] = None
    txt: Optional[List[str]] = None
    metadata: Optional[Dict[str, str]] = None


@dataclass
class Domain:
    """Domain information."""
    name: str
    status: DomainStatus
    owner: str
    registered_at: int
    expires_at: int
    auto_renew: bool = True
    records: Optional[DomainRecord] = None


@dataclass
class DomainAvailability:
    """Domain availability check result."""
    name: str
    available: bool
    price: Optional[float] = None
    premium: bool = False


# ==================== DNS Types ====================

@dataclass
class DnsRecord:
    """DNS record."""
    type: DnsRecordType
    name: str
    value: str
    ttl: int = 3600
    priority: Optional[int] = None  # For MX and SRV


@dataclass
class DnsZone:
    """DNS zone."""
    domain: str
    records: List[DnsRecord]
    updated_at: int


# ==================== Deployment Types ====================

@dataclass
class RedirectRule:
    """Redirect rule."""
    source: str
    destination: str
    status_code: int = 301
    permanent: bool = True


@dataclass
class Deployment:
    """Deployment information."""
    id: str
    domain: str
    cid: str
    status: DeploymentStatus
    url: str
    created_at: int
    updated_at: int
    build_logs: Optional[str] = None
    error_message: Optional[str] = None


@dataclass
class DeploymentStats:
    """Deployment statistics."""
    requests: int
    bandwidth: int
    unique_visitors: int
    period: str


# ==================== SSL Types ====================

@dataclass
class Certificate:
    """SSL certificate."""
    domain: str
    status: CertificateStatus
    auto_renew: bool
    issuer: str
    issued_at: Optional[int] = None
    expires_at: Optional[int] = None
    fingerprint: Optional[str] = None


# ==================== Site Configuration ====================

@dataclass
class SiteConfig:
    """Site configuration."""
    domain: str
    cid: Optional[str] = None
    headers: Optional[Dict[str, str]] = None
    redirects: Optional[List[RedirectRule]] = None
    error_pages: Optional[Dict[str, str]] = None
    spa: bool = False
    clean_urls: bool = True
    trailing_slash: bool = False


# ==================== Analytics ====================

@dataclass
class PageView:
    """Page view data."""
    path: str
    views: int


@dataclass
class Referrer:
    """Referrer data."""
    referrer: str
    count: int


@dataclass
class Country:
    """Country data."""
    country: str
    count: int


@dataclass
class AnalyticsData:
    """Analytics data."""
    domain: str
    period: str
    page_views: int
    unique_visitors: int
    bandwidth: int
    top_pages: List[PageView]
    top_referrers: List[Referrer]
    top_countries: List[Country]


# ==================== Errors ====================

class HostingError(Exception):
    """Base exception for hosting errors."""

    def __init__(self, message: str, code: Optional[str] = None, status_code: Optional[int] = None):
        super().__init__(message)
        self.code = code
        self.status_code = status_code


class DomainNotFoundError(HostingError):
    """Raised when a domain is not found."""

    def __init__(self, domain: str):
        super().__init__(f"Domain not found: {domain}", "DOMAIN_NOT_FOUND", 404)


class DomainUnavailableError(HostingError):
    """Raised when a domain is not available."""

    def __init__(self, domain: str):
        super().__init__(f"Domain is not available: {domain}", "DOMAIN_UNAVAILABLE", 409)
