"""Add task-scoped static web project previews.

Revision ID: 0036
Revises: 0035
Create Date: 2026-08-21
"""
from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision: str = "0036"
down_revision: str | None = "0035"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    op.create_table(
        "web_previews",
        sa.Column("preview_id", postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column(
            "user_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("users.user_id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "task_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("tasks.task_id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("root_path", sa.Text(), nullable=False),
        sa.Column("entry_path", sa.Text(), nullable=False, server_default="index.html"),
        sa.Column("name", sa.Text(), nullable=False, server_default="网页项目预览"),
        sa.Column("spa_fallback", sa.Boolean(), nullable=False, server_default=sa.true()),
        sa.Column("status", sa.Text(), nullable=False, server_default="active"),
        sa.Column("file_count", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("size_bytes", sa.BigInteger(), nullable=False, server_default="0"),
        sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
    )
    op.create_index("ix_web_previews_user_status", "web_previews", ["user_id", "status"])
    op.create_index("ix_web_previews_task_created", "web_previews", ["task_id", "created_at"])


def downgrade() -> None:
    op.drop_index("ix_web_previews_task_created", table_name="web_previews")
    op.drop_index("ix_web_previews_user_status", table_name="web_previews")
    op.drop_table("web_previews")
