"""add chatapi async generation mode

Revision ID: c310d7193c7f
Revises: 66b40a9e2d63
Create Date: 2026-05-26 14:52:20.327046
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'c310d7193c7f'
down_revision: Union[str, None] = '66b40a9e2d63'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

def upgrade() -> None:
    op.create_table(
        "chat_generation_tasks",
        sa.Column("id", sa.String(length=32), primary_key=True),
        sa.Column("user_id", sa.String(length=32), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("original_prompt", sa.Text(), nullable=False),
        sa.Column("optimized_prompt", sa.Text(), nullable=True),
        sa.Column("gen_type", sa.String(length=16), server_default="video", nullable=False),
        sa.Column("duration", sa.Integer(), nullable=True),
        sa.Column("aspect_ratio", sa.String(length=8), nullable=True),
        sa.Column("resolution", sa.String(length=8), nullable=True),
        sa.Column("image_size", sa.String(length=16), nullable=True),
        sa.Column("image_proportion", sa.String(length=8), nullable=True),
        sa.Column("image_px", sa.String(length=16), nullable=True),
        sa.Column("status", sa.String(length=32), server_default="generating", nullable=False),
        sa.Column("pipeline_stage", sa.String(length=32), nullable=True),
        sa.Column("generation_mode", sa.String(length=32), server_default="chatapi_async", nullable=False),
        sa.Column("media_references", sa.Text(), nullable=True),
        sa.Column("provider_task_id", sa.String(length=128), nullable=True),
        sa.Column("seedance_task_id", sa.String(length=128), nullable=True),
        sa.Column("remote_result_url", sa.Text(), nullable=True),
        sa.Column("image_url", sa.String(length=512), nullable=True),
        sa.Column("video_url", sa.String(length=512), nullable=True),
        sa.Column("engine_id", sa.String(length=32), nullable=True),
        sa.Column("engine_snapshot_json", sa.Text(), nullable=True),
        sa.Column("provider_response_json", sa.Text(), nullable=True),
        sa.Column("credits_cost", sa.Float(), server_default="0", nullable=False),
        sa.Column("text_credits_cost", sa.Float(), server_default="0", nullable=False),
        sa.Column("text_tokens_used", sa.Integer(), server_default="0", nullable=False),
        sa.Column("video_tokens_used", sa.Integer(), server_default="0", nullable=False),
        sa.Column("image_tokens_used", sa.Integer(), server_default="0", nullable=False),
        sa.Column("retry_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("poll_count", sa.Integer(), server_default="0", nullable=False),
        sa.Column("last_poll_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deadline_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("generated_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("idempotency_key", sa.String(length=64), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=True),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=True),
    )
    op.create_index("ix_chat_generation_tasks_user_id", "chat_generation_tasks", ["user_id"])
    op.create_index("ix_chat_generation_tasks_gen_type", "chat_generation_tasks", ["gen_type"])
    op.create_index("ix_chat_generation_tasks_status", "chat_generation_tasks", ["status"])
    op.create_index("ix_chat_generation_tasks_pipeline_stage", "chat_generation_tasks", ["pipeline_stage"])
    op.create_index("ix_chat_generation_tasks_generation_mode", "chat_generation_tasks", ["generation_mode"])
    op.create_index("ix_chat_generation_tasks_provider_task_id", "chat_generation_tasks", ["provider_task_id"])
    op.create_index("ix_chat_generation_tasks_seedance_task_id", "chat_generation_tasks", ["seedance_task_id"])
    op.create_index("ix_chat_generation_tasks_engine_id", "chat_generation_tasks", ["engine_id"])
    op.create_index("ix_chat_generation_tasks_idempotency_key", "chat_generation_tasks", ["idempotency_key"])

    op.create_table(
        "chat_generation_task_events",
        sa.Column("id", sa.String(length=32), primary_key=True),
        sa.Column("task_id", sa.String(length=32), sa.ForeignKey("chat_generation_tasks.id", ondelete="CASCADE"), nullable=False),
        sa.Column("generation_mode", sa.String(length=32), nullable=True),
        sa.Column("event_type", sa.String(length=64), nullable=False),
        sa.Column("from_status", sa.String(length=32), nullable=True),
        sa.Column("to_status", sa.String(length=32), nullable=True),
        sa.Column("from_stage", sa.String(length=32), nullable=True),
        sa.Column("to_stage", sa.String(length=32), nullable=True),
        sa.Column("message", sa.String(length=512), nullable=True),
        sa.Column("detail_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=True),
    )
    op.create_index("ix_chat_generation_task_events_task_id", "chat_generation_task_events", ["task_id"])
    op.create_index("ix_chat_generation_task_events_generation_mode", "chat_generation_task_events", ["generation_mode"])
    op.create_index("ix_chat_generation_task_events_event_type", "chat_generation_task_events", ["event_type"])
    op.create_index("ix_chat_generation_task_events_created_at", "chat_generation_task_events", ["created_at"])

    op.create_table(
        "chat_provider_call_logs",
        sa.Column("id", sa.String(length=32), primary_key=True),
        sa.Column("task_id", sa.String(length=32), sa.ForeignKey("chat_generation_tasks.id", ondelete="CASCADE"), nullable=False),
        sa.Column("generation_mode", sa.String(length=32), nullable=True),
        sa.Column("provider", sa.String(length=64), nullable=True),
        sa.Column("api_type", sa.String(length=64), nullable=False),
        sa.Column("model", sa.String(length=128), nullable=True),
        sa.Column("engine_id", sa.String(length=32), nullable=True),
        sa.Column("status", sa.String(length=32), nullable=False),
        sa.Column("latency_ms", sa.Integer(), nullable=True),
        sa.Column("http_status", sa.Integer(), nullable=True),
        sa.Column("provider_task_id", sa.String(length=128), nullable=True),
        sa.Column("request_hash", sa.String(length=64), nullable=True),
        sa.Column("response_hash", sa.String(length=64), nullable=True),
        sa.Column("request_excerpt", sa.Text(), nullable=True),
        sa.Column("response_excerpt", sa.Text(), nullable=True),
        sa.Column("prompt_tokens", sa.Integer(), server_default="0", nullable=False),
        sa.Column("completion_tokens", sa.Integer(), server_default="0", nullable=False),
        sa.Column("total_tokens", sa.Integer(), server_default="0", nullable=False),
        sa.Column("error_code", sa.String(length=128), nullable=True),
        sa.Column("error_message", sa.Text(), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=True),
    )
    op.create_index("ix_chat_provider_call_logs_task_id", "chat_provider_call_logs", ["task_id"])
    op.create_index("ix_chat_provider_call_logs_generation_mode", "chat_provider_call_logs", ["generation_mode"])
    op.create_index("ix_chat_provider_call_logs_provider", "chat_provider_call_logs", ["provider"])
    op.create_index("ix_chat_provider_call_logs_api_type", "chat_provider_call_logs", ["api_type"])
    op.create_index("ix_chat_provider_call_logs_engine_id", "chat_provider_call_logs", ["engine_id"])
    op.create_index("ix_chat_provider_call_logs_status", "chat_provider_call_logs", ["status"])
    op.create_index("ix_chat_provider_call_logs_provider_task_id", "chat_provider_call_logs", ["provider_task_id"])
    op.create_index("ix_chat_provider_call_logs_created_at", "chat_provider_call_logs", ["created_at"])


def downgrade() -> None:
    op.drop_index("ix_chat_provider_call_logs_created_at", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_provider_task_id", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_status", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_engine_id", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_api_type", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_provider", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_generation_mode", table_name="chat_provider_call_logs")
    op.drop_index("ix_chat_provider_call_logs_task_id", table_name="chat_provider_call_logs")
    op.drop_table("chat_provider_call_logs")

    op.drop_index("ix_chat_generation_task_events_created_at", table_name="chat_generation_task_events")
    op.drop_index("ix_chat_generation_task_events_event_type", table_name="chat_generation_task_events")
    op.drop_index("ix_chat_generation_task_events_generation_mode", table_name="chat_generation_task_events")
    op.drop_index("ix_chat_generation_task_events_task_id", table_name="chat_generation_task_events")
    op.drop_table("chat_generation_task_events")

    op.drop_index("ix_chat_generation_tasks_idempotency_key", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_engine_id", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_seedance_task_id", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_provider_task_id", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_generation_mode", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_pipeline_stage", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_status", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_gen_type", table_name="chat_generation_tasks")
    op.drop_index("ix_chat_generation_tasks_user_id", table_name="chat_generation_tasks")
    op.drop_table("chat_generation_tasks")