"""add video_projects table

Revision ID: 91e21ecaef23
Revises: 080398045849
Create Date: 2025-10-26 14:49:28.048658

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import sqlite
from airunner.utils.db import safe_alter_column, add_table, drop_table
from airunner.components.llm.data.llm_generator_settings import (
    LLMGeneratorSettings,
)
from airunner.components.video.data.video_project import VideoProject

# revision identifiers, used by Alembic.
revision: str = "91e21ecaef23"
down_revision: Union[str, None] = "080398045849"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Use add_table helper which checks if table exists first
    add_table(VideoProject)

    # Use drop_table helper which checks if table exists first
    drop_table(table_name="fine_tuned_models")

    # Use safe_alter_column helper for SQLite compatibility
    safe_alter_column(
        LLMGeneratorSettings,
        "auto_extract_knowledge",
        existing_type=sa.BOOLEAN(),
        nullable=True,
        existing_server_default=sa.text("'1'"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Use safe_alter_column helper for SQLite compatibility
    safe_alter_column(
        LLMGeneratorSettings,
        "auto_extract_knowledge",
        existing_type=sa.BOOLEAN(),
        nullable=False,
        existing_server_default=sa.text("'1'"),
    )
    op.create_table(
        "fine_tuned_models",
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.Column("name", sa.VARCHAR(), nullable=False),
        sa.Column("adapter_path", sa.VARCHAR(), nullable=True),
        sa.Column("date_added", sa.DATETIME(), nullable=True),
        sa.Column("last_trained", sa.DATETIME(), nullable=True),
        sa.Column("files_used", sqlite.JSON(), nullable=True),
        sa.Column("settings", sqlite.JSON(), nullable=True),
        sa.Column("tags", sqlite.JSON(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    # Use drop_table helper which checks if table exists first
    drop_table(VideoProject)
    # ### end Alembic commands ###
