"""initial_schema

Revision ID: 8af66d07fbf5
Revises: 0001_pgvector
Create Date: 2026-02-20 18:03:08.383537

"""

from collections.abc import Sequence

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

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "8af66d07fbf5"
down_revision: str | None = "0001_pgvector"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "user",
        sa.Column("clerk_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("full_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_clerk_id"), "user", ["clerk_id"], unique=True)
    op.create_index(op.f("ix_user_email"), "user", ["email"], unique=False)
    op.create_table(
        "restaurant",
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("slug", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("phone", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("timezone", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("subscription_plan", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("owner_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("settings", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_restaurant_owner_id"), "restaurant", ["owner_id"], unique=False)
    op.create_index(op.f("ix_restaurant_slug"), "restaurant", ["slug"], unique=True)
    op.create_table(
        "customer",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("phone", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("visit_count", sa.Integer(), nullable=False),
        sa.Column("no_show_count", sa.Integer(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["restaurant_id"],
            ["restaurant.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_customer_email"), "customer", ["email"], unique=False)
    op.create_index(op.f("ix_customer_phone"), "customer", ["phone"], unique=False)
    op.create_index(op.f("ix_customer_restaurant_id"), "customer", ["restaurant_id"], unique=False)
    op.create_table(
        "knowledge_document",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("content", sa.Text(), nullable=False),
        sa.Column("source", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("embedding", pgvector.sqlalchemy.vector.VECTOR(dim=1536), nullable=True),
        sa.Column("metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["restaurant_id"],
            ["restaurant.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_knowledge_document_restaurant_id"),
        "knowledge_document",
        ["restaurant_id"],
        unique=False,
    )
    op.create_table(
        "time_slot",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("day_of_week", sa.Integer(), nullable=False),
        sa.Column("start_time", sa.Time(), nullable=False),
        sa.Column("end_time", sa.Time(), nullable=False),
        sa.Column("max_covers", sa.Integer(), nullable=False),
        sa.Column("slot_duration_minutes", sa.Integer(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.ForeignKeyConstraint(
            ["restaurant_id"],
            ["restaurant.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_time_slot_restaurant_id"), "time_slot", ["restaurant_id"], unique=False
    )
    op.create_table(
        "conversation",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("channel", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("agent_type", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("summary", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("customer_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customer.id"],
        ),
        sa.ForeignKeyConstraint(
            ["restaurant_id"],
            ["restaurant.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_conversation_customer_id"), "conversation", ["customer_id"], unique=False
    )
    op.create_index(
        op.f("ix_conversation_restaurant_id"), "conversation", ["restaurant_id"], unique=False
    )
    op.create_table(
        "order",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("total_cents", sa.Integer(), nullable=False),
        sa.Column("mollie_payment_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("customer_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customer.id"],
        ),
        sa.ForeignKeyConstraint(
            ["restaurant_id"],
            ["restaurant.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_order_customer_id"), "order", ["customer_id"], unique=False)
    op.create_index(
        op.f("ix_order_mollie_payment_id"), "order", ["mollie_payment_id"], unique=False
    )
    op.create_index(op.f("ix_order_restaurant_id"), "order", ["restaurant_id"], unique=False)
    op.create_index(op.f("ix_order_status"), "order", ["status"], unique=False)
    op.create_table(
        "reservation",
        sa.Column("restaurant_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("guest_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("guest_email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("guest_phone", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("party_size", sa.Integer(), nullable=False),
        sa.Column("reserved_at", sa.DateTime(), nullable=False),
        sa.Column("notes", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("source", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("customer_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.ForeignKeyConstraint(
            ["customer_id"],
            ["customer.id"],
        ),
        sa.ForeignKeyConstraint(
            ["restaurant_id"],
            ["restaurant.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_reservation_customer_id"), "reservation", ["customer_id"], unique=False
    )
    op.create_index(
        op.f("ix_reservation_restaurant_id"), "reservation", ["restaurant_id"], unique=False
    )
    op.create_index(op.f("ix_reservation_status"), "reservation", ["status"], unique=False)
    op.create_table(
        "message",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("conversation_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("role", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("content", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("tool_calls", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("token_count", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["conversation_id"],
            ["conversation.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_message_conversation_id"), "message", ["conversation_id"], unique=False
    )
    op.create_table(
        "order_item",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("order_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("menu_item_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("quantity", sa.Integer(), nullable=False),
        sa.Column("unit_price_cents", sa.Integer(), nullable=False),
        sa.Column("options", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["order_id"],
            ["order.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_order_item_order_id"), "order_item", ["order_id"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_order_item_order_id"), table_name="order_item")
    op.drop_table("order_item")
    op.drop_index(op.f("ix_message_conversation_id"), table_name="message")
    op.drop_table("message")
    op.drop_index(op.f("ix_reservation_status"), table_name="reservation")
    op.drop_index(op.f("ix_reservation_restaurant_id"), table_name="reservation")
    op.drop_index(op.f("ix_reservation_customer_id"), table_name="reservation")
    op.drop_table("reservation")
    op.drop_index(op.f("ix_order_status"), table_name="order")
    op.drop_index(op.f("ix_order_restaurant_id"), table_name="order")
    op.drop_index(op.f("ix_order_mollie_payment_id"), table_name="order")
    op.drop_index(op.f("ix_order_customer_id"), table_name="order")
    op.drop_table("order")
    op.drop_index(op.f("ix_conversation_restaurant_id"), table_name="conversation")
    op.drop_index(op.f("ix_conversation_customer_id"), table_name="conversation")
    op.drop_table("conversation")
    op.drop_index(op.f("ix_time_slot_restaurant_id"), table_name="time_slot")
    op.drop_table("time_slot")
    op.drop_index(op.f("ix_knowledge_document_restaurant_id"), table_name="knowledge_document")
    op.drop_table("knowledge_document")
    op.drop_index(op.f("ix_customer_restaurant_id"), table_name="customer")
    op.drop_index(op.f("ix_customer_phone"), table_name="customer")
    op.drop_index(op.f("ix_customer_email"), table_name="customer")
    op.drop_table("customer")
    op.drop_index(op.f("ix_restaurant_slug"), table_name="restaurant")
    op.drop_index(op.f("ix_restaurant_owner_id"), table_name="restaurant")
    op.drop_table("restaurant")
    op.drop_index(op.f("ix_user_email"), table_name="user")
    op.drop_index(op.f("ix_user_clerk_id"), table_name="user")
    op.drop_table("user")
    # ### end Alembic commands ###
