"""Initial schema migration.

Revision ID: c605f2284613
Revises:
Create Date: 2025-10-20 00:00:00.000000

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import mysql

# Revision identifiers are used by Alembic.
revision = "c605f2284613"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### Commands auto-generated by Alembic. Please adjust. ###
    op.create_table(
        "pipeline",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("origin_pipeline", sa.String(length=255), nullable=True),
        sa.Column("dashboard_url", sa.Text(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("pipeline", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_pipeline_origin_pipeline"), ["origin_pipeline"], unique=True
        )

    op.create_table(
        "storage_service",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=True),
        sa.Column("url", sa.String(length=255), nullable=True),
        sa.Column("user_name", sa.String(length=255), nullable=True),
        sa.Column("api_key", sa.String(length=255), nullable=True),
        sa.Column("download_limit", sa.Integer(), nullable=True),
        sa.Column("download_offset", sa.Integer(), nullable=True),
        sa.Column("default", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("storage_service", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_storage_service_name"), ["name"], unique=True
        )

    op.create_table(
        "agent",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("linking_type_value", sa.String(length=255), nullable=True),
        sa.Column("agent_type", sa.String(length=255), nullable=True),
        sa.Column("agent_value", sa.String(length=255), nullable=True),
        sa.Column("storage_service_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["storage_service_id"],
            ["storage_service.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("agent", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_agent_agent_type"), ["agent_type"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_agent_agent_value"), ["agent_value"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_agent_linking_type_value"),
            ["linking_type_value"],
            unique=False,
        )

    op.create_table(
        "fetch_job",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("total_packages", sa.Integer(), nullable=True),
        sa.Column("total_aips", sa.Integer(), nullable=True),
        sa.Column("total_dips", sa.Integer(), nullable=True),
        sa.Column("total_sips", sa.Integer(), nullable=True),
        sa.Column("total_replicas", sa.Integer(), nullable=True),
        sa.Column("total_deleted_aips", sa.Integer(), nullable=True),
        sa.Column("download_start", sa.DateTime(), nullable=True),
        sa.Column("download_end", sa.DateTime(), nullable=True),
        sa.Column("download_directory", sa.String(length=255), nullable=True),
        sa.Column("storage_service_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["storage_service_id"],
            ["storage_service.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "storage_location",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("current_location", sa.String(length=255), nullable=True),
        sa.Column("description", sa.String(length=255), nullable=True),
        sa.Column("storage_service_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["storage_service_id"],
            ["storage_service.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("storage_location", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_storage_location_current_location"),
            ["current_location"],
            unique=True,
        )

    op.create_table(
        "aip",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("uuid", sa.String(length=255), nullable=True),
        sa.Column("transfer_name", sa.String(length=255), nullable=True),
        sa.Column("create_date", sa.DateTime(), nullable=True),
        sa.Column("mets_sha256", sa.String(length=64), nullable=True),
        sa.Column("size", sa.BigInteger(), nullable=True),
        sa.Column("storage_service_id", sa.Integer(), nullable=False),
        sa.Column("storage_location_id", sa.Integer(), nullable=False),
        sa.Column("fetch_job_id", sa.Integer(), nullable=False),
        sa.Column("origin_pipeline_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["fetch_job_id"],
            ["fetch_job.id"],
        ),
        sa.ForeignKeyConstraint(
            ["origin_pipeline_id"],
            ["pipeline.id"],
        ),
        sa.ForeignKeyConstraint(
            ["storage_location_id"],
            ["storage_location.id"],
        ),
        sa.ForeignKeyConstraint(
            ["storage_service_id"],
            ["storage_service.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("aip", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_aip_create_date"), ["create_date"], unique=False
        )
        batch_op.create_index(batch_op.f("ix_aip_uuid"), ["uuid"], unique=False)

    op.create_table(
        "index_tasks",
        sa.Column("index_task_id", sa.String(length=36), nullable=False),
        sa.Column("fetch_job_id", sa.Integer(), nullable=False),
        sa.Column("indexing_start", sa.DateTime(), nullable=True),
        sa.Column("indexing_progress", sa.String(length=255), nullable=True),
        sa.Column("indexing_end", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["fetch_job_id"],
            ["fetch_job.id"],
        ),
        sa.PrimaryKeyConstraint("index_task_id"),
    )
    op.create_table(
        "file",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=True),
        sa.Column("filepath", sa.Text(), nullable=True),
        sa.Column("uuid", sa.String(length=255), nullable=True),
        sa.Column(
            "file_type",
            sa.Enum("original", "preservation", name="filetype"),
            nullable=True,
        ),
        sa.Column("size", sa.BigInteger(), nullable=True),
        sa.Column("date_created", sa.DateTime(), nullable=True),
        sa.Column("puid", sa.String(length=255), nullable=True),
        sa.Column("file_format", sa.String(length=255), nullable=True),
        sa.Column("format_version", sa.String(length=255), nullable=True),
        sa.Column("checksum_type", sa.String(length=255), nullable=True),
        sa.Column("checksum_value", sa.String(length=255), nullable=True),
        sa.Column("premis_object", mysql.LONGTEXT(), nullable=True),
        sa.Column("original_file_id", sa.Integer(), nullable=True),
        sa.Column("aip_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["aip_id"],
            ["aip.id"],
        ),
        sa.ForeignKeyConstraint(
            ["original_file_id"],
            ["file.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("file", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_file_checksum_value"), ["checksum_value"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_file_date_created"), ["date_created"], unique=False
        )
        batch_op.create_index(batch_op.f("ix_file_name"), ["name"], unique=False)
        batch_op.create_index(batch_op.f("ix_file_puid"), ["puid"], unique=False)
        batch_op.create_index(batch_op.f("ix_file_uuid"), ["uuid"], unique=False)

    op.create_table(
        "event",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("type", sa.String(length=255), nullable=True),
        sa.Column("uuid", sa.String(length=255), nullable=True),
        sa.Column("date", sa.DateTime(), nullable=True),
        sa.Column("detail", sa.String(length=255), nullable=True),
        sa.Column("outcome", sa.String(length=255), nullable=True),
        sa.Column("outcome_detail", mysql.LONGTEXT(), nullable=True),
        sa.Column("file_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["file_id"],
            ["file.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("event", schema=None) as batch_op:
        batch_op.create_index(batch_op.f("ix_event_type"), ["type"], unique=False)
        batch_op.create_index(batch_op.f("ix_event_uuid"), ["uuid"], unique=False)

    op.create_table(
        "event_agents",
        sa.Column("event_id", sa.Integer(), nullable=True),
        sa.Column("agent_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["agent_id"],
            ["agent.id"],
        ),
        sa.ForeignKeyConstraint(
            ["event_id"],
            ["event.id"],
        ),
    )
    op.create_table(
        "get_mets_tasks",
        sa.Column("get_mets_task_id", sa.String(length=36), nullable=False),
        sa.Column("workflow_coordinator_id", sa.String(length=36), nullable=True),
        sa.Column("package_uuid", sa.String(length=36), nullable=True),
        sa.Column("status", sa.String(length=36), nullable=True),
        sa.PrimaryKeyConstraint("get_mets_task_id"),
    )
    op.create_table(
        "package_tasks",
        sa.Column("package_task_id", sa.String(length=36), nullable=False),
        sa.Column("workflow_coordinator_id", sa.String(length=36), nullable=True),
        sa.PrimaryKeyConstraint("package_task_id"),
    )
    # ### End Alembic commands. ###


def downgrade():
    # ### Commands auto-generated by Alembic. Please adjust. ###
    op.drop_table("package_tasks")
    op.drop_table("get_mets_tasks")
    op.drop_table("event_agents")
    with op.batch_alter_table("event", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_event_uuid"))
        batch_op.drop_index(batch_op.f("ix_event_type"))

    op.drop_table("event")
    with op.batch_alter_table("file", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_file_uuid"))
        batch_op.drop_index(batch_op.f("ix_file_puid"))
        batch_op.drop_index(batch_op.f("ix_file_name"))
        batch_op.drop_index(batch_op.f("ix_file_date_created"))
        batch_op.drop_index(batch_op.f("ix_file_checksum_value"))

    op.drop_table("file")
    op.drop_table("index_tasks")
    with op.batch_alter_table("aip", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_aip_uuid"))
        batch_op.drop_index(batch_op.f("ix_aip_create_date"))

    op.drop_table("aip")
    with op.batch_alter_table("storage_location", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_storage_location_current_location"))

    op.drop_table("storage_location")
    op.drop_table("fetch_job")
    with op.batch_alter_table("agent", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_agent_linking_type_value"))
        batch_op.drop_index(batch_op.f("ix_agent_agent_value"))
        batch_op.drop_index(batch_op.f("ix_agent_agent_type"))

    op.drop_table("agent")
    with op.batch_alter_table("storage_service", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_storage_service_name"))

    op.drop_table("storage_service")
    with op.batch_alter_table("pipeline", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_pipeline_origin_pipeline"))

    op.drop_table("pipeline")
    # ### End Alembic commands. ###
