"""Change status_updates to JSON

Revision ID: bc853b0f6e47
Revises: 9716bc0dfe9c
Create Date: 2024-10-14 14:57:55.117588

"""

from typing import Sequence, Union

import sqlalchemy as sa
from pgvector.sqlalchemy import Vector
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "bc853b0f6e47"
down_revision: Union[str, None] = "9716bc0dfe9c"
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.drop_index("ix_memoryentity_id", table_name="memoryentity")
    op.drop_table("memoryentity")
    op.drop_index("ix_calendareventdb_id", table_name="calendareventdb")
    op.drop_table("calendareventdb")

    # Drop the existing status_updates column
    op.drop_column("goal", "status_updates")

    # Add the new status_updates column as JSON
    op.add_column("goal", sa.Column("status_updates", sa.JSON(), nullable=False, server_default=sa.text("'[]'::jsonb")))
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Drop the JSON status_updates column
    op.drop_column("goal", "status_updates")

    # Re-add the status_updates column as ARRAY
    op.add_column(
        "goal", sa.Column("status_updates", postgresql.ARRAY(sa.VARCHAR()), nullable=False, server_default="'{}'::character varying[]")
    )
    op.create_table(
        "calendareventdb",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column("event_id", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("summary", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("description", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.Column("start", postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
        sa.Column("end", postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
        sa.Column("location", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.Column("attendee_emails", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("recurrence", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("reminders", sa.BOOLEAN(), autoincrement=False, nullable=False),
        sa.Column("visibility", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.PrimaryKeyConstraint("id", name="calendareventdb_pkey"),
    )
    op.create_index("ix_calendareventdb_id", "calendareventdb", ["id"], unique=False)
    op.create_table(
        "memoryentity",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
        sa.Column("updated_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
        sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column("entity_name", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("entity_label", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("text", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("embedding", Vector(dim=1536), autoincrement=False, nullable=True),
        sa.Column("embedding_text_md5", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.PrimaryKeyConstraint("id", name="memoryentity_pkey"),
        sa.UniqueConstraint("user_id", "entity_name", "entity_label", name="memoryentity_user_id_entity_name_entity_label_key"),
    )
    op.create_index("ix_memoryentity_id", "memoryentity", ["id"], unique=False)
    # ### end Alembic commands ###
