"""Initial revision

Revision ID: 48f9a7635310
Revises: 
Create Date: 2023-12-27 01:35:29.099926

"""
from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "suppgram_agents",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("deactivated", sa.Boolean(), nullable=False),
        sa.Column("telegram_user_id", sa.Integer(), nullable=True),
        sa.Column("telegram_first_name", sa.String(), nullable=True),
        sa.Column("telegram_last_name", sa.String(), nullable=True),
        sa.Column("telegram_username", sa.String(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("telegram_user_id"),
    )
    op.create_table(
        "suppgram_customers",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("telegram_user_id", sa.Integer(), nullable=True),
        sa.Column("telegram_first_name", sa.String(), nullable=True),
        sa.Column("telegram_last_name", sa.String(), nullable=True),
        sa.Column("telegram_username", sa.String(), nullable=True),
        sa.Column("shell_uuid", sa.String(), nullable=True),
        sa.Column("pubnub_user_id", sa.String(), nullable=True),
        sa.Column("pubnub_channel_id", sa.String(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("shell_uuid"),
        sa.UniqueConstraint("telegram_user_id"),
    )
    op.create_table(
        "suppgram_telegram_chats",
        sa.Column("telegram_chat_id", sa.Integer(), nullable=False),
        sa.Column("roles", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("telegram_chat_id"),
    )
    op.create_table(
        "suppgram_tags",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("created_at_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("created_by_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["created_by_id"],
            ["suppgram_agents.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "suppgram_workplaces",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("agent_id", sa.Integer(), nullable=False),
        sa.Column("telegram_bot_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["agent_id"],
            ["suppgram_agents.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "suppgram_conversations",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("customer_id", sa.Integer(), nullable=False),
        sa.Column("assigned_workplace_id", sa.Integer(), nullable=True),
        sa.Column(
            "state",
            sa.Enum("NEW", "ASSIGNED", "RESOLVED", name="conversationstate"),
            nullable=False,
        ),
        sa.Column("customer_rating", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["assigned_workplace_id"],
            ["suppgram_workplaces.id"],
        ),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["suppgram_customers.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "suppgram_conversation_messages",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("conversation_id", sa.Integer(), nullable=False),
        sa.Column(
            "kind",
            sa.Enum("FROM_CUSTOMER", "FROM_AGENT", "POSTPONED", "RESOLVED", name="messagekind"),
            nullable=False,
        ),
        sa.Column("time_utc", sa.DateTime(timezone=True), nullable=False),
        sa.Column("text", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["suppgram_conversations.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "suppgram_conversation_tag_associations",
        sa.Column("conversation_id", sa.Integer(), nullable=False),
        sa.Column("tag_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["suppgram_conversations.id"],
        ),
        sa.ForeignKeyConstraint(
            ["tag_id"],
            ["suppgram_tags.id"],
        ),
        sa.PrimaryKeyConstraint("conversation_id", "tag_id"),
    )
    op.create_table(
        "suppgram_telegram_messages",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("chat_id", sa.Integer(), nullable=False),
        sa.Column("telegram_bot_id", sa.Integer(), nullable=False),
        sa.Column("telegram_message_id", sa.Integer(), nullable=False),
        sa.Column(
            "kind",
            sa.Enum(
                "NEW_CONVERSATION_NOTIFICATION",
                "RATE_CONVERSATION",
                "NUDGE_TO_START_BOT_NOTIFICATION",
                "CUSTOMER_MESSAGE_HISTORY",
                name="telegrammessagekind",
            ),
            nullable=False,
        ),
        sa.Column("agent_id", sa.Integer(), nullable=True),
        sa.Column("customer_id", sa.Integer(), nullable=True),
        sa.Column("conversation_id", sa.Integer(), nullable=True),
        sa.Column("telegram_bot_username", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["agent_id"],
            ["suppgram_agents.id"],
        ),
        sa.ForeignKeyConstraint(
            ["chat_id"],
            ["suppgram_telegram_chats.telegram_chat_id"],
        ),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["suppgram_conversations.id"],
        ),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["suppgram_customers.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("suppgram_telegram_messages")
    op.drop_table("suppgram_conversation_tag_associations")
    op.drop_table("suppgram_conversation_messages")
    op.drop_table("suppgram_conversations")
    op.drop_table("suppgram_workplaces")
    op.drop_table("suppgram_tags")
    op.drop_table("suppgram_telegram_chats")
    op.drop_table("suppgram_customers")
    op.drop_table("suppgram_agents")
    # ### end Alembic commands ###
