use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let table = Alias::new("site_settings");

        if !manager.has_column("site_settings", "ai_enabled").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(
                            ColumnDef::new(Alias::new("ai_enabled"))
                                .boolean()
                                .null()
                                .default(false),
                        )
                        .to_owned(),
                )
                .await?;
        }

        if !manager.has_column("site_settings", "ai_provider").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_provider")).string().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager.has_column("site_settings", "ai_api_base").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_api_base")).string().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager.has_column("site_settings", "ai_api_key").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_api_key")).text().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager.has_column("site_settings", "ai_chat_model").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_chat_model")).string().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager
            .has_column("site_settings", "ai_embedding_model")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(
                            ColumnDef::new(Alias::new("ai_embedding_model"))
                                .string()
                                .null(),
                        )
                        .to_owned(),
                )
                .await?;
        }

        if !manager
            .has_column("site_settings", "ai_system_prompt")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_system_prompt")).text().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager.has_column("site_settings", "ai_top_k").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_top_k")).integer().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager.has_column("site_settings", "ai_chunk_size").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(table.clone())
                        .add_column(ColumnDef::new(Alias::new("ai_chunk_size")).integer().null())
                        .to_owned(),
                )
                .await?;
        }

        if !manager
            .has_column("site_settings", "ai_last_indexed_at")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(table)
                        .add_column(
                            ColumnDef::new(Alias::new("ai_last_indexed_at"))
                                .timestamp_with_time_zone()
                                .null(),
                        )
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let table = Alias::new("site_settings");

        for column in [
            "ai_last_indexed_at",
            "ai_chunk_size",
            "ai_top_k",
            "ai_system_prompt",
            "ai_embedding_model",
            "ai_chat_model",
            "ai_api_key",
            "ai_api_base",
            "ai_provider",
            "ai_enabled",
        ] {
            if manager.has_column("site_settings", column).await? {
                manager
                    .alter_table(
                        Table::alter()
                            .table(table.clone())
                            .drop_column(Alias::new(column))
                            .to_owned(),
                    )
                    .await?;
            }
        }

        Ok(())
    }
}
