"""Create Quantfury orders model

Revision ID: 53cbb66861d9
Revises: e84250567e04
Create Date: 2021-07-31 11:10:57.991455

"""
from alembic import op
import sqlalchemy as sa
import enum


# revision identifiers, used by Alembic.
revision = '53cbb66861d9'
down_revision = 'e84250567e04'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    #op.execute("""
    #CREATE TYPE quantfuryorderstatus AS ENUM (
    #'LONG', 'SHORT');""")

    class QuantfuryOrderStatus(enum.Enum):
        hold = 'hold'
        open = 'open'
        close = 'close'
        cancel = 'cancel'
        target = 'target'

    class QuantfuryOrderType(enum.Enum):
        stop_order = 'STOP_ORDER'
        target_order = 'TARGET_ORDER'
        limit_order = 'LIMIT_ORDER'

    class QuantfuryPositionType(enum.Enum):
        long = 'long'
        short = 'short'

    op.create_table(
        'quantfury_positions',
        sa.Column('quantfury_order_id', sa.Integer, primary_key=True, nullable=False),
        sa.Column('trade_id', sa.Integer, nullable=False),
        sa.Column('limit_id', sa.String, nullable=False, unique=True),
        sa.Column('symbol', sa.String, nullable=False),

        sa.Column('limit_price', sa.Numeric, nullable=True),
        sa.Column('limit_quantity', sa.Numeric, nullable=False),
        sa.Column('limit_amount_instrument', sa.Numeric, nullable=False),
        sa.Column('limit_amount_system', sa.Numeric, nullable=False),
        sa.Column('limit_stop_order', sa.Numeric, nullable=False),
        sa.Column('limit_create_date', sa.DateTime, nullable=True),
        sa.Column('limit_execution_type', sa.Numeric, nullable=True),

        sa.Column('api_position_id', sa.String, nullable=True, unique=True),
        sa.Column('position_open_price', sa.Numeric, nullable=True),
        sa.Column('position_quantity', sa.Numeric, nullable=True),
        sa.Column('position_amount_instrument', sa.Numeric, nullable=True),
        sa.Column('position_amount_system', sa.Numeric, nullable=True),
        sa.Column('position_open_date', sa.DateTime, nullable=True),
        sa.Column('position_order', sa.Integer, nullable=True),
        sa.Column('position_session_currency', sa.String, nullable=True),
        sa.Column('position_session_id', sa.String, nullable=True),
        sa.Column('position_spread_adjustment_end_date', sa.DateTime, nullable=True),
        sa.Column('position_type', sa.Enum(QuantfuryPositionType), nullable=True),
        sa.Column('status', sa.Enum(QuantfuryOrderStatus), nullable=False, default=False),
        sa.Column('profit_and_loss_instrument', sa.Numeric, nullable=True),
        sa.Column('profit_and_loss_system', sa.Numeric, nullable=True),
        sa.Column('profit_and_loss_account', sa.Numeric, nullable=True),
        sa.Column('close_price', sa.Numeric, nullable=True),
        sa.Column('close_date', sa.DateTime, nullable=True)
    )

    op.create_foreign_key(
        constraint_name='quantfury_positions_trade_id_fkey',
        source_table='quantfury_positions',
        referent_table='trades',
        local_cols=['trade_id'],
        remote_cols=['trade_id'])

    op.add_column('symbol_metadata', sa.Column('currency_pair', sa.String(), nullable=True))
    
    op.create_table(
        'quantfury_orders',

        sa.Column('quantfury_order_id', sa.Integer, primary_key=True, nullable=False),
        sa.Column('api_order_id', sa.String, nullable=False),
        sa.Column('api_position_id', sa.String, nullable=False),

        sa.Column('order_type', sa.Enum(QuantfuryOrderType), nullable=False),
        sa.Column('price', sa.Numeric, nullable=True),
        sa.Column('quantity', sa.Numeric, nullable=False),
        sa.Column('amount_instrument', sa.Numeric, nullable=False),
        sa.Column('amount_system', sa.Numeric, nullable=False)
    )


    op.create_foreign_key(
        constraint_name='quantfury_orders_api_position_id_fkey',
        source_table='quantfury_orders',
        referent_table='quantfury_positions',
        local_cols=['api_position_id'],
        remote_cols=['api_position_id'])

    op.add_column('accounts', sa.Column('broker_name', sa.String(), nullable=True))
    op.execute("UPDATE accounts SET broker_name = 'binance'")
    op.alter_column('accounts', 'broker_name', nullable=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('quantfury_orders')
    op.drop_table('quantfury_positions')
    op.execute("DROP TYPE quantfuryorderstatus;")
    op.execute("DROP TYPE IF EXISTS quantfuryordertype;")
    op.execute("DROP TYPE IF EXISTS quantfurypositiontype;")
    # ### end Alembic commands ###
