"""Add activity_events table (Activity Feed).

A generic per-user activity history: free-text event_type + a shared
(related_entity_type, related_entity_id) pointer, so future features publish
events without a migration. Indexed for "this user, newest first" and unread.

Revision ID: c5e7a9b1d3f0
Revises: b4d6f8a0c2e3
Create Date: 2026-07-04

"""
from alembic import op
import sqlalchemy as sa


revision = "c5e7a9b1d3f0"
down_revision = "b4d6f8a0c2e3"
branch_labels = None
depends_on = None

_BIGINT = sa.BigInteger().with_variant(sa.Integer(), "sqlite")


def upgrade():
    op.create_table(
        "activity_events",
        sa.Column("user_id", _BIGINT, nullable=False),
        sa.Column("event_type", sa.String(length=50), nullable=False),
        sa.Column("title", sa.String(length=150), nullable=False),
        sa.Column("message", sa.String(length=500), nullable=True),
        sa.Column(
            "related_entity_type",
            sa.Enum("listing", "offer", "conversation", "message", "meetup", "report",
                    "advertisement", "subscription", "system", name="activity_related_entity_type"),
            nullable=True,
        ),
        sa.Column("related_entity_id", _BIGINT, nullable=True),
        sa.Column("icon", sa.String(length=60), nullable=True),
        sa.Column("is_read", sa.Boolean(), server_default="0", nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("id", _BIGINT, autoincrement=True, nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"], name="fk_activity_events_user_id"),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("activity_events", schema=None) as batch_op:
        batch_op.create_index("ix_activity_events_user_created", ["user_id", "created_at"], unique=False)
        batch_op.create_index("ix_activity_events_user_unread", ["user_id", "is_read"], unique=False)
        batch_op.create_index("ix_activity_events_event_type", ["event_type"], unique=False)


def downgrade():
    op.drop_table("activity_events")
