from datetime import date, datetime

from sqlalchemy import Date, DateTime, Float, ForeignKey, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base


class FundamentalData(Base):
    __tablename__ = "fundamental_data"
    __table_args__ = (
        UniqueConstraint("ticker_id", name="uq_fundamental_data_ticker"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    ticker_id: Mapped[int] = mapped_column(
        ForeignKey("tickers.id", ondelete="CASCADE"), nullable=False
    )
    pe_ratio: Mapped[float | None] = mapped_column(Float, nullable=True)
    revenue_growth: Mapped[float | None] = mapped_column(Float, nullable=True)
    earnings_surprise: Mapped[float | None] = mapped_column(Float, nullable=True)
    market_cap: Mapped[float | None] = mapped_column(Float, nullable=True)
    next_earnings_date: Mapped[date | None] = mapped_column(Date, nullable=True)
    fetched_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False
    )
    unavailable_fields_json: Mapped[str] = mapped_column(
        Text, nullable=False, default="{}"
    )

    ticker = relationship("Ticker", back_populates="fundamental_data")
