"""add documents table

Revision ID: 366a6659343d
Revises: 55d923d0e3be
Create Date: 2026-04-02 15:39:44.182345

"""

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

# revision identifiers, used by Alembic.
revision = "366a6659343d"
down_revision = "55d923d0e3be"
branch_labels = None
depends_on = None


def upgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.create_table(
    "documents",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("user_id", sa.String(), nullable=False),
    sa.Column("title", sa.String(length=500), nullable=False),
    sa.Column("content", sa.Text(), nullable=False),
    sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column("folder", sa.String(), nullable=True),
    sa.Column("external_id", sa.String(), nullable=True),
    sa.Column("source_type", sa.String(), nullable=False),
    sa.Column("source_provider", sa.String(), nullable=True),
    sa.Column("sections_indexed", sa.Integer(), nullable=False),
    sa.Column("created_at", sa.DateTime(), nullable=False),
    sa.Column("updated_at", sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(
      ["graph_id"],
      ["graphs.graph_id"],
    ),
    sa.ForeignKeyConstraint(
      ["user_id"],
      ["users.id"],
    ),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint(
      "graph_id", "external_id", name="uq_documents_graph_external_id"
    ),
  )
  op.create_index("idx_documents_graph", "documents", ["graph_id"], unique=False)
  op.create_index(
    "idx_documents_graph_source", "documents", ["graph_id", "source_type"], unique=False
  )
  op.create_index("idx_documents_user", "documents", ["user_id"], unique=False)
  # ### end Alembic commands ###


def downgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.drop_index("idx_documents_user", table_name="documents")
  op.drop_index("idx_documents_graph_source", table_name="documents")
  op.drop_index("idx_documents_graph", table_name="documents")
  op.drop_table("documents")
  # ### end Alembic commands ###
