import uuid
from datetime import datetime

from sqlalchemy import String, Integer, Float, ForeignKey, Index, func
from sqlalchemy import Uuid
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base, JSONType


class MonitoringRecord(Base):
    __tablename__ = "monitoring_records"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    user_id: Mapped[str] = mapped_column(
        String(36),
        ForeignKey("users.id", ondelete="CASCADE"),
        nullable=False,
    )
    brand_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("brands.id", ondelete="CASCADE"),
        nullable=False,
    )
    content_id: Mapped[uuid.UUID | None] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("contents.id", ondelete="SET NULL"),
        nullable=True,
    )
    query_id: Mapped[uuid.UUID | None] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("queries.id", ondelete="SET NULL"),
        nullable=True,
    )
    task_type: Mapped[str] = mapped_column(String(50), nullable=False)
    status: Mapped[str] = mapped_column(
        String(20), server_default="pending", nullable=False,
    )
    baseline_data: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    current_data: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    change_report: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    interval_hours: Mapped[int] = mapped_column(
        Integer, server_default="24", nullable=False,
    )
    last_checked_at: Mapped[datetime | None] = mapped_column(nullable=True)
    next_check_at: Mapped[datetime | None] = mapped_column(nullable=True)
    created_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    brand: Mapped["Brand"] = relationship("Brand")
    user: Mapped["User"] = relationship("User")

    __table_args__ = (
        Index("idx_monitoring_records_user_id", "user_id"),
        Index("idx_monitoring_records_brand_id", "brand_id"),
        Index("idx_monitoring_records_status", "status"),
        Index("idx_monitoring_records_next_check_at", "next_check_at"),
    )


class ContentBaseline(Base):
    __tablename__ = "content_baselines"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    brand_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("brands.id", ondelete="CASCADE"),
        nullable=False,
    )
    content_id: Mapped[uuid.UUID | None] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("contents.id", ondelete="SET NULL"),
        nullable=True,
    )
    citation_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    positive_ratio: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
    avg_rank: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
    platform_data: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    recorded_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        nullable=False,
    )
    created_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        nullable=False,
    )

    brand: Mapped["Brand"] = relationship("Brand")

    __table_args__ = (
        Index("idx_content_baselines_brand_id", "brand_id"),
        Index("idx_content_baselines_content_id", "content_id"),
    )
