import uuid
from datetime import datetime

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

from app.database import Base, JSONType


class BrandKnowledge(Base):
    __tablename__ = "brand_knowledge"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    organization_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("organizations.id", ondelete="CASCADE"),
        nullable=False,
    )
    category: Mapped[str] = mapped_column(String(50), nullable=False)
    title: Mapped[str] = mapped_column(String(200), nullable=False)
    content: Mapped[str] = mapped_column(Text, nullable=False)
    extra_metadata: Mapped[dict | None] = mapped_column("metadata", JSONType, nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, server_default="true", nullable=False)
    created_by: Mapped[uuid.UUID | None] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("users.id", ondelete="SET NULL"),
        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,
    )

    # Relationships
    organization: Mapped["Organization"] = relationship(
        "Organization", back_populates="brand_knowledge_entries"
    )
    creator: Mapped["User | None"] = relationship(
        "User", foreign_keys=[created_by]
    )

    __table_args__ = (
        Index("idx_brand_knowledge_organization_id", "organization_id"),
        Index("idx_brand_knowledge_category", "category"),
        Index("idx_brand_knowledge_is_active", "is_active"),
    )


class Keyword(Base):
    __tablename__ = "keywords"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    organization_id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("organizations.id", ondelete="CASCADE"),
        nullable=False,
    )
    project_id: Mapped[uuid.UUID | None] = mapped_column(
        Uuid(as_uuid=True),
        ForeignKey("lifecycle_projects.id", ondelete="SET NULL"),
        nullable=True,
    )
    keyword: Mapped[str] = mapped_column(String(200), nullable=False)
    category: Mapped[str] = mapped_column(String(50), nullable=False)
    priority: Mapped[int] = mapped_column(Integer, server_default="0", nullable=False)
    search_volume: Mapped[int | None] = mapped_column(Integer, nullable=True)
    competition_level: Mapped[str | None] = mapped_column(String(20), nullable=True)
    status: Mapped[str] = mapped_column(String(20), server_default="active", nullable=False)
    created_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        nullable=False,
    )

    # Relationships
    organization: Mapped["Organization"] = relationship(
        "Organization", back_populates="keywords"
    )
    project: Mapped["LifecycleProject | None"] = relationship(
        "LifecycleProject"
    )

    __table_args__ = (
        Index("idx_keywords_organization_id", "organization_id"),
        Index("idx_keywords_project_id", "project_id"),
        Index("idx_keywords_category", "category"),
        Index("idx_keywords_status", "status"),
        Index("idx_keywords_keyword", "keyword"),
    )
