"""TLD Price History model for tracking domain extension prices over time."""
from datetime import datetime
from sqlalchemy import String, DateTime, Float, Integer, Text
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class TLDPrice(Base):
    """
    TLD Price model for tracking domain extension prices.
    
    Stores historical pricing data for different TLDs (e.g., .com, .io, .co)
    from various registrars.
    """
    
    __tablename__ = "tld_prices"
    
    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    
    # TLD info
    tld: Mapped[str] = mapped_column(String(50), index=True, nullable=False)  # e.g., "com", "io", "co"
    
    # Registrar info
    registrar: Mapped[str] = mapped_column(String(100), nullable=False)  # e.g., "namecheap", "godaddy"
    
    # Pricing (in USD)
    registration_price: Mapped[float] = mapped_column(Float, nullable=False)  # First year
    renewal_price: Mapped[float] = mapped_column(Float, nullable=True)  # Renewal
    transfer_price: Mapped[float] = mapped_column(Float, nullable=True)  # Transfer
    
    # Currency
    currency: Mapped[str] = mapped_column(String(3), default="USD")
    
    # Timestamp
    recorded_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True)
    
    # Additional info
    promo_price: Mapped[float | None] = mapped_column(Float, nullable=True)  # Promotional price
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    
    def __repr__(self) -> str:
        return f"<TLDPrice .{self.tld} @ {self.registrar}: ${self.registration_price}>"


class TLDInfo(Base):
    """
    General TLD information and metadata.
    """
    
    __tablename__ = "tld_info"
    
    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    tld: Mapped[str] = mapped_column(String(50), unique=True, index=True, nullable=False)
    
    # Metadata
    type: Mapped[str] = mapped_column(String(50), nullable=True)  # generic, country-code, sponsored
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    registry: Mapped[str | None] = mapped_column(String(200), nullable=True)
    
    # Restrictions
    is_restricted: Mapped[bool] = mapped_column(default=False)
    restriction_notes: Mapped[str | None] = mapped_column(Text, nullable=True)
    
    # Popularity (for sorting)
    popularity_rank: Mapped[int | None] = mapped_column(Integer, nullable=True)
    
    # Timestamps
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def __repr__(self) -> str:
        return f"<TLDInfo .{self.tld}>"

