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(Invite::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Invite::Id).uuid().primary_key().not_null())
                    .col(
                        ColumnDef::new(Invite::Code)
                            .string()
                            .unique_key()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Invite::CreatorId).uuid().not_null())
                    .col(
                        ColumnDef::new(Invite::MaxUses)
                            .integer()
                            .not_null()
                            .default(5),
                    )
                    .col(
                        ColumnDef::new(Invite::UsedCount)
                            .integer()
                            .not_null()
                            .default(0),
                    )
                    .col(ColumnDef::new(Invite::ExpiresAt).timestamp().not_null())
                    .col(ColumnDef::new(Invite::RevokedAt).timestamp().null())
                    .col(
                        ColumnDef::new(Invite::CreatedAt)
                            .timestamp()
                            .not_null()
                            .extra("DEFAULT NOW()"),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_invite_creator")
                            .from(Invite::Table, Invite::CreatorId)
                            .to(User::Table, User::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        // Create indexes
        manager
            .create_index(
                Index::create()
                    .name("IDX_invite_creator_id")
                    .table(Invite::Table)
                    .col(Invite::CreatorId)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("IDX_invite_expires_at")
                    .table(Invite::Table)
                    .col(Invite::ExpiresAt)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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

#[derive(DeriveIden)]
enum Invite {
    Table,
    Id,
    Code,
    CreatorId,
    MaxUses,
    UsedCount,
    ExpiresAt,
    RevokedAt,
    CreatedAt,
}

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