# pylint: disable=invalid-name
"""Overvotes

Revision ID: fa342e75506b
Revises: b0b160c2f187
Create Date: 2020-07-06 23:54:03.420984+00:00

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "fa342e75506b"
down_revision = "b0b160c2f187"
branch_labels = None
depends_on = None


def upgrade():
    # Create the new many-to-many relationship table (and a unique constraint
    # it depends on)
    op.create_unique_constraint(
        op.f("contest_choice_id_contest_id_key"), "contest_choice", ["id", "contest_id"]
    )
    op.create_table(
        "ballot_interpretation_contest_choice",
        sa.Column("ballot_id", sa.String(length=200), nullable=False),
        sa.Column("contest_id", sa.String(length=200), nullable=False),
        sa.Column("contest_choice_id", sa.String(length=200), nullable=False),
        sa.ForeignKeyConstraint(
            ["ballot_id", "contest_id"],
            ["ballot_interpretation.ballot_id", "ballot_interpretation.contest_id"],
            name=op.f("ballot_interpretation_contest_choice_ballot_id_contest_id_fkey"),
        ),
        sa.ForeignKeyConstraint(
            ["contest_choice_id", "contest_id"],
            ["contest_choice.id", "contest_choice.contest_id"],
            name=op.f(
                "ballot_interpretation_contest_choice_contest_choice_id_contest_id_fkey"
            ),
        ),
    )

    # Add is_overvote field, fill in a default value of false, then make it
    # required
    op.add_column(
        "ballot_interpretation",
        sa.Column("is_overvote", sa.Boolean()),
    )
    op.execute("UPDATE ballot_interpretation SET is_overvote = false")
    op.alter_column("ballot_interpretation", "is_overvote", nullable=False)

    # Copy over the existing contest choice selections to the new many-to-many
    # relationship table.
    op.execute(
        """
        INSERT INTO ballot_interpretation_contest_choice
            (ballot_id, contest_id, contest_choice_id)
        SELECT ballot_id, contest_id, contest_choice_id
        FROM ballot_interpretation
        WHERE contest_choice_id IS NOT NULL
        """
    )

    # Drop the old one-to-many relationship
    op.drop_constraint(
        "ballot_interpretation_contest_choice_id_fkey",
        "ballot_interpretation",
        type_="foreignkey",
    )
    op.drop_column("ballot_interpretation", "contest_choice_id")


def downgrade():
    pass  # pragma: no cover
    # Auto-generated by Alembic, not tested
    # op.add_column(
    #     "ballot_interpretation",
    #     sa.Column(
    #         "contest_choice_id",
    #         sa.VARCHAR(length=200),
    #         autoincrement=False,
    #         nullable=True,
    #     ),
    # )
    # op.create_foreign_key(
    #     "ballot_interpretation_contest_choice_id_fkey",
    #     "ballot_interpretation",
    #     "contest_choice",
    #     ["contest_choice_id"],
    #     ["id"],
    #     ondelete="CASCADE",
    # )
    # op.drop_column("ballot_interpretation", "is_overvote")
    # op.drop_table("ballot_interpretation_contest_choice")
    # op.drop_constraint(
    #     op.f("contest_choice_id_contest_id_key"), "contest_choice", type_="unique"
    # )
