"""Init database

Revision ID: 615f2c3df9bd
Revises:
Create Date: 2020-05-02 13:59:35.346971

"""
import sqlalchemy as sa
from sqlalchemy.sql import column, table

from alembic import context, op

# revision identifiers, used by Alembic.
revision = "615f2c3df9bd"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    schema_upgrade()
    if context.get_x_argument(as_dictionary=True).get("data"):
        data_upgrade()


def downgrade():
    if context.get_x_argument(as_dictionary=True).get("data"):
        data_downgrade()
    schema_downgrade()


def schema_upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "chore",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("period", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "task",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("chore_id", sa.Integer(), nullable=False),
        sa.Column("execution_date", sa.Date(), nullable=False),
        sa.ForeignKeyConstraint(
            ["chore_id"],
            ["chore.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def schema_downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("task")
    op.drop_table("chore")
    # ### end Alembic commands ###


def data_upgrade():
    chore_table = table(
        "chore",
        column("name", sa.String()),
        column("description", sa.String()),
        column("period", sa.Integer()),
    )
    op.bulk_insert(
        chore_table,
        [
            {
                "name": "Sanitaire",
                "description": "Nettoyer les vasques de la salle de bain et des toilettes",
                "period": 7,
            },
            {
                "name": "Sols",
                "description": "Nettoyer les sols de la cuisine et de la salle à manger",
                "period": 14,
            },
        ],
    )


def data_downgrade():
    op.execute("delete from task")
    op.execute("delete from chore")
