"""Initial migration

Revision ID: 9f2f70b48ab5
Revises:
Create Date: 2025-04-25 15:35:03.861966

"""

from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "9f2f70b48ab5"
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:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "repos",
        sa.Column(
            "id", sa.UUID(), nullable=False, comment="MergeStat identifier for the repo"
        ),
        sa.Column("repo", sa.Text(), nullable=False, comment="URL for the repo"),
        sa.Column("ref", sa.Text(), nullable=True, comment="ref for the repo"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp of when the MergeStat repo entry was created",
        ),
        sa.Column(
            "settings", sa.JSON(), nullable=False, comment="JSON settings for the repo"
        ),
        sa.Column(
            "tags", sa.JSON(), nullable=False, comment="array of tags for the repo"
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "git_blame",
        sa.Column(
            "repo_id",
            sa.UUID(),
            nullable=False,
            comment="foreign key for public.repos.id",
        ),
        sa.Column(
            "path",
            sa.Text(),
            nullable=False,
            comment="path of the file the modification was made in",
        ),
        sa.Column(
            "line_no",
            sa.Integer(),
            nullable=False,
            comment="line number of the modification",
        ),
        sa.Column(
            "author_email",
            sa.Text(),
            nullable=True,
            comment="email of the author who modified the line",
        ),
        sa.Column(
            "author_name",
            sa.Text(),
            nullable=True,
            comment="name of the author who modified the line",
        ),
        sa.Column(
            "author_when",
            sa.DateTime(timezone=True),
            nullable=True,
            comment="timestamp of when the modification was authored",
        ),
        sa.Column(
            "commit_hash",
            sa.Text(),
            nullable=True,
            comment="hash of the commit the modification was made in",
        ),
        sa.Column("line", sa.Text(), nullable=True, comment="content of the line"),
        sa.Column(
            "_mergestat_synced_at",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp when record was synced into the MergeStat database",
        ),
        sa.ForeignKeyConstraint(["repo_id"], ["repos.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("repo_id", "path", "line_no"),
    )
    op.create_table(
        "git_commit_stats",
        sa.Column(
            "repo_id",
            sa.UUID(),
            nullable=False,
            comment="foreign key for public.repos.id",
        ),
        sa.Column(
            "commit_hash", sa.Text(), nullable=False, comment="hash of the commit"
        ),
        sa.Column(
            "file_path",
            sa.Text(),
            nullable=False,
            comment="path of the file the modification was made in",
        ),
        sa.Column(
            "additions",
            sa.Integer(),
            nullable=False,
            comment="the number of additions in this path of the commit",
        ),
        sa.Column(
            "deletions",
            sa.Integer(),
            nullable=False,
            comment="the number of deletions in this path of the commit",
        ),
        sa.Column(
            "old_file_mode",
            sa.Text(),
            nullable=False,
            comment="old file mode derived from git mode",
        ),
        sa.Column(
            "new_file_mode",
            sa.Text(),
            nullable=True,
            comment="new file mode derived from git mode",
        ),
        sa.Column(
            "_mergestat_synced_at",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp when record was synced into the MergeStat database",
        ),
        sa.ForeignKeyConstraint(["repo_id"], ["repos.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("repo_id", "commit_hash", "file_path"),
    )
    op.create_table(
        "git_commits",
        sa.Column(
            "repo_id",
            sa.UUID(),
            nullable=False,
            comment="foreign key for public.repos.id",
        ),
        sa.Column("hash", sa.Text(), nullable=False, comment="hash of the commit"),
        sa.Column("message", sa.Text(), nullable=True, comment="message of the commit"),
        sa.Column(
            "author_name",
            sa.Text(),
            nullable=True,
            comment="name of the author of the modification",
        ),
        sa.Column(
            "author_email",
            sa.Text(),
            nullable=True,
            comment="email of the author of the modification",
        ),
        sa.Column(
            "author_when",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp of when the modification was authored",
        ),
        sa.Column(
            "committer_name",
            sa.Text(),
            nullable=True,
            comment="name of the author who committed the modification",
        ),
        sa.Column(
            "committer_email",
            sa.Text(),
            nullable=True,
            comment="email of the author who committed the modification",
        ),
        sa.Column(
            "committer_when",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp of when the commit was made",
        ),
        sa.Column(
            "parents",
            sa.Integer(),
            nullable=False,
            comment="the number of parents of the commit",
        ),
        sa.Column(
            "_mergestat_synced_at",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp when record was synced into the MergeStat database",
        ),
        sa.ForeignKeyConstraint(["repo_id"], ["repos.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("repo_id", "hash"),
    )
    op.create_table(
        "git_files",
        sa.Column(
            "repo_id",
            sa.UUID(),
            nullable=False,
            comment="foreign key for public.repos.id",
        ),
        sa.Column("path", sa.Text(), nullable=False, comment="path of the file"),
        sa.Column(
            "executable",
            sa.Boolean(),
            nullable=False,
            comment="boolean to determine if the file is an executable",
        ),
        sa.Column("contents", sa.Text(), nullable=True, comment="contents of the file"),
        sa.Column(
            "_mergestat_synced_at",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp when record was synced into the MergeStat database",
        ),
        sa.ForeignKeyConstraint(["repo_id"], ["repos.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("repo_id", "path"),
    )
    op.create_table(
        "git_refs",
        sa.Column(
            "repo_id",
            sa.UUID(),
            nullable=False,
            comment="foreign key for public.repos.id",
        ),
        sa.Column("full_name", sa.Text(), nullable=False),
        sa.Column(
            "hash",
            sa.Text(),
            nullable=True,
            comment="hash of the commit for refs that are not of type tag",
        ),
        sa.Column("name", sa.Text(), nullable=True, comment="name of the ref"),
        sa.Column("remote", sa.Text(), nullable=True, comment="remote of the ref"),
        sa.Column("target", sa.Text(), nullable=True, comment="target of the ref"),
        sa.Column("type", sa.Text(), nullable=True, comment="type of the ref"),
        sa.Column(
            "tag_commit_hash",
            sa.Text(),
            nullable=True,
            comment="hash of the commit for refs that are of type tag",
        ),
        sa.Column(
            "_mergestat_synced_at",
            sa.DateTime(timezone=True),
            nullable=False,
            comment="timestamp when record was synced into the MergeStat database",
        ),
        sa.ForeignKeyConstraint(["repo_id"], ["repos.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("repo_id", "full_name"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("git_refs")
    op.drop_table("git_files")
    op.drop_table("git_commits")
    op.drop_table("git_commit_stats")
    op.drop_table("git_blame")
    op.drop_table("repos")
    # ### end Alembic commands ###
