"""initial

Revision ID: 8514cd884e91
Revises:
Create Date: 2025-03-02 07:49:19.673286+00:00

"""

from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "8514cd884e91"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "accounts",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("username", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
        sa.Column("account_id", sa.Integer(), nullable=False),
        sa.Column("hashed_password", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("account_id", name=op.f("pk_accounts_account_id")),
        sa.UniqueConstraint("username", name=op.f("uq_accounts_username")),
    )
    op.create_table(
        "friedolin",
        sa.Column("friedolin", sa.Enum("friedolin", name="_friedolintype"), nullable=False),
        sa.PrimaryKeyConstraint("friedolin", name=op.f("pk_friedolin_friedolin")),
        sa.UniqueConstraint("friedolin", name=op.f("uq_friedolin_friedolin")),
    )
    op.create_table(
        "reports",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("report_id", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("report_id", name=op.f("pk_reports_report_id")),
    )
    op.create_table(
        "scopes",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("scope_id", sa.Integer(), nullable=False),
        sa.Column(
            "scope",
            sa.Enum("submit", "upload", "review", "admin", "root", name="scopes"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("scope_id", name=op.f("pk_scopes_scope_id")),
        sa.UniqueConstraint("scope", name=op.f("uq_scopes_scope")),
    )
    op.create_table(
        "tags",
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.Column("tag", sqlmodel.sql.sqltypes.AutoString(length=32), nullable=False),
        sa.PrimaryKeyConstraint("tag_id", name=op.f("pk_tags_tag_id")),
        sa.UniqueConstraint("tag", name=op.f("uq_tags_tag")),
    )
    op.create_table(
        "account_scope_links",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("account_id", sa.Integer(), nullable=False),
        sa.Column("scope_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["accounts.account_id"],
            name=op.f("fk_account_scope_links_account_id_accounts"),
        ),
        sa.ForeignKeyConstraint(
            ["scope_id"], ["scopes.scope_id"], name=op.f("fk_account_scope_links_scope_id_scopes")
        ),
        sa.PrimaryKeyConstraint(
            "account_id", "scope_id", name=op.f("pk_account_scope_links_account_id_scope_id")
        ),
        sa.UniqueConstraint("account_id", "scope_id", name="_account_scope_uc"),
    )
    op.create_table(
        "datasets",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("title", sqlmodel.sql.sqltypes.AutoString(length=512), nullable=False),
        sa.Column("slug", sqlmodel.sql.sqltypes.AutoString(length=128), nullable=False),
        sa.Column("publisher", sqlmodel.sql.sqltypes.AutoString(length=256), nullable=False),
        sa.Column("homepage", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(length=4096), nullable=True),
        sa.Column("dataset_created_at", sa.DateTime(), nullable=True),
        sa.Column("dataset_updated_at", sa.DateTime(), nullable=True),
        sa.Column(
            "source_type",
            sa.Enum("unknown", "web", "http", "ftp", "s3", name="sourcetype"),
            nullable=False,
        ),
        sa.Column("source_available", sa.Boolean(), nullable=False),
        sa.Column("last_seen_at", sa.DateTime(), nullable=True),
        sa.Column(
            "source_access",
            sa.Enum(
                "unknown", "public", "registration", "paywalled", "proprietary", name="accesstype"
            ),
            nullable=False,
        ),
        sa.Column(
            "scarcity",
            sa.Enum(
                "unknown",
                "source_only",
                "external_unconfirmed",
                "external_confirmed",
                "uploaded",
                name="scarcity",
            ),
            nullable=False,
        ),
        sa.Column(
            "threat",
            sa.Enum(
                "unknown",
                "indefinite",
                "watchlist",
                "endangered",
                "takedown_issued",
                "extinct",
                name="threat",
            ),
            nullable=False,
        ),
        sa.Column("dataset_id", sa.Integer(), nullable=False),
        sa.Column("account_id", sa.Integer(), nullable=True),
        sa.Column(
            "scrape_status",
            sa.Enum("unknown", "not_started", "in_progress", "completed", name="scrapestatus"),
            nullable=False,
        ),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ["account_id"], ["accounts.account_id"], name=op.f("fk_datasets_account_id_accounts")
        ),
        sa.PrimaryKeyConstraint("dataset_id", name=op.f("pk_datasets_dataset_id")),
    )
    op.create_index(op.f("ix_datasets_slug"), "datasets", ["slug"], unique=True)
    op.create_table(
        "dataset_parts",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("part_slug", sqlmodel.sql.sqltypes.AutoString(length=256), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(length=4096), nullable=True),
        sa.Column("dataset_part_id", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.Integer(), nullable=True),
        sa.Column("account_id", sa.Integer(), nullable=True),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["accounts.account_id"],
            name=op.f("fk_dataset_parts_account_id_accounts"),
        ),
        sa.ForeignKeyConstraint(
            ["dataset_id"],
            ["datasets.dataset_id"],
            name=op.f("fk_dataset_parts_dataset_id_datasets"),
        ),
        sa.PrimaryKeyConstraint("dataset_part_id", name=op.f("pk_dataset_parts_dataset_part_id")),
        sa.UniqueConstraint("dataset_id", "part_slug", name="_dataset_part_slug_uc"),
    )
    op.create_index(
        op.f("ix_dataset_parts_part_slug"), "dataset_parts", ["part_slug"], unique=False
    )
    op.create_table(
        "dataset_tag_links",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("dataset_id", sa.Integer(), nullable=False),
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dataset_id"],
            ["datasets.dataset_id"],
            name=op.f("fk_dataset_tag_links_dataset_id_datasets"),
        ),
        sa.ForeignKeyConstraint(
            ["tag_id"], ["tags.tag_id"], name=op.f("fk_dataset_tag_links_tag_id_tags")
        ),
        sa.PrimaryKeyConstraint(
            "dataset_id", "tag_id", name=op.f("pk_dataset_tag_links_dataset_id_tag_id")
        ),
    )
    op.create_table(
        "dataset_urls",
        sa.Column("dataset_url_id", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.Integer(), nullable=True),
        sa.Column("url", sqlmodel.sql.sqltypes.AutoString(length=512), nullable=False),
        sa.ForeignKeyConstraint(
            ["dataset_id"],
            ["datasets.dataset_id"],
            name=op.f("fk_dataset_urls_dataset_id_datasets"),
        ),
        sa.PrimaryKeyConstraint("dataset_url_id", name=op.f("pk_dataset_urls_dataset_url_id")),
    )
    op.create_table(
        "external_identifiers",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column(
            "type",
            sa.Enum(
                "ark",
                "cid",
                "doi",
                "isni",
                "isbn",
                "issn",
                "purl",
                "qid",
                "rrid",
                "urn",
                "uri",
                "orcid",
                name="externalidentifiertype",
            ),
            nullable=False,
        ),
        sa.Column("identifier", sqlmodel.sql.sqltypes.AutoString(length=512), nullable=False),
        sa.Column("external_identifier_id", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["dataset_id"],
            ["datasets.dataset_id"],
            name=op.f("fk_external_identifiers_dataset_id_datasets"),
        ),
        sa.PrimaryKeyConstraint(
            "external_identifier_id", name=op.f("pk_external_identifiers_external_identifier_id")
        ),
    )
    op.create_table(
        "external_sources",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("source", sqlmodel.sql.sqltypes.AutoString(length=256), nullable=False),
        sa.Column("url", sqlmodel.sql.sqltypes.AutoString(length=512), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(length=4096), nullable=False),
        sa.Column("external_source_id", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.Integer(), nullable=True),
        sa.Column("account_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["accounts.account_id"],
            name=op.f("fk_external_sources_account_id_accounts"),
        ),
        sa.ForeignKeyConstraint(
            ["dataset_id"],
            ["datasets.dataset_id"],
            name=op.f("fk_external_sources_dataset_id_datasets"),
        ),
        sa.PrimaryKeyConstraint(
            "external_source_id", name=op.f("pk_external_sources_external_source_id")
        ),
    )
    op.create_table(
        "uploads",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("method", sqlmodel.sql.sqltypes.AutoString(length=2048), nullable=True),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(length=4096), nullable=True),
        sa.Column("upload_id", sa.Integer(), nullable=False),
        sa.Column("dataset_id", sa.Integer(), nullable=True),
        sa.Column("account_id", sa.Integer(), nullable=True),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("seeders", sa.Integer(), nullable=True),
        sa.Column("leechers", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"], ["accounts.account_id"], name=op.f("fk_uploads_account_id_accounts")
        ),
        sa.ForeignKeyConstraint(
            ["dataset_id"], ["datasets.dataset_id"], name=op.f("fk_uploads_dataset_id_datasets")
        ),
        sa.PrimaryKeyConstraint("upload_id", name=op.f("pk_uploads_upload_id")),
    )
    op.create_table(
        "audit_log",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("audit_log_id", sa.Integer(), nullable=False),
        sa.Column("actor_id", sa.Integer(), nullable=True),
        sa.Column(
            "action",
            sa.Enum(
                "request",
                "approve",
                "deny",
                "report",
                "add_scope",
                "remove_scope",
                "dismiss",
                "trust",
                "distrust",
                "suspend",
                name="moderationaction",
            ),
            nullable=False,
        ),
        sa.Column("target_dataset_id", sa.Integer(), nullable=True),
        sa.Column("target_upload_id", sa.Integer(), nullable=True),
        sa.Column("target_account_id", sa.Integer(), nullable=True),
        sa.Column("value", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.ForeignKeyConstraint(
            ["actor_id"], ["accounts.account_id"], name=op.f("fk_audit_log_actor_id_accounts")
        ),
        sa.ForeignKeyConstraint(
            ["target_account_id"],
            ["accounts.account_id"],
            name=op.f("fk_audit_log_target_account_id_accounts"),
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["target_dataset_id"],
            ["datasets.dataset_id"],
            name=op.f("fk_audit_log_target_dataset_id_datasets"),
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["target_upload_id"],
            ["uploads.upload_id"],
            name=op.f("fk_audit_log_target_upload_id_uploads"),
            ondelete="SET NULL",
        ),
        sa.PrimaryKeyConstraint("audit_log_id", name=op.f("pk_audit_log_audit_log_id")),
    )
    op.create_table(
        "dataset_paths",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("dataset_path_id", sa.Integer(), nullable=False),
        sa.Column("dataset_part_id", sa.Integer(), nullable=True),
        sa.Column("path", sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=False),
        sa.ForeignKeyConstraint(
            ["dataset_part_id"],
            ["dataset_parts.dataset_part_id"],
            name=op.f("fk_dataset_paths_dataset_part_id_dataset_parts"),
        ),
        sa.PrimaryKeyConstraint("dataset_path_id", name=op.f("pk_dataset_paths_dataset_path_id")),
    )
    op.create_table(
        "torrent_files",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("file_name", sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=False),
        sa.Column("v1_infohash", sqlmodel.sql.sqltypes.AutoString(length=40), nullable=False),
        sa.Column("v2_infohash", sqlmodel.sql.sqltypes.AutoString(length=64), nullable=True),
        sa.Column("version", sa.Enum("v1", "v2", "hybrid", name="torrentversion"), nullable=False),
        sa.Column("total_size", sa.Integer(), nullable=False),
        sa.Column("piece_size", sa.Integer(), nullable=False),
        sa.Column("torrent_size", sa.Integer(), nullable=True),
        sa.Column("torrent_file_id", sa.Integer(), nullable=False),
        sa.Column("account_id", sa.Integer(), nullable=True),
        sa.Column("upload_id", sa.Integer(), nullable=True),
        sa.Column("short_hash", sqlmodel.sql.sqltypes.AutoString(length=8), nullable=False),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["accounts.account_id"],
            name=op.f("fk_torrent_files_account_id_accounts"),
        ),
        sa.ForeignKeyConstraint(
            ["upload_id"], ["uploads.upload_id"], name=op.f("fk_torrent_files_upload_id_uploads")
        ),
        sa.PrimaryKeyConstraint("torrent_file_id", name=op.f("pk_torrent_files_torrent_file_id")),
    )
    op.create_index(
        op.f("ix_torrent_files_short_hash"), "torrent_files", ["short_hash"], unique=False
    )
    op.create_index(
        op.f("ix_torrent_files_v1_infohash"), "torrent_files", ["v1_infohash"], unique=True
    )
    op.create_index(
        op.f("ix_torrent_files_v2_infohash"), "torrent_files", ["v2_infohash"], unique=True
    )
    op.create_table(
        "upload_dataset_part_links",
        sa.Column("dataset_part_id", sa.Integer(), nullable=False),
        sa.Column("upload_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dataset_part_id"],
            ["dataset_parts.dataset_part_id"],
            name=op.f("fk_upload_dataset_part_links_dataset_part_id_dataset_parts"),
        ),
        sa.ForeignKeyConstraint(
            ["upload_id"],
            ["uploads.upload_id"],
            name=op.f("fk_upload_dataset_part_links_upload_id_uploads"),
        ),
        sa.PrimaryKeyConstraint(
            "dataset_part_id",
            "upload_id",
            name=op.f("pk_upload_dataset_part_links_dataset_part_id_upload_id"),
        ),
    )
    op.create_table(
        "files_in_torrent",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("file_in_torrent_id", sa.Integer(), nullable=False),
        sa.Column("path", sqlmodel.sql.sqltypes.AutoString(length=1024), nullable=False),
        sa.Column("size", sa.Integer(), nullable=False),
        sa.Column("torrent_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["torrent_id"],
            ["torrent_files.torrent_file_id"],
            name=op.f("fk_files_in_torrent_torrent_id_torrent_files"),
        ),
        sa.PrimaryKeyConstraint(
            "file_in_torrent_id", name=op.f("pk_files_in_torrent_file_in_torrent_id")
        ),
    )
    op.create_table(
        "trackers_in_torrent",
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("tracker_in_torrent_id", sa.Integer(), nullable=False),
        sa.Column("url", sqlmodel.sql.sqltypes.AutoString(length=512), nullable=False),
        sa.Column("torrent_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["torrent_id"],
            ["torrent_files.torrent_file_id"],
            name=op.f("fk_trackers_in_torrent_torrent_id_torrent_files"),
        ),
        sa.PrimaryKeyConstraint(
            "tracker_in_torrent_id", name=op.f("pk_trackers_in_torrent_tracker_in_torrent_id")
        ),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("trackers_in_torrent")
    op.drop_table("files_in_torrent")
    op.drop_table("upload_dataset_part_links")
    op.drop_index(op.f("ix_torrent_files_v2_infohash"), table_name="torrent_files")
    op.drop_index(op.f("ix_torrent_files_v1_infohash"), table_name="torrent_files")
    op.drop_index(op.f("ix_torrent_files_short_hash"), table_name="torrent_files")
    op.drop_table("torrent_files")
    op.drop_table("dataset_paths")
    op.drop_table("audit_log")
    op.drop_table("uploads")
    op.drop_table("external_sources")
    op.drop_table("external_identifiers")
    op.drop_table("dataset_urls")
    op.drop_table("dataset_tag_links")
    op.drop_index(op.f("ix_dataset_parts_part_slug"), table_name="dataset_parts")
    op.drop_table("dataset_parts")
    op.drop_index(op.f("ix_datasets_slug"), table_name="datasets")
    op.drop_table("datasets")
    op.drop_table("account_scope_links")
    op.drop_table("tags")
    op.drop_table("scopes")
    op.drop_table("reports")
    op.drop_table("friedolin")
    op.drop_table("accounts")
    # ### end Alembic commands ###
