"""phase11 life-care: 11 tables (connector/health/finance/knowledge)

Revision ID: p11e5f6a7b8c9
Revises: p10d4e5f6a7b8
Create Date: 2026-06-14 04:00:00.000000

"""
from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel
from alembic import op

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

S = sqlmodel.sql.sqltypes.AutoString


def upgrade() -> None:
    op.create_table(
        "connector_source",
        sa.Column("id", S(), nullable=False),
        sa.Column("domain", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("kind", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("on", sa.Boolean(), nullable=False),
        sa.Column("last", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "health_ring",
        sa.Column("id", S(), nullable=False),
        sa.Column("label", S(), nullable=False),
        sa.Column("val", S(), nullable=False),
        sa.Column("unit", S(), nullable=False),
        sa.Column("goal", S(), nullable=False),
        sa.Column("pct", sa.Integer(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "health_vital",
        sa.Column("id", S(), nullable=False),
        sa.Column("label", S(), nullable=False),
        sa.Column("val", S(), nullable=False),
        sa.Column("unit", S(), nullable=False),
        sa.Column("trend", S(), nullable=False),
        sa.Column("note", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "sleep_night",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("total", S(), nullable=False),
        sa.Column("score", sa.Integer(), nullable=False),
        sa.Column("stages_json", S(), nullable=False),
        sa.Column("week_json", S(), nullable=False),
        sa.Column("note", S(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "health_habit",
        sa.Column("id", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("goal", S(), nullable=False),
        sa.Column("done", sa.Integer(), nullable=False),
        sa.Column("total", sa.Integer(), nullable=False),
        sa.Column("streak", sa.Integer(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("at", S(), nullable=False),
        sa.Column("automation_rule_id", S(), nullable=True),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["automation_rule_id"], ["automation_rule.id"]),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "finance_budget",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("spent", sa.Integer(), nullable=False),
        sa.Column("limit", sa.Integer(), nullable=False),
        sa.Column("pct", sa.Integer(), nullable=False),
        sa.Column("delta_pct", sa.Integer(), nullable=False),
        sa.Column("days_left", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "finance_category",
        sa.Column("id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("amt", sa.Integer(), nullable=False),
        sa.Column("pct", sa.Integer(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("over", sa.Boolean(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "finance_sub",
        sa.Column("id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("date", S(), nullable=False),
        sa.Column("in_days", sa.Integer(), nullable=False),
        sa.Column("amt", sa.Integer(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("note", S(), nullable=False),
        sa.Column("paused", sa.Boolean(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "finance_insight",
        sa.Column("id", S(), nullable=False),
        sa.Column("kind", S(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("body", S(), nullable=False),
        sa.Column("action", S(), nullable=False),
        sa.Column("action_ref", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "knowledge_collection",
        sa.Column("id", S(), nullable=False),
        sa.Column("name", S(), nullable=False),
        sa.Column("count", sa.Integer(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "knowledge_item",
        sa.Column("id", S(), nullable=False),
        sa.Column("type", S(), nullable=False),
        sa.Column("title", S(), nullable=False),
        sa.Column("src", S(), nullable=False),
        sa.Column("time", S(), nullable=False),
        sa.Column("tone", S(), nullable=False),
        sa.Column("icon", S(), nullable=False),
        sa.Column("tags_json", S(), nullable=False),
        sa.Column("excerpt", S(), nullable=False),
        sa.Column("ai", S(), nullable=False),
        sa.Column("collection_id", S(), nullable=True),
        sa.Column("connector_id", S(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("sort_order", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(["collection_id"], ["knowledge_collection.id"]),
        sa.ForeignKeyConstraint(["connector_id"], ["connector_source.id"]),
        sa.PrimaryKeyConstraint("id"),
    )


def downgrade() -> None:
    for t in (
        "knowledge_item", "knowledge_collection", "finance_insight", "finance_sub",
        "finance_category", "finance_budget", "health_habit", "sleep_night",
        "health_vital", "health_ring", "connector_source",
    ):
        op.drop_table(t)
