import uuid
from datetime import datetime

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

from app.database import Base, JSONType


class PlatformRule(Base):
    __tablename__ = "platform_rules"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid(as_uuid=True),
        primary_key=True,
        default=uuid.uuid4,
    )
    platform: Mapped[str] = mapped_column(String(50), nullable=False)
    rule_category: Mapped[str] = mapped_column(String(50), nullable=False)
    rule_name: Mapped[str] = mapped_column(String(200), nullable=False)
    description: Mapped[str | None] = mapped_column(Text, nullable=True)
    check_criteria: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
    severity: Mapped[str] = mapped_column(String(20), nullable=False)
    is_active: Mapped[bool] = mapped_column(Boolean, server_default="true", nullable=False)
    updated_at: Mapped[datetime] = mapped_column(
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    __table_args__ = (
        Index("idx_platform_rules_platform", "platform"),
        Index("idx_platform_rules_rule_category", "rule_category"),
        Index("idx_platform_rules_is_active", "is_active"),
        Index("idx_platform_rules_platform_category", "platform", "rule_category"),
    )
