"""Initial PostgreSQL schema

Revision ID: 3307e3616c5e
Revises:
Create Date: 2025-12-30 11:57:32.430719

"""

from collections.abc import Sequence

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

# revision identifiers, used by Alembic.
revision: str = "3307e3616c5e"
down_revision: str | Sequence[str] | None = None
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "actors",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("creator", sa.String(length=255), nullable=False),
        sa.Column("passphrase", sa.Text(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_actors_creator"), "actors", ["creator"], unique=False)
    op.create_table(
        "attributes",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("bucket_name", sa.String(length=255), nullable=False),
        sa.Column("bucket", sa.String(length=255), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("timestamp", sa.DateTime(), nullable=True),
        sa.Column("ttl_timestamp", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint("id", "bucket_name"),
    )
    op.create_index(
        "idx_attributes_ttl",
        "attributes",
        ["ttl_timestamp"],
        unique=False,
        postgresql_where="ttl_timestamp IS NOT NULL",
    )
    op.create_table(
        "peertrustees",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("peerid", sa.String(length=255), nullable=False),
        sa.Column("baseuri", sa.Text(), nullable=False),
        sa.Column("type", sa.String(length=255), nullable=False),
        sa.Column("passphrase", sa.Text(), nullable=False),
        sa.PrimaryKeyConstraint("id", "peerid"),
    )
    op.create_table(
        "properties",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("value", sa.Text(), nullable=False),
        sa.PrimaryKeyConstraint("id", "name"),
    )
    op.create_index("idx_properties_value", "properties", ["value"], unique=False)
    op.create_table(
        "subscription_diffs",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("subid_seqnr", sa.String(length=255), nullable=False),
        sa.Column("subid", sa.String(length=255), nullable=False),
        sa.Column("timestamp", sa.DateTime(), nullable=False),
        sa.Column("diff", sa.Text(), nullable=False),
        sa.Column("seqnr", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id", "subid_seqnr"),
    )
    op.create_table(
        "subscriptions",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("peer_sub_id", sa.String(length=255), nullable=False),
        sa.Column("peerid", sa.String(length=255), nullable=False),
        sa.Column("subid", sa.String(length=255), nullable=False),
        sa.Column("granularity", sa.String(length=255), nullable=True),
        sa.Column("target", sa.String(length=255), nullable=True),
        sa.Column("subtarget", sa.String(length=255), nullable=True),
        sa.Column("resource", sa.String(length=255), nullable=True),
        sa.Column("seqnr", sa.Integer(), nullable=False),
        sa.Column("callback", sa.Boolean(), nullable=False),
        sa.PrimaryKeyConstraint("id", "peer_sub_id"),
    )
    op.create_table(
        "trusts",
        sa.Column("id", sa.String(length=255), nullable=False),
        sa.Column("peerid", sa.String(length=255), nullable=False),
        sa.Column("baseuri", sa.Text(), nullable=False),
        sa.Column("type", sa.String(length=255), nullable=False),
        sa.Column("relationship", sa.String(length=255), nullable=False),
        sa.Column("secret", sa.String(length=255), nullable=False),
        sa.Column("desc", sa.Text(), nullable=True),
        sa.Column("approved", sa.Boolean(), nullable=False),
        sa.Column("peer_approved", sa.Boolean(), nullable=False),
        sa.Column("verified", sa.Boolean(), nullable=False),
        sa.Column("verification_token", sa.String(length=255), nullable=True),
        sa.Column("peer_identifier", sa.String(length=255), nullable=True),
        sa.Column("established_via", sa.String(length=255), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("last_accessed", sa.DateTime(), nullable=True),
        sa.Column("last_connected_via", sa.String(length=255), nullable=True),
        sa.Column("client_name", sa.String(length=255), nullable=True),
        sa.Column("client_version", sa.String(length=255), nullable=True),
        sa.Column("client_platform", sa.String(length=255), nullable=True),
        sa.Column("oauth_client_id", sa.String(length=255), nullable=True),
        sa.PrimaryKeyConstraint("id", "peerid"),
    )
    op.create_index("idx_trusts_secret", "trusts", ["secret"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("idx_trusts_secret", table_name="trusts")
    op.drop_table("trusts")
    op.drop_table("subscriptions")
    op.drop_table("subscription_diffs")
    op.drop_index("idx_properties_value", table_name="properties")
    op.drop_table("properties")
    op.drop_table("peertrustees")
    op.drop_index(
        "idx_attributes_ttl",
        table_name="attributes",
        postgresql_where="ttl_timestamp IS NOT NULL",
    )
    op.drop_table("attributes")
    op.drop_index(op.f("ix_actors_creator"), table_name="actors")
    op.drop_table("actors")
    # ### end Alembic commands ###
