"""Add relationship between Accounts and Balances

Revision ID: c39e3caf08b6
Revises: b1f321f39713
Create Date: 2021-04-13 00:13:41.047697

"""
import os
from dotenv import load_dotenv
from alembic import op
import sqlalchemy as sa
from broker_clients import Accounts

# revision identifiers, used by Alembic.

revision = 'c39e3caf08b6'
down_revision = '14f9fd83af6a'
branch_labels = None
depends_on = None


load_dotenv()
cypher_key = os.getenv("CYPHER_KEY")

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    op.create_table(
        'accounts',
        sa.Column('account_id', sa.Integer(), primary_key=True),
        sa.Column('name', sa.String(), unique=True, nullable=False),
        sa.Column('api_key', sa.String(), unique=True, nullable=False),
        sa.Column('secret_key', sa.String(), nullable=False),
        sa.Column('token', sa.String(), nullable=True),
        sa.Column('created_at', sa.DateTime(), default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(), default=sa.func.now())
    )

    op.bulk_insert(Accounts.__table__,
                   [
                       {
                           'name': 'testnet',
                           'api_key': 'u32stTD0rmEOaZteUJA9gx6ll2hyMhJNYoYjvp9y8eHPpQiGSebw54IayuRwXQAg',
                           'secret_key': 'zbht3ZFM2KT5uAKZHRiI5epvlhPEcGGXPNdPEArPSvPFxEHF9LkY2lwNtUVtvttU',
                       }
                   ])
    op.add_column('balances', sa.Column('account_id', sa.Integer(), nullable=True))
    op.execute("UPDATE balances SET account_id = 1")
    op.alter_column('balances', 'account_id', nullable=False)
    op.create_foreign_key(
        constraint_name='balances_account_id_fkey',
        source_table='balances',
        referent_table='accounts',
        local_cols=['account_id'],
        remote_cols=['account_id'])

    op.create_table(
        'orders',
        sa.Column('order_id', sa.Integer, primary_key=True, nullable=False),
        sa.Column('symbol', sa.String, nullable=False),
        sa.Column('order_list_id', sa.Integer, nullable=False),
        sa.Column('client_order_id', sa.String, nullable=False),
        sa.Column('transact_time', sa.DateTime, nullable=False),
        sa.Column('price', sa.Numeric, nullable=True),
        sa.Column('original_quantity', sa.Numeric, nullable=False),  # quantity requested
        sa.Column('executed_quantity', sa.Numeric, nullable=False),  # quantity filled
        sa.Column('cumulative_quote_quantity', sa.Numeric, nullable=False),

        sa.Column('status', sa.String, nullable=False),
        sa.Column('time_in_force', sa.String, nullable=False),
        sa.Column('type', sa.String, nullable=False),
        sa.Column('side', sa.String, nullable=False),

        sa.Column('stop_price', sa.Numeric, nullable=True),
        sa.Column('iceberg_quantity', sa.Numeric, nullable=True),  # For hiding the actual order quantity on big orders
        sa.Column('time', sa.DateTime, nullable=True),
        sa.Column('update_time', sa.DateTime, nullable=True),
        sa.Column('is_working', sa.Boolean, nullable=True),
        sa.Column('original_quote_order_quantity', sa.Numeric, nullable=True),  # quote quantity requested
    )

    op.create_table(
        'fills',
        sa.Column('fill_id', sa.Integer, primary_key=True, autoincrement=True),
        sa.Column('order_id', sa.Integer, nullable=False),
        sa.Column('price', sa.Numeric, nullable=False),
        sa.Column('quantity', sa.Numeric, nullable=False),
        sa.Column('commission', sa.Numeric, nullable=False),
        sa.Column('commission_asset', sa.String, nullable=False),
        sa.Column('trade_id', sa.Integer, nullable=False),
    )

    op.create_foreign_key(
        constraint_name='fills_order_id_fkey',
        source_table='fills',
        referent_table='orders',
        local_cols=['order_id'],
        remote_cols=['order_id'])

    op.create_table(
        'symbol_metadata',
        sa.Column('symbol_id', sa.Integer, primary_key=True, autoincrement=True),
        sa.Column('symbol', sa.String, nullable=False, unique=True),
        sa.Column('minimum_quantity_lot_size', sa.String, nullable=False),
        sa.Column('minimum_price_filter', sa.String, nullable=False),
        sa.Column('minimum_notional', sa.String, nullable=False)
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(constraint_name='fills_order_id_fkey', table_name='fills', type_='foreignkey')
    op.drop_table('fills')
    op.drop_table('orders')
    op.drop_constraint(constraint_name='balances_account_id_fkey', table_name='balances', type_='foreignkey')
    op.drop_column('balances', 'account_id')
    op.drop_table('accounts')


    op.drop_table('symbol_metadata')
    # ### end Alembic commands ###
