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

"""Added proper delete actions

Revision ID: 2ed6823c69b2
Revises: 230ac26db527
Create Date: 2023-07-12 11:00:37.785183

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


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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # op.drop_constraint('fk_api_keys_users_id_user', 'api_keys', type_='foreignkey')
    op.execute("ALTER TABLE api_keys DROP CONSTRAINT IF EXISTS fk_api_keys_users_id_user")
    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.execute("ALTER TABLE fido_credentials DROP CONSTRAINT IF EXISTS fk_fido_credentials_users_id_user")
    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.execute("ALTER TABLE game_results DROP CONSTRAINT IF EXISTS fk_game_results_users_id_user")
    # op.drop_constraint('fk_game_results_quiz_id_quiz', 'game_results', type_='foreignkey')
    op.execute("ALTER TABLE game_results DROP CONSTRAINT IF EXISTS fk_game_results_quiz_id_quiz")
    op.create_foreign_key("fk_game_results_quiz_id_quiz", "game_results", "quiz", ["quiz"], ["id"], ondelete="CASCADE")
    op.create_foreign_key(
        "fk_game_results_users_id_user", "game_results", "users", ["user"], ["id"], ondelete="CASCADE"
    )
    # op.drop_constraint('fk_quiz_users_id_user_id', 'quiz', type_='foreignkey')
    op.execute("ALTER TABLE quiz DROP CONSTRAINT IF EXISTS fk_quiz_users_id_user_id")
    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.execute("ALTER TABLE quiztivitys DROP CONSTRAINT IF EXISTS fk_quiztivitys_users_id_user")
    op.create_foreign_key("fk_quiztivitys_users_id_user", "quiztivitys", "users", ["user"], ["id"], ondelete="CASCADE")
    # op.drop_constraint('fk_quiztivityshares_quiztivitys_id_quiztivity', 'quiztivityshares', type_='foreignkey')
    op.execute("ALTER TABLE quiztivityshares DROP CONSTRAINT IF EXISTS fk_quiztivityshares_quiztivitys_id_quiztivity")
    # op.drop_constraint('fk_quiztivityshares_users_id_user', 'quiztivityshares', type_='foreignkey')
    op.execute("ALTER TABLE quiztivityshares DROP CONSTRAINT IF EXISTS fk_quiztivityshares_users_id_user")
    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.execute("ALTER TABLE storage_items DROP CONSTRAINT IF EXISTS fk_storage_items_users_id_user")
    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.execute("ALTER TABLE user_sessions DROP CONSTRAINT IF EXISTS fk_user_sessions_users_id_user")
    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_users_id_user", "quiztivityshares", "users", ["user"], ["id"])
    op.create_foreign_key(
        "fk_quiztivityshares_quiztivitys_id_quiztivity", "quiztivityshares", "quiztivitys", ["quiztivity"], ["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_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_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"])
    # ### end Alembic commands ###
