"""initial_migration

Revision ID: 0c92c803d35b
Revises:
Create Date: 2026-02-27 00:00:24.724563

"""

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0c92c803d35b"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "xarizmi_exchange",
        sa.Column("name", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("name"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "xarizmi_symbol",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("base_currency", sa.String(), nullable=False),
        sa.Column("quote_currency", sa.String(), nullable=False),
        sa.Column("fee_currency", sa.String(), nullable=True),
        sa.Column(
            "name",
            sa.String(),
            sa.Computed(
                "base_currency || '-' || quote_currency",
            ),
            nullable=False,
        ),
        sa.Column("exchange_name", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["exchange_name"],
            ["xarizmi_exchange.name"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "base_currency",
            "quote_currency",
            "exchange_name",
            name="uix_symbol_name_exchange",
        ),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "xarizmi_candlestick",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("symbol_id", sa.Integer(), nullable=False),
        sa.Column("close", sa.Float(), nullable=False),
        sa.Column("open", sa.Float(), nullable=False),
        sa.Column("high", sa.Float(), nullable=False),
        sa.Column("low", sa.Float(), nullable=False),
        sa.Column("volume", sa.Float(), nullable=False),
        sa.Column("amount", sa.Float(), nullable=True),
        sa.Column(
            "interval_type",
            sa.Enum(
                "MIN_1",
                "MIN_3",
                "MIN_5",
                "MIN_15",
                "MIN_30",
                "HOUR_1",
                "HOUR_2",
                "HOUR_4",
                "HOUR_6",
                "HOUR_8",
                "HOUR_12",
                "DAY_1",
                "DAY_7",
                "DAY_14",
                "WEEK_1",
                "MONTH_1",
                name="interval_type_enum",
            ),
            nullable=False,
        ),
        sa.Column("interval", sa.BigInteger(), nullable=True),
        sa.Column("datetime", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["symbol_id"],
            ["xarizmi_symbol.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "symbol_id",
            "interval_type",
            "interval",
            name="uix_symbol_interval",
        ),
    )
    op.create_table(
        "xarizmi_order",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("symbol_id", sa.Integer(), nullable=False),
        sa.Column("order_id", sa.String(), nullable=False),
        sa.Column("amount", sa.Float(), nullable=False),
        sa.Column("price", sa.Float(), nullable=False),
        sa.Column(
            "side", sa.Enum("BUY", "SELL", name="side_enum"), nullable=False
        ),
        sa.Column(
            "status",
            sa.Enum(
                "ACTIVE",
                "DONE",
                "PARTIALLY_FILLED",
                "CANCELLED",
                name="order_status_enum",
            ),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["symbol_id"],
            ["xarizmi_symbol.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "symbol_id", "order_id", name="uix_symbol_id_order_id"
        ),
    )
    op.create_table(
        "xarizmi_portfolio_item",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("symbol_id", sa.Integer(), nullable=False),
        sa.Column("market_value", sa.Float(), nullable=False),
        sa.Column("quantity", sa.Float(), nullable=False),
        sa.Column("datetime", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["symbol_id"],
            ["xarizmi_symbol.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "symbol_id", "datetime", name="uix_symbol_datetime"
        ),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("xarizmi_portfolio_item")
    op.drop_table("xarizmi_order")
    op.drop_table("xarizmi_candlestick")
    op.drop_table("xarizmi_symbol")
    op.drop_table("xarizmi_exchange")
    # ### end Alembic commands ###
