"""initial migration

Revision ID: 3bfd5eb2e4ff
Revises: 
Create Date: 2020-04-13 13:57:32.167477

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


# revision identifiers, used by Alembic.
revision = '3bfd5eb2e4ff'
down_revision = None
branch_labels = None
depends_on = None


def seed_data():
    # Fill the db with dummy data
    category_table = sa.sql.table('categories',
                                  sa.sql.column('id', sa.Integer),
                                  sa.sql.column('type', sa.String)
                                  )

    item_table = sa.sql.table('items',
                              sa.sql.column('id', sa.Integer),
                              sa.sql.column('description', sa.String),
                              sa.sql.column('amount', sa.Numeric),
                              sa.sql.column('date', sa.Date),
                              sa.sql.column('category_id', sa.Integer)
                              )

    op.bulk_insert(
        category_table,
        [{
            "id": 1,
            "type": "Food"
        }, {
            "id": 2,
            "type": "Housing"
        }, {
            "id": 3,
            "type": "Salary"
        }, {
            "id": 4,
            "type": "Leisure"
        }]
    )

    op.bulk_insert(
        item_table,
        [{
            "id": 1,
            "description": "salary",
            "amount": 2800.25,
            "date": datetime.date(2020, 1, 31),
            "category_id": 3
        }, {
            "id": 2,
            "description": "Aldi",
            "amount": 16.99,
            "date": datetime.date(2020, 1, 31),
            "category_id": 1
        }, {
            "id": 3,
            "description": "Netto",
            "amount": 8.32,
            "date": datetime.date(2020, 2, 8),
            "category_id": 1
        }, {
            "id": 4,
            "description": "Sippi",
            "amount": 80,
            "date": datetime.date(2020, 2, 9),
            "category_id": 4
        }, {
            "id": 5,
            "description": "Treptower Klause",
            "amount": 16.5,
            "date": datetime.date(2020, 2, 13),
            "category_id": 4
        }]
    )

    op.get_bind().execute("ALTER SEQUENCE categories_id_seq RESTART WITH 5;")
    op.get_bind().execute("ALTER SEQUENCE items_id_seq RESTART WITH 6;")


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('categories',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('type', sa.String(length=50), nullable=True),
                    sa.PrimaryKeyConstraint('id')
                    )
    op.create_table('items',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('description', sa.String(
                        length=120), nullable=True),
                    sa.Column('amount', sa.Numeric(scale=2), nullable=False),
                    sa.Column('date', sa.Date(), nullable=False),
                    sa.Column('category_id', sa.Integer(), nullable=False),
                    sa.ForeignKeyConstraint(
                        ['category_id'], ['categories.id'], ),
                    sa.PrimaryKeyConstraint('id')
                    )
    # ### end Alembic commands ###
    seed_data()


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('items')
    op.drop_table('categories')
    # ### end Alembic commands ###
