"""add signal context snapshots

Revision ID: 016
Revises: 015
Create Date: 2026-07-02 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "016"
down_revision: Union[str, None] = "015"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "signal_context_snapshots",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("trade_setup_id", sa.Integer(), nullable=False),
        sa.Column("ticker_id", sa.Integer(), nullable=False),
        sa.Column("detected_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("strategy_version", sa.String(length=80), nullable=False),
        sa.Column("direction", sa.String(length=10), nullable=False),
        sa.Column("entry_price", sa.Float(), nullable=False),
        sa.Column("stop_loss", sa.Float(), nullable=False),
        sa.Column("target", sa.Float(), nullable=False),
        sa.Column("rr_ratio", sa.Float(), nullable=False),
        sa.Column("confidence_score", sa.Float(), nullable=True),
        sa.Column("recommended_action", sa.String(length=20), nullable=True),
        sa.Column("risk_level", sa.String(length=10), nullable=True),
        sa.Column("momentum_percentile", sa.Float(), nullable=True),
        sa.Column("score_context_json", sa.Text(), nullable=False),
        sa.Column("sentiment_context_json", sa.Text(), nullable=False),
        sa.Column("fundamental_context_json", sa.Text(), nullable=False),
        sa.ForeignKeyConstraint(["ticker_id"], ["tickers.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["trade_setup_id"], ["trade_setups.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("trade_setup_id", name="uq_signal_context_trade_setup"),
    )
    op.create_index(
        "ix_signal_context_ticker_detected",
        "signal_context_snapshots",
        ["ticker_id", "detected_at"],
    )


def downgrade() -> None:
    op.drop_index("ix_signal_context_ticker_detected", table_name="signal_context_snapshots")
    op.drop_table("signal_context_snapshots")
