# SPDX-FileCopyrightText: 2023 Marlon W (Mawoka)
#
# SPDX-License-Identifier: MPL-2.0

"""Added Ratings, Plays and Views to Quiz

Revision ID: 230ac26db527
Revises: 32649a1ffcf2
Create Date: 2023-06-30 19:18:02.881031

"""
from alembic import op
import sqlalchemy as sa
import ormar


# revision identifiers, used by Alembic.
revision = "230ac26db527"
down_revision = "32649a1ffcf2"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "rating",
        sa.Column("id", ormar.fields.sqlalchemy_uuid.CHAR(32), nullable=False),
        sa.Column("user", ormar.fields.sqlalchemy_uuid.CHAR(32), nullable=True),
        sa.Column("positive", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False),
        sa.Column("quiz", ormar.fields.sqlalchemy_uuid.CHAR(32), nullable=True),
        sa.ForeignKeyConstraint(["quiz"], ["quiz.id"], name="fk_rating_quiz_id_quiz"),
        sa.ForeignKeyConstraint(["user"], ["users.id"], name="fk_rating_users_id_user"),
        sa.PrimaryKeyConstraint("id"),
    )
    # op.drop_constraint('fk_api_keys_users_id_user', 'api_keys', type_='foreignkey')
    # op.create_foreign_key('fk_api_keys_users_id_user', 'api_keys', 'users', ['user'], ['id'], ondelete='CASCADE')
    # op.drop_constraint('fk_fido_credentials_users_id_user', 'fido_credentials', type_='foreignkey')
    # op.create_foreign_key('fk_fido_credentials_users_id_user', 'fido_credentials', 'users', ['user'], ['id'], ondelete='CASCADE')
    # op.drop_constraint('fk_game_results_users_id_user', 'game_results', type_='foreignkey')
    # op.drop_constraint('fk_game_results_quiz_id_quiz', 'game_results', type_='foreignkey')
    # op.create_foreign_key('fk_game_results_users_id_user', 'game_results', 'users', ['user'], ['id'], ondelete='CASCADE')
    # op.create_foreign_key('fk_game_results_quiz_id_quiz', 'game_results', 'quiz', ['quiz'], ['id'], ondelete='CASCADE')
    op.add_column("quiz", sa.Column("likes", sa.Integer(), server_default="0", nullable=False))
    op.add_column("quiz", sa.Column("dislikes", sa.Integer(), server_default="0", nullable=False))
    op.add_column("quiz", sa.Column("plays", sa.Integer(), server_default="0", nullable=False))
    op.add_column("quiz", sa.Column("views", sa.Integer(), server_default="0", nullable=False))
    # op.drop_constraint('fk_quiz_users_id_user_id', 'quiz', type_='foreignkey')
    # op.create_foreign_key('fk_quiz_users_id_user_id', 'quiz', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    # op.drop_constraint('fk_quiztivitys_users_id_user', 'quiztivitys', type_='foreignkey')
    # op.create_foreign_key('fk_quiztivitys_users_id_user', 'quiztivitys', 'users', ['user'], ['id'], ondelete='CASCADE')
    # op.drop_constraint('fk_quiztivityshares_users_id_user', 'quiztivityshares', type_='foreignkey')
    # op.drop_constraint('fk_quiztivityshares_quiztivitys_id_quiztivity', 'quiztivityshares', type_='foreignkey')
    # op.create_foreign_key('fk_quiztivityshares_users_id_user', 'quiztivityshares', 'users', ['user'], ['id'], ondelete='CASCADE')
    # op.create_foreign_key('fk_quiztivityshares_quiztivitys_id_quiztivity', 'quiztivityshares', 'quiztivitys', ['quiztivity'], ['id'], ondelete='CASCADE')
    # op.drop_constraint('fk_storage_items_users_id_user', 'storage_items', type_='foreignkey')
    # op.create_foreign_key('fk_storage_items_users_id_user', 'storage_items', 'users', ['user'], ['id'], ondelete='SET NULL')
    # op.drop_constraint('fk_user_sessions_users_id_user', 'user_sessions', type_='foreignkey')
    # op.create_foreign_key('fk_user_sessions_users_id_user', 'user_sessions', 'users', ['user'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # op.drop_constraint('fk_user_sessions_users_id_user', 'user_sessions', type_='foreignkey')
    # op.create_foreign_key('fk_user_sessions_users_id_user', 'user_sessions', 'users', ['user'], ['id'])
    # op.drop_constraint('fk_storage_items_users_id_user', 'storage_items', type_='foreignkey')
    # op.create_foreign_key('fk_storage_items_users_id_user', 'storage_items', 'users', ['user'], ['id'])
    # op.drop_constraint('fk_quiztivityshares_quiztivitys_id_quiztivity', 'quiztivityshares', type_='foreignkey')
    # op.drop_constraint('fk_quiztivityshares_users_id_user', 'quiztivityshares', type_='foreignkey')
    # op.create_foreign_key('fk_quiztivityshares_quiztivitys_id_quiztivity', 'quiztivityshares', 'quiztivitys', ['quiztivity'], ['id'])
    # op.create_foreign_key('fk_quiztivityshares_users_id_user', 'quiztivityshares', 'users', ['user'], ['id'])
    # op.drop_constraint('fk_quiztivitys_users_id_user', 'quiztivitys', type_='foreignkey')
    # op.create_foreign_key('fk_quiztivitys_users_id_user', 'quiztivitys', 'users', ['user'], ['id'])
    # op.drop_constraint('fk_quiz_users_id_user_id', 'quiz', type_='foreignkey')
    # op.create_foreign_key('fk_quiz_users_id_user_id', 'quiz', 'users', ['user_id'], ['id'])
    op.drop_column("quiz", "views")
    op.drop_column("quiz", "plays")
    op.drop_column("quiz", "dislikes")
    op.drop_column("quiz", "likes")
    # op.drop_constraint('fk_game_results_quiz_id_quiz', 'game_results', type_='foreignkey')
    # op.drop_constraint('fk_game_results_users_id_user', 'game_results', type_='foreignkey')
    # op.create_foreign_key('fk_game_results_quiz_id_quiz', 'game_results', 'quiz', ['quiz'], ['id'])
    # op.create_foreign_key('fk_game_results_users_id_user', 'game_results', 'users', ['user'], ['id'])
    # op.drop_constraint('fk_fido_credentials_users_id_user', 'fido_credentials', type_='foreignkey')
    # op.create_foreign_key('fk_fido_credentials_users_id_user', 'fido_credentials', 'users', ['user'], ['id'])
    # op.drop_constraint('fk_api_keys_users_id_user', 'api_keys', type_='foreignkey')
    # op.create_foreign_key('fk_api_keys_users_id_user', 'api_keys', 'users', ['user'], ['id'])
    op.drop_table("rating")
    # ### end Alembic commands ###
