import uuid
from datetime import datetime

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

from app.database import Base, JSONType


class SchemaSuggestion(Base):
    __tablename__ = "schema_suggestions"

    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,
    )
    schema_type: Mapped[str] = mapped_column(
        String(50), nullable=False,
    )
    target_url: Mapped[str | None] = mapped_column(
        String(500), nullable=True,
    )
    json_ld_template: Mapped[dict] = mapped_column(
        JSONType, nullable=False, default=dict,
    )
    json_ld_filled: Mapped[dict | None] = mapped_column(
        JSONType, nullable=True,
    )
    priority: Mapped[str] = mapped_column(
        String(20), nullable=False, default="medium",
    )
    status: Mapped[str] = mapped_column(
        String(20), nullable=False, default="pending",
    )
    diagnosis_dimensions: Mapped[dict | None] = mapped_column(
        JSONType, nullable=True,
    )
    implementation_difficulty: Mapped[str] = mapped_column(
        String(20), nullable=False, default="medium",
    )
    estimated_impact: Mapped[str | None] = mapped_column(
        Text, nullable=True,
    )
    validation_errors: Mapped[dict | None] = mapped_column(
        JSONType, nullable=True,
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        nullable=False,
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    brand: Mapped["Brand"] = relationship("Brand", back_populates="schema_suggestions")

    __table_args__ = (
        Index("idx_schema_suggestions_brand_id", "brand_id"),
        Index("idx_schema_suggestions_status", "status"),
        Index("idx_schema_suggestions_schema_type", "schema_type"),
        Index("idx_schema_suggestions_brand_status", "brand_id", "status"),
    )


from app.models.brand import Brand  # noqa: E402, F401
