"""Adds data models for incident cost and incident cost type

Revision ID: c86be389dc1a
Revises: 0694cd18ea4f
Create Date: 2021-03-01 12:52:05.029318

"""
from alembic import op

from sqlalchemy.dialects import postgresql
import sqlalchemy as sa
import sqlalchemy_utils

# revision identifiers, used by Alembic.
revision = "c86be389dc1a"
down_revision = "0694cd18ea4f"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "incident_cost_type",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(), nullable=True),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("details", sqlalchemy_utils.types.json.JSONType(), nullable=True),
        sa.Column("default", sa.Boolean(), nullable=True),
        sa.Column("search_vector", sqlalchemy_utils.types.ts_vector.TSVectorType(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        "ix_incident_cost_type_search_vector",
        "incident_cost_type",
        ["search_vector"],
        unique=False,
        postgresql_using="gin",
    )
    op.create_table(
        "incident_cost",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("amount", sa.Numeric(precision=10, scale=2), nullable=True),
        sa.Column("incident_cost_type_id", sa.Integer(), nullable=True),
        sa.Column("incident_id", sa.Integer(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["incident_cost_type_id"],
            ["incident_cost_type.id"],
        ),
        sa.ForeignKeyConstraint(["incident_id"], ["incident.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.drop_column("incident", "cost")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "incident",
        sa.Column(
            "cost", postgresql.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=True
        ),
    )
    op.drop_index("ix_incident_cost_type_search_vector", table_name="incident_cost_type")
    op.drop_table("incident_cost_type")
    op.drop_table("incident_cost")
    # ### end Alembic commands ###
