#
# This file is part of Invenio.
# Copyright (C) 2024 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Create Preservation Info tables."""

import sqlalchemy as sa
import sqlalchemy_utils
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "2e6fbe6998cc"
down_revision = "7983d81b23cb"
branch_labels = ()
# Depends on invenio-webhooks table creation for FK reference
depends_on = "a095bd179f5c"


def upgrade():
    """Upgrade database."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "preservation_info",
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("id", sqlalchemy_utils.types.uuid.UUIDType(), nullable=False),
        sa.Column(
            "object_uuid", sqlalchemy_utils.types.uuid.UUIDType(), nullable=False
        ),
        sa.Column("revision_id", sa.Integer(), nullable=True),
        sa.Column("status", sa.CHAR(length=1), nullable=False),
        sa.Column("harvest_timestamp", sa.DateTime(), nullable=True),
        sa.Column("archive_timestamp", sa.DateTime(), nullable=True),
        sa.Column("uri", sa.String(length=255), nullable=True),
        sa.Column("path", sa.String(length=255), nullable=True),
        sa.Column("event_id", sqlalchemy_utils.types.uuid.UUIDType(), nullable=True),
        sa.Column(
            "description",
            sa.JSON()
            .with_variant(sqlalchemy_utils.types.json.JSONType(), "mysql")
            .with_variant(
                postgresql.JSONB(none_as_null=True, astext_type=sa.Text()), "postgresql"
            )
            .with_variant(sqlalchemy_utils.types.json.JSONType(), "sqlite"),
            nullable=True,
        ),
        sa.ForeignKeyConstraint(
            ["event_id"],
            ["webhooks_events.id"],
            name=op.f("fk_preservation_info_event_id_webhooks_events"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_preservation_info")),
    )
    op.create_index(
        op.f("ix_preservation_info_archive_timestamp"),
        "preservation_info",
        ["archive_timestamp"],
        unique=False,
    )
    op.create_index(
        op.f("ix_preservation_info_object_uuid"),
        "preservation_info",
        ["object_uuid"],
        unique=False,
    )
    op.create_index(
        op.f("ix_preservation_info_revision_id"),
        "preservation_info",
        ["revision_id"],
        unique=False,
    )
    op.drop_index("ix_uq_partial_files_object_is_head", table_name="files_object")
    op.alter_column(
        "files_objecttags",
        "key",
        existing_type=sa.TEXT(),
        type_=sa.String(length=255),
        existing_nullable=False,
    )
    op.alter_column(
        "records_metadata_version",
        "json",
        existing_type=postgresql.JSON(astext_type=sa.Text()),
        type_=sa.JSON()
        .with_variant(sqlalchemy_utils.types.json.JSONType(), "mysql")
        .with_variant(
            postgresql.JSONB(none_as_null=True, astext_type=sa.Text()), "postgresql"
        )
        .with_variant(sqlalchemy_utils.types.json.JSONType(), "sqlite"),
        existing_nullable=True,
        autoincrement=False,
    )
    # ### end Alembic commands ###


def downgrade():
    """Downgrade database."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column(
        "records_metadata_version",
        "json",
        existing_type=sa.JSON()
        .with_variant(sqlalchemy_utils.types.json.JSONType(), "mysql")
        .with_variant(
            postgresql.JSONB(none_as_null=True, astext_type=sa.Text()), "postgresql"
        )
        .with_variant(sqlalchemy_utils.types.json.JSONType(), "sqlite"),
        type_=postgresql.JSON(astext_type=sa.Text()),
        existing_nullable=True,
        autoincrement=False,
    )
    op.alter_column(
        "files_objecttags",
        "key",
        existing_type=sa.String(length=255),
        type_=sa.TEXT(),
        existing_nullable=False,
    )
    op.create_index(
        "ix_uq_partial_files_object_is_head",
        "files_object",
        ["bucket_id", "key"],
        unique=False,
    )
    op.drop_index(
        op.f("ix_preservation_info_revision_id"), table_name="preservation_info"
    )
    op.drop_index(
        op.f("ix_preservation_info_object_uuid"), table_name="preservation_info"
    )
    op.drop_index(
        op.f("ix_preservation_info_archive_timestamp"), table_name="preservation_info"
    )
    op.drop_table("preservation_info")
    # ### end Alembic commands ###
