import uuid
from datetime import datetime

from sqlalchemy import String, Float, Integer, ForeignKey, Index, func
from sqlalchemy import Uuid
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import TypeDecorator, JSON

from app.database import Base


class JSONType(TypeDecorator):
    impl = JSON
    cache_ok = True

    def load_dialect_impl(self, dialect):
        if dialect.name == "postgresql":
            return dialect.type_descriptor(JSONB())
        return dialect.type_descriptor(JSON())


class CompetitorInsight(Base):
    __tablename__ = "competitor_insights"

    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,
    )
    competitor_name: Mapped[str] = mapped_column(String(100), nullable=False)
    analysis_type: Mapped[str] = mapped_column(
        String(50), nullable=False,
    )
    insight_data: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    citation_count_brand: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    citation_count_competitor: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    sentiment_brand: Mapped[float | None] = mapped_column(Float, nullable=True)
    sentiment_competitor: Mapped[float | None] = mapped_column(Float, nullable=True)
    platform_breakdown: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    gap_analysis: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    opportunity_areas: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    recommendations: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    confidence: Mapped[str] = mapped_column(String(20), default="medium", nullable=False)
    period_days: Mapped[int] = mapped_column(Integer, default=30, nullable=False)
    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,
    )

    __table_args__ = (
        Index("idx_competitor_insights_brand_id", "brand_id"),
        Index("idx_competitor_insights_analysis_type", "analysis_type"),
    )
