"""scheduled_jobs 表(定时任务,DESIGN §8.5).

Revision ID: 0011
Revises: 0010
Create Date: 2026-06-18

新增独立表 scheduled_jobs —— 不碰现有 schema(公测兼容)。一行 = 一个"到点把
prompt 喂进 agent 主管线"的计划。守护循环(web/app.py lifespan)按 (enabled,
next_run_at) 索引扫到点 job 触发。详 DESIGN §8.5 / core/storage/models.py。
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import JSONB, UUID as PG_UUID


revision: str = "0011"
down_revision: Union[str, None] = "0010"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "scheduled_jobs",
        sa.Column("job_id", PG_UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "user_id", PG_UUID(as_uuid=True),
            sa.ForeignKey("users.user_id", ondelete="CASCADE"), nullable=False,
        ),
        sa.Column("name", sa.Text(), nullable=False),
        sa.Column("prompt", sa.Text(), nullable=False),
        sa.Column("cron", sa.Text(), nullable=False),
        sa.Column("tz", sa.Text(), nullable=False, server_default="Asia/Shanghai"),
        sa.Column("mode", sa.Text(), nullable=False, server_default="isolated"),
        sa.Column(
            "bound_task_id", PG_UUID(as_uuid=True),
            sa.ForeignKey("tasks.task_id", ondelete="SET NULL"), nullable=True,
        ),
        sa.Column("skill", sa.Text(), nullable=False, server_default=""),
        sa.Column("model_profile", sa.Text(), nullable=False, server_default=""),
        sa.Column("notify", JSONB(), nullable=True),
        sa.Column("enabled", sa.Boolean(), nullable=False, server_default="true"),
        sa.Column("timeout_seconds", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("next_run_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("last_run_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("last_status", sa.Text(), nullable=True),
        sa.Column("last_error", sa.Text(), nullable=True),
        sa.Column("last_task_id", PG_UUID(as_uuid=True), nullable=True),
        sa.Column("consecutive_failures", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("run_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    # 守护循环 due 扫描热路径:WHERE enabled AND deleted_at IS NULL AND next_run_at<=now()
    op.create_index(
        "ix_scheduled_jobs_due", "scheduled_jobs", ["enabled", "next_run_at"],
    )
    # 用户列出自己的 job
    op.create_index(
        "ix_scheduled_jobs_user", "scheduled_jobs", ["user_id"],
    )


def downgrade() -> None:
    op.drop_index("ix_scheduled_jobs_user", table_name="scheduled_jobs")
    op.drop_index("ix_scheduled_jobs_due", table_name="scheduled_jobs")
    op.drop_table("scheduled_jobs")
