"""evaluation bloquee

Revision ID: cddabc3f868a
Revises: 2e4875004e12
Create Date: 2024-02-25 16:39:45.947342

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker  # added by ev


# revision identifiers, used by Alembic.
revision = "cddabc3f868a"
down_revision = "2e4875004e12"
branch_labels = None
depends_on = None

Session = sessionmaker()


def upgrade():
    # ces champs étaient nullables
    # Added by ev: remove duplicates
    bind = op.get_bind()
    session = Session(bind=bind)
    session.execute(
        sa.text(
            """UPDATE notes_evaluation SET description='' WHERE description IS NULL;"""
        )
    )
    session.execute(
        sa.text("""UPDATE notes_evaluation SET note_max=20. WHERE note_max IS NULL;""")
    )
    session.execute(
        sa.text(
            """UPDATE notes_evaluation SET coefficient=0. WHERE coefficient IS NULL;"""
        )
    )
    #
    with op.batch_alter_table("notes_evaluation", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("blocked_until", sa.DateTime(timezone=True), nullable=True)
        )
        batch_op.alter_column("description", existing_type=sa.TEXT(), nullable=False)
        batch_op.alter_column(
            "note_max", existing_type=sa.DOUBLE_PRECISION(precision=53), nullable=False
        )
        batch_op.alter_column(
            "coefficient",
            existing_type=sa.DOUBLE_PRECISION(precision=53),
            nullable=False,
        )

    with op.batch_alter_table("notes_formsemestre", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "mode_calcul_moyennes", sa.Integer(), server_default="0", nullable=False
            )
        )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("notes_formsemestre", schema=None) as batch_op:
        batch_op.drop_column("mode_calcul_moyennes")

    with op.batch_alter_table("notes_evaluation", schema=None) as batch_op:
        batch_op.alter_column(
            "coefficient",
            existing_type=sa.DOUBLE_PRECISION(precision=53),
            nullable=True,
        )
        batch_op.alter_column(
            "note_max", existing_type=sa.DOUBLE_PRECISION(precision=53), nullable=True
        )
        batch_op.alter_column("description", existing_type=sa.TEXT(), nullable=True)
        batch_op.drop_column("blocked_until")

    # ### end Alembic commands ###
