"""phase10 research+travel: research/trip/rag 14 tables

Revision ID: p10d4e5f6a7b8
Revises: p9c3d4e5f6a7
Create Date: 2026-06-14 03:00:00.000000

"""
from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel
from alembic import op

revision: str = "p10d4e5f6a7b8"
down_revision: Union[str, Sequence[str], None] = "p9c3d4e5f6a7"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

S = sqlmodel.sql.sqltypes.AutoString
J = sa.JSON


def upgrade() -> None:
    op.create_table(
        "research_collection",
        sa.Column("id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("n", sa.Integer(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "research_source",
        sa.Column("id", S(), nullable=False),
        sa.Column("kind", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("from_label", S(), nullable=False),
        sa.Column("col", S(), nullable=True),
        sa.Column("learned", sa.Boolean(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["col"], ["research_collection.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "research_report",
        sa.Column("id", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("asked", S(), nullable=False),
        sa.Column("meta", S(), nullable=False),
        sa.Column("synthesis", S(), nullable=False),
        sa.Column("note", S(), nullable=False),
        sa.Column("counts", J(), nullable=True),
        sa.Column("cross", J(), nullable=True),
        sa.Column("collection_id", S(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["collection_id"], ["research_collection.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "knowledge_qa",
        sa.Column("id", S(), nullable=False),
        sa.Column("q", S(), nullable=False),
        sa.Column("a", S(), nullable=False),
        sa.Column("refs", J(), nullable=True),
        sa.Column("collection_id", S(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["collection_id"], ["research_collection.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "research_chart",
        sa.Column("id", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("asked", S(), nullable=False),
        sa.Column("unit", S(), nullable=False),
        sa.Column("bars", J(), nullable=True),
        sa.Column("insight", S(), nullable=False),
        sa.Column("caution", S(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "rag_chunk",
        sa.Column("id", S(), nullable=False),
        sa.Column("source_id", S(), nullable=False),
        sa.Column("seq", sa.Integer(), nullable=False),
        sa.Column("text", S(), nullable=False),
        sa.Column("part_label", S(), nullable=False),
        sa.Column("embedding", J(), nullable=True),
        sa.Column("dim", sa.Integer(), nullable=False),
        sa.Column("model", S(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["source_id"], ["research_source.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip",
        sa.Column("id", S(), nullable=False),
        sa.Column("dday", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("dates", S(), nullable=False),
        sa.Column("purpose", S(), nullable=False),
        sa.Column("brief", S(), nullable=False),
        sa.Column("weather", J(), nullable=True),
        sa.Column("expense_budget", sa.Integer(), nullable=False),
        sa.Column("expense_planned", sa.Integer(), nullable=False),
        sa.Column("expense_note", S(), nullable=False),
        sa.Column("expense_rows", J(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip_route",
        sa.Column("id", S(), nullable=False),
        sa.Column("trip_id", S(), nullable=False),
        sa.Column("dir", S(), nullable=False),
        sa.Column("mode", S(), nullable=False),
        sa.Column("from_st", S(), nullable=False),
        sa.Column("ft", S(), nullable=False),
        sa.Column("to_st", S(), nullable=False),
        sa.Column("tt", S(), nullable=False),
        sa.Column("seat", S(), nullable=False),
        sa.Column("note", S(), nullable=False),
        sa.ForeignKeyConstraint(["trip_id"], ["trip.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip_stay",
        sa.Column("id", S(), nullable=False),
        sa.Column("trip_id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("desc", S(), nullable=False),
        sa.Column("conf", S(), nullable=False),
        sa.ForeignKeyConstraint(["trip_id"], ["trip.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip_prep",
        sa.Column("id", S(), nullable=False),
        sa.Column("trip_id", S(), nullable=False),
        sa.Column("text", S(), nullable=False),
        sa.Column("state", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["trip_id"], ["trip.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip_day",
        sa.Column("id", S(), nullable=False),
        sa.Column("trip_id", S(), nullable=False),
        sa.Column("tab", S(), nullable=False),
        sa.Column("items", J(), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["trip_id"], ["trip.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip_checklist",
        sa.Column("id", S(), nullable=False),
        sa.Column("trip_id", S(), nullable=False),
        sa.Column("group", S(), nullable=False),
        sa.Column("text", S(), nullable=False),
        sa.Column("auto", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["trip_id"], ["trip.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "saved_trip",
        sa.Column("id", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("tag", S(), nullable=False),
        sa.Column("note", S(), nullable=False),
        sa.Column("now", S(), nullable=False),
        sa.Column("delta", S(), nullable=False),
        sa.Column("down", sa.Boolean(), nullable=False),
        sa.Column("watch", sa.Boolean(), nullable=False),
        sa.Column("hint", S(), nullable=False),
        sa.Column("spark", J(), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "trip_plan",
        sa.Column("id", S(), nullable=False),
        sa.Column("example", S(), nullable=False),
        sa.Column("idx", sa.Integer(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("meta", S(), nullable=False),
        sa.Column("summary", S(), nullable=False),
        sa.Column("weather", S(), nullable=False),
        sa.Column("transport", J(), nullable=True),
        sa.Column("stay", J(), nullable=True),
        sa.Column("days", J(), nullable=True),
        sa.Column("budget", J(), nullable=True),
        sa.Column("checklist", J(), nullable=True),
        sa.Column("sources", J(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )


def downgrade() -> None:
    for t in (
        "trip_plan", "saved_trip", "trip_checklist", "trip_day", "trip_prep",
        "trip_stay", "trip_route", "trip", "rag_chunk", "research_chart",
        "knowledge_qa", "research_report", "research_source", "research_collection",
    ):
        op.drop_table(t)
