"""initial tables

Revision ID: 9716bc0dfe9c
Revises: 45223a2c0c67
Create Date: 2024-10-05 16:00:21.417357

"""

from typing import Sequence, Union

import sqlalchemy as sa
from pgvector.sqlalchemy import Vector
from sqlmodel.sql.sqltypes import AutoString

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "9716bc0dfe9c"
down_revision: Union[str, None] = "45223a2c0c67"
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(
        "memory",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("name", AutoString(), nullable=False),
        sa.Column("text", AutoString(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=True),
        sa.Column("embedding", Vector(dim=1536), nullable=True),
        sa.Column("embedding_text_md5", AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_memory_id"), "memory", ["id"], unique=False)
    op.create_table(
        "calendareventdb",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("event_id", AutoString(), nullable=False),
        sa.Column("summary", AutoString(), nullable=False),
        sa.Column("description", AutoString(), nullable=True),
        sa.Column("start", sa.DateTime(), nullable=False),
        sa.Column("end", sa.DateTime(), nullable=False),
        sa.Column("location", AutoString(), nullable=True),
        sa.Column("attendee_emails", AutoString(), nullable=False),
        sa.Column("recurrence", AutoString(), nullable=False),
        sa.Column("reminders", sa.Boolean(), nullable=False),
        sa.Column("visibility", AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_calendareventdb_id"), "calendareventdb", ["id"], unique=False)
    op.create_table(
        "contextmessageset",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("message_ids", sa.JSON(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "is_active"),
    )
    op.create_index(op.f("ix_contextmessageset_id"), "contextmessageset", ["id"], unique=False)
    op.create_table(
        "goal",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("name", AutoString(), nullable=False),
        sa.Column("description", AutoString(), nullable=False),
        sa.Column("status_updates", sa.ARRAY(sa.String()), server_default="{}", nullable=False),
        sa.Column("strategy", AutoString(), nullable=False),
        sa.Column("end_condition", AutoString(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=True),
        sa.Column("priority", sa.Integer(), nullable=True),
        sa.Column("target_completion_time", sa.DateTime(), nullable=True),
        sa.Column("embedding", Vector(dim=1536), nullable=True),
        sa.Column("embedding_text_md5", AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "name", "is_active"),
    )
    op.create_index(op.f("ix_goal_id"), "goal", ["id"], unique=False)
    op.create_table(
        "memoryentity",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("entity_name", AutoString(), nullable=False),
        sa.Column("entity_label", AutoString(), nullable=False),
        sa.Column("text", AutoString(), nullable=False),
        sa.Column("embedding", Vector(dim=1536), nullable=True),
        sa.Column("embedding_text_md5", AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "entity_name", "entity_label"),
    )
    op.create_index(op.f("ix_memoryentity_id"), "memoryentity", ["id"], unique=False)
    op.create_table(
        "message",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("role", AutoString(), nullable=False),
        sa.Column("content", AutoString(), nullable=True),
        sa.Column("model", AutoString(), nullable=True),
        sa.Column("tool_calls", sa.JSON(), nullable=True),
        sa.Column("tool_call_id", AutoString(), nullable=True),
        sa.Column("memory_metadata", sa.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_message_id"), "message", ["id"], unique=False)
    op.create_table(
        "user",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_id"), "user", ["id"], unique=False)
    op.create_table(
        "userpreference",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("preferred_name", AutoString(), nullable=True),
        sa.Column("full_name", AutoString(), nullable=True),
        sa.Column("user_time_zone", AutoString(), nullable=True),
        sa.Column("display_internal_monologue", sa.Boolean(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "is_active"),
    )
    op.create_index(op.f("ix_userpreference_id"), "userpreference", ["id"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_userpreference_id"), table_name="userpreference")
    op.drop_table("userpreference")
    op.drop_index(op.f("ix_user_id"), table_name="user")
    op.drop_table("user")
    op.drop_index(op.f("ix_message_id"), table_name="message")
    op.drop_table("message")
    op.drop_index(op.f("ix_memoryentity_id"), table_name="memoryentity")
    op.drop_table("memoryentity")
    op.drop_index(op.f("ix_goal_id"), table_name="goal")
    op.drop_table("goal")
    op.drop_index(op.f("ix_contextmessageset_id"), table_name="contextmessageset")
    op.drop_table("contextmessageset")
    op.drop_index(op.f("ix_calendareventdb_id"), table_name="calendareventdb")
    op.drop_table("calendareventdb")
    op.drop_index(op.f("ix_memory_id"), table_name="memory")
    op.drop_table("memory")
    # ### end Alembic commands ###
