"""Add telemetry_events table.

Revision ID: 012_add_telemetry_events
Revises: 011_add_yield_transaction_click_id
Create Date: 2025-12-15
"""

from alembic import op
import sqlalchemy as sa


revision = "012_add_telemetry_events"
down_revision = "011_add_yield_transaction_click_id"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "telemetry_events",
        sa.Column("id", sa.Integer(), primary_key=True),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("event_name", sa.String(length=60), nullable=False),
        sa.Column("listing_id", sa.Integer(), nullable=True),
        sa.Column("inquiry_id", sa.Integer(), nullable=True),
        sa.Column("yield_domain_id", sa.Integer(), nullable=True),
        sa.Column("click_id", sa.String(length=64), nullable=True),
        sa.Column("domain", sa.String(length=255), nullable=True),
        sa.Column("source", sa.String(length=30), nullable=True),
        sa.Column("ip_hash", sa.String(length=64), nullable=True),
        sa.Column("user_agent", sa.String(length=500), nullable=True),
        sa.Column("referrer", sa.String(length=500), nullable=True),
        sa.Column("metadata_json", sa.Text(), nullable=True),
        sa.Column("is_authenticated", sa.Boolean(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
    )

    op.create_index("ix_telemetry_events_event_name", "telemetry_events", ["event_name"])
    op.create_index("ix_telemetry_events_user_id", "telemetry_events", ["user_id"])
    op.create_index("ix_telemetry_events_listing_id", "telemetry_events", ["listing_id"])
    op.create_index("ix_telemetry_events_inquiry_id", "telemetry_events", ["inquiry_id"])
    op.create_index("ix_telemetry_events_yield_domain_id", "telemetry_events", ["yield_domain_id"])
    op.create_index("ix_telemetry_events_click_id", "telemetry_events", ["click_id"])
    op.create_index("ix_telemetry_events_domain", "telemetry_events", ["domain"])
    op.create_index("ix_telemetry_events_created_at", "telemetry_events", ["created_at"])
    op.create_index("ix_telemetry_event_name_created", "telemetry_events", ["event_name", "created_at"])
    op.create_index("ix_telemetry_user_created", "telemetry_events", ["user_id", "created_at"])
    op.create_index("ix_telemetry_listing_created", "telemetry_events", ["listing_id", "created_at"])
    op.create_index("ix_telemetry_yield_created", "telemetry_events", ["yield_domain_id", "created_at"])


def downgrade() -> None:
    op.drop_index("ix_telemetry_yield_created", table_name="telemetry_events")
    op.drop_index("ix_telemetry_listing_created", table_name="telemetry_events")
    op.drop_index("ix_telemetry_user_created", table_name="telemetry_events")
    op.drop_index("ix_telemetry_event_name_created", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_created_at", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_domain", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_click_id", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_yield_domain_id", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_inquiry_id", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_listing_id", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_user_id", table_name="telemetry_events")
    op.drop_index("ix_telemetry_events_event_name", table_name="telemetry_events")
    op.drop_table("telemetry_events")

