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> {
        manager
            .create_table(
                Table::create()
                    .table(DirectMessages::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(DirectMessages::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(DirectMessages::SenderUserId).uuid().not_null())
                    .col(ColumnDef::new(DirectMessages::RecipientUserId).uuid().not_null())
                    .col(ColumnDef::new(DirectMessages::Body).text().not_null())
                    .col(
                        ColumnDef::new(DirectMessages::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_direct_messages_sender_user")
                            .from(DirectMessages::Table, DirectMessages::SenderUserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_direct_messages_recipient_user")
                            .from(DirectMessages::Table, DirectMessages::RecipientUserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_direct_messages_sender_created_at")
                    .table(DirectMessages::Table)
                    .col(DirectMessages::SenderUserId)
                    .col(DirectMessages::CreatedAt)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_direct_messages_recipient_created_at")
                    .table(DirectMessages::Table)
                    .col(DirectMessages::RecipientUserId)
                    .col(DirectMessages::CreatedAt)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(DirectMessages::Table).to_owned())
            .await?;

        Ok(())
    }
}

#[derive(DeriveIden)]
enum DirectMessages {
    Table,
    Id,
    SenderUserId,
    RecipientUserId,
    Body,
    CreatedAt,
}

#[derive(DeriveIden)]
enum Users {
    Table,
    Id,
}
