"""init

Revision ID: ffb9404a7bbf
Revises: 
Create Date: 2023-02-15 17:57:58.221529

"""  # noqa: W291
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    from hitfactorpy_sqlalchemy.orm.models import (
        SAEnumMatchLevel,
        SAEnumClassification,
        SAEnumDivision,
        SAEnumPowerFactor,
        SAEnumScoring,
    )

    op.create_table(
        "match_report",
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=False),
        sa.Column("date", sa.Date(), nullable=True),
        sa.Column("match_level", SAEnumMatchLevel, nullable=True),
        sa.Column(
            "report_hash",
            postgresql.UUID(as_uuid=True),
            nullable=True,
            comment="This represents an md5 hexdigest, stored as a UUID. Used to identify potential duplicate report imports.",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("report_hash"),
    )
    op.create_table(
        "match_report_competitor_version",
        sa.Column("created", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column("updated", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("match_id", postgresql.UUID(as_uuid=True), autoincrement=False, nullable=False),
        sa.Column("member_number", sa.Unicode(length=64), autoincrement=False, nullable=True),
        sa.Column("first_name", sa.Unicode(length=64), autoincrement=False, nullable=True),
        sa.Column("last_name", sa.Unicode(length=64), autoincrement=False, nullable=True),
        sa.Column("division", SAEnumDivision, autoincrement=False, nullable=True),
        sa.Column("classification", SAEnumClassification, autoincrement=False, nullable=True),
        sa.Column("power_factor", SAEnumPowerFactor, autoincrement=False, nullable=True),
        sa.Column("dq", sa.Boolean(), autoincrement=False, nullable=False),
        sa.Column("reentry", sa.Boolean(), autoincrement=False, nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), autoincrement=False, nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.PrimaryKeyConstraint("id", "transaction_id"),
    )
    op.create_index(
        op.f("ix_match_report_competitor_version_end_transaction_id"),
        "match_report_competitor_version",
        ["end_transaction_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_match_report_competitor_version_operation_type"),
        "match_report_competitor_version",
        ["operation_type"],
        unique=False,
    )
    op.create_index(
        op.f("ix_match_report_competitor_version_transaction_id"),
        "match_report_competitor_version",
        ["transaction_id"],
        unique=False,
    )
    op.create_table(
        "match_report_stage_score_version",
        sa.Column("created", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column("updated", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("match_id", postgresql.UUID(as_uuid=True), autoincrement=False, nullable=False),
        sa.Column("competitor_id", postgresql.UUID(as_uuid=True), autoincrement=False, nullable=False),
        sa.Column("stage_id", postgresql.UUID(as_uuid=True), autoincrement=False, nullable=False),
        sa.Column("dq", sa.Boolean(), autoincrement=False, nullable=False),
        sa.Column("dnf", sa.Boolean(), autoincrement=False, nullable=False),
        sa.Column("a", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("b", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("c", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("d", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("m", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("npm", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("ns", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("procedural", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("late_shot", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("extra_shot", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("extra_hit", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("other_penalty", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("t1", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=False),
        sa.Column("t2", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=False),
        sa.Column("t3", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=False),
        sa.Column("t4", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=False),
        sa.Column("t5", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=False),
        sa.Column(
            "time", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=False
        ),
        sa.Column(
            "raw_points", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=True
        ),
        sa.Column(
            "penalty_points",
            sa.Numeric(precision=8, scale=2, decimal_return_scale=2),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "total_points", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), autoincrement=False, nullable=True
        ),
        sa.Column(
            "hit_factor", sa.Numeric(precision=8, scale=4, decimal_return_scale=4), autoincrement=False, nullable=True
        ),
        sa.Column(
            "stage_points", sa.Numeric(precision=8, scale=4, decimal_return_scale=4), autoincrement=False, nullable=True
        ),
        sa.Column("stage_place", sa.Integer(), autoincrement=False, nullable=True),
        sa.Column("stage_power_factor", SAEnumPowerFactor, autoincrement=False, nullable=True),
        sa.Column("transaction_id", sa.BigInteger(), autoincrement=False, nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.PrimaryKeyConstraint("id", "transaction_id"),
    )
    op.create_index(
        op.f("ix_match_report_stage_score_version_end_transaction_id"),
        "match_report_stage_score_version",
        ["end_transaction_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_match_report_stage_score_version_operation_type"),
        "match_report_stage_score_version",
        ["operation_type"],
        unique=False,
    )
    op.create_index(
        op.f("ix_match_report_stage_score_version_transaction_id"),
        "match_report_stage_score_version",
        ["transaction_id"],
        unique=False,
    )
    op.create_table(
        "match_report_stage_version",
        sa.Column("created", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column("updated", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("match_id", postgresql.UUID(as_uuid=True), autoincrement=False, nullable=False),
        sa.Column("name", sa.Unicode(length=255), autoincrement=False, nullable=True),
        sa.Column("min_rounds", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("max_points", sa.Integer(), autoincrement=False, nullable=False),
        sa.Column("classifier", sa.Boolean(), autoincrement=False, nullable=False),
        sa.Column("classifier_number", sa.Unicode(length=64), autoincrement=False, nullable=True),
        sa.Column("stage_number", sa.Integer(), autoincrement=False, nullable=True),
        sa.Column("scoring_type", SAEnumScoring, autoincrement=False, nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), autoincrement=False, nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.PrimaryKeyConstraint("id", "transaction_id"),
    )
    op.create_index(
        op.f("ix_match_report_stage_version_end_transaction_id"),
        "match_report_stage_version",
        ["end_transaction_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_match_report_stage_version_operation_type"),
        "match_report_stage_version",
        ["operation_type"],
        unique=False,
    )
    op.create_index(
        op.f("ix_match_report_stage_version_transaction_id"),
        "match_report_stage_version",
        ["transaction_id"],
        unique=False,
    )
    op.create_table(
        "match_report_version",
        sa.Column("created", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column("updated", sa.DateTime(), autoincrement=False, nullable=False),
        sa.Column(
            "id",
            postgresql.UUID(as_uuid=True),
            server_default=sa.text("gen_random_uuid()"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("name", sa.Unicode(length=255), autoincrement=False, nullable=False),
        sa.Column("date", sa.Date(), autoincrement=False, nullable=True),
        sa.Column("match_level", SAEnumMatchLevel, autoincrement=False, nullable=True),
        sa.Column(
            "report_hash",
            postgresql.UUID(as_uuid=True),
            autoincrement=False,
            nullable=True,
            comment="This represents an md5 hexdigest, stored as a UUID. Used to identify potential duplicate report imports.",
        ),
        sa.Column("transaction_id", sa.BigInteger(), autoincrement=False, nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.PrimaryKeyConstraint("id", "transaction_id"),
    )
    op.create_index(
        op.f("ix_match_report_version_end_transaction_id"), "match_report_version", ["end_transaction_id"], unique=False
    )
    op.create_index(
        op.f("ix_match_report_version_operation_type"), "match_report_version", ["operation_type"], unique=False
    )
    op.create_index(
        op.f("ix_match_report_version_transaction_id"), "match_report_version", ["transaction_id"], unique=False
    )
    op.create_table(
        "transaction",
        sa.Column("issued_at", sa.DateTime(), nullable=True),
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("remote_addr", sa.String(length=50), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "match_report_competitor",
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("match_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("member_number", sa.Unicode(length=64), nullable=True),
        sa.Column("first_name", sa.Unicode(length=64), nullable=True),
        sa.Column("last_name", sa.Unicode(length=64), nullable=True),
        sa.Column("division", SAEnumDivision, nullable=True),
        sa.Column("classification", SAEnumClassification, nullable=True),
        sa.Column("power_factor", SAEnumPowerFactor, nullable=True),
        sa.Column("dq", sa.Boolean(), nullable=False),
        sa.Column("reentry", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(["match_id"], ["match_report.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "match_report_stage",
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("match_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=True),
        sa.Column("min_rounds", sa.Integer(), nullable=False),
        sa.Column("max_points", sa.Integer(), nullable=False),
        sa.Column("classifier", sa.Boolean(), nullable=False),
        sa.Column("classifier_number", sa.Unicode(length=64), nullable=True),
        sa.Column("stage_number", sa.Integer(), nullable=True),
        sa.Column("scoring_type", SAEnumScoring, nullable=False),
        sa.ForeignKeyConstraint(["match_id"], ["match_report.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("match_id", "stage_number", name="stage_score_stage_number_unique_per_match"),
    )
    op.create_table(
        "match_report_stage_score",
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("match_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("competitor_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("stage_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("dq", sa.Boolean(), nullable=False),
        sa.Column("dnf", sa.Boolean(), nullable=False),
        sa.Column("a", sa.Integer(), nullable=False),
        sa.Column("b", sa.Integer(), nullable=False),
        sa.Column("c", sa.Integer(), nullable=False),
        sa.Column("d", sa.Integer(), nullable=False),
        sa.Column("m", sa.Integer(), nullable=False),
        sa.Column("npm", sa.Integer(), nullable=False),
        sa.Column("ns", sa.Integer(), nullable=False),
        sa.Column("procedural", sa.Integer(), nullable=False),
        sa.Column("late_shot", sa.Integer(), nullable=False),
        sa.Column("extra_shot", sa.Integer(), nullable=False),
        sa.Column("extra_hit", sa.Integer(), nullable=False),
        sa.Column("other_penalty", sa.Integer(), nullable=False),
        sa.Column("t1", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=False),
        sa.Column("t2", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=False),
        sa.Column("t3", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=False),
        sa.Column("t4", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=False),
        sa.Column("t5", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=False),
        sa.Column("time", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=False),
        sa.Column("raw_points", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=True),
        sa.Column("penalty_points", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=True),
        sa.Column("total_points", sa.Numeric(precision=8, scale=2, decimal_return_scale=2), nullable=True),
        sa.Column("hit_factor", sa.Numeric(precision=8, scale=4, decimal_return_scale=4), nullable=True),
        sa.Column("stage_points", sa.Numeric(precision=8, scale=4, decimal_return_scale=4), nullable=True),
        sa.Column("stage_place", sa.Integer(), nullable=True),
        sa.Column("stage_power_factor", SAEnumPowerFactor, nullable=True),
        sa.ForeignKeyConstraint(["competitor_id"], ["match_report_competitor.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["match_id"], ["match_report.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["stage_id"], ["match_report_stage.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    from hitfactorpy_sqlalchemy.orm.models import (
        SAEnumMatchLevel,
        SAEnumClassification,
        SAEnumDivision,
        SAEnumPowerFactor,
        SAEnumScoring,
    )

    op.drop_table("match_report_stage_score")
    op.drop_table("match_report_stage")
    op.drop_table("match_report_competitor")
    op.drop_table("transaction")
    op.drop_index(op.f("ix_match_report_version_transaction_id"), table_name="match_report_version")
    op.drop_index(op.f("ix_match_report_version_operation_type"), table_name="match_report_version")
    op.drop_index(op.f("ix_match_report_version_end_transaction_id"), table_name="match_report_version")
    op.drop_table("match_report_version")
    op.drop_index(op.f("ix_match_report_stage_version_transaction_id"), table_name="match_report_stage_version")
    op.drop_index(op.f("ix_match_report_stage_version_operation_type"), table_name="match_report_stage_version")
    op.drop_index(op.f("ix_match_report_stage_version_end_transaction_id"), table_name="match_report_stage_version")
    op.drop_table("match_report_stage_version")
    op.drop_index(
        op.f("ix_match_report_stage_score_version_transaction_id"), table_name="match_report_stage_score_version"
    )
    op.drop_index(
        op.f("ix_match_report_stage_score_version_operation_type"), table_name="match_report_stage_score_version"
    )
    op.drop_index(
        op.f("ix_match_report_stage_score_version_end_transaction_id"), table_name="match_report_stage_score_version"
    )
    op.drop_table("match_report_stage_score_version")
    op.drop_index(
        op.f("ix_match_report_competitor_version_transaction_id"), table_name="match_report_competitor_version"
    )
    op.drop_index(
        op.f("ix_match_report_competitor_version_operation_type"), table_name="match_report_competitor_version"
    )
    op.drop_index(
        op.f("ix_match_report_competitor_version_end_transaction_id"), table_name="match_report_competitor_version"
    )
    op.drop_table("match_report_competitor_version")
    op.drop_table("match_report")

    SAEnumMatchLevel.drop(op.get_bind())
    SAEnumClassification.drop(op.get_bind())
    SAEnumDivision.drop(op.get_bind())
    SAEnumPowerFactor.drop(op.get_bind())
    SAEnumScoring.drop(op.get_bind())
    # ### end Alembic commands ###
