# -*- coding: utf-8 -*-
"""init

Revision ID: b8e52f791852
Revises:
Create Date: 2025-11-25 11:30:09.959540

"""
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from alembic import op


# revision identifiers, used by Alembic.
revision = "b8e52f791852"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "file",
        sa.Column(
            "filename",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "mime_type",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "extension",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column("size", sa.Integer(), nullable=False),
        sa.Column(
            "storage_path",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "storage_type",
            sqlmodel.sql.sqltypes.AutoString(),
            server_default="local",
            nullable=False,
        ),
        sa.Column(
            "create_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "update_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column("shared", sa.Boolean(), server_default="0", nullable=False),
        sa.Column("user_id", sa.Uuid(), nullable=True),
        sa.Column("conversation_id", sa.Uuid(), nullable=True),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_file_conversation_id"),
        "file",
        ["conversation_id"],
        unique=False,
    )
    op.create_table(
        "user",
        sa.Column(
            "email",
            sqlmodel.sql.sqltypes.AutoString(length=255),
            nullable=False,
        ),
        sa.Column(
            "username",
            sqlmodel.sql.sqltypes.AutoString(length=255),
            nullable=False,
        ),
        sa.Column("avatar", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("is_superuser", sa.Boolean(), nullable=False),
        sa.Column(
            "create_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "update_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "last_login_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column(
            "last_login_ip",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column(
            "password",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column(
            "oauth_provider",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column(
            "oauth_id",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column("parent_id", sa.Uuid(), nullable=True),
        sa.ForeignKeyConstraint(["parent_id"], ["user.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_email"), "user", ["email"], unique=True)
    op.create_index(
        op.f("ix_user_oauth_id"),
        "user",
        ["oauth_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_user_oauth_provider"),
        "user",
        ["oauth_provider"],
        unique=False,
    )
    op.create_table(
        "conversation",
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "description",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column(
            "create_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "update_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "collected",
            sa.Boolean(),
            server_default="0",
            nullable=True,
        ),
        sa.Column("pinned", sa.Boolean(), server_default="0", nullable=True),
        sa.Column("shared", sa.Boolean(), server_default="0", nullable=True),
        sa.Column("running", sa.Boolean(), server_default="0", nullable=True),
        sa.Column("deleted", sa.Boolean(), server_default="0", nullable=True),
        sa.Column("user_id", sa.Uuid(), nullable=False),
        sa.Column(
            "chat_mode",
            sa.Enum(
                "general",
                "dr",
                "browser",
                "finance",
                "ds",
                name="chatmode",
            ),
            nullable=True,
        ),
        sa.Column(
            "sandbox_id",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "sandbox_url",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["user.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "state",
        sa.Column("conversation_id", sa.Uuid(), nullable=False),
        sa.Column(
            "content",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=True,
        ),
        sa.Column(
            "create_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "update_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["conversation.id"],
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("conversation_id"),
    )
    op.create_index(
        op.f("ix_state_conversation_id"),
        "state",
        ["conversation_id"],
        unique=True,
    )
    op.create_table(
        "message",
        sa.Column("message", sa.JSON(), nullable=True),
        sa.Column(
            "create_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "update_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "feedback",
            sa.Enum("like", "dislike", name="feedbacktype"),
            nullable=True,
        ),
        sa.Column(
            "collected",
            sa.Boolean(),
            server_default="0",
            nullable=True,
        ),
        sa.Column("task_id", sa.Uuid(), nullable=True),
        sa.Column("meta_data", sa.JSON(), nullable=True),
        sa.Column("conversation_id", sa.Uuid(), nullable=False),
        sa.Column("parent_message_id", sa.Uuid(), nullable=True),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["conversation.id"],
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(["parent_message_id"], ["message.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_message_conversation_id"),
        "message",
        ["conversation_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_message_parent_message_id"),
        "message",
        ["parent_message_id"],
        unique=False,
    )
    op.create_table(
        "plan",
        sa.Column("conversation_id", sa.Uuid(), nullable=False),
        sa.Column("content", sa.JSON(), nullable=True),
        sa.Column(
            "create_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column(
            "update_time",
            sqlmodel.sql.sqltypes.AutoString(),
            nullable=False,
        ),
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["conversation.id"],
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("plan")
    op.drop_index(op.f("ix_message_parent_message_id"), table_name="message")
    op.drop_index(op.f("ix_message_conversation_id"), table_name="message")
    op.drop_table("message")
    op.drop_index(op.f("ix_state_conversation_id"), table_name="state")
    op.drop_table("state")
    op.drop_table("conversation")
    op.drop_index(op.f("ix_user_oauth_provider"), table_name="user")
    op.drop_index(op.f("ix_user_oauth_id"), table_name="user")
    op.drop_index(op.f("ix_user_email"), table_name="user")
    op.drop_table("user")
    op.drop_index(op.f("ix_file_conversation_id"), table_name="file")
    op.drop_table("file")
    # ### end Alembic commands ###
