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(Repositories::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Repositories::Id)
                            .integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(Repositories::Uuid)
                            .uuid()
                            .not_null()
                            .unique_key(),
                    )
                    .col(ColumnDef::new(Repositories::OwnerId).integer().not_null())
                    .col(ColumnDef::new(Repositories::Slug).string_len(64).not_null())
                    .col(ColumnDef::new(Repositories::Description).string())
                    .col(
                        ColumnDef::new(Repositories::Visibility)
                            .string_len(16)
                            .not_null()
                            .default("public"),
                    )
                    .col(
                        ColumnDef::new(Repositories::DefaultBranch)
                            .string_len(255)
                            .not_null()
                            .default("main"),
                    )
                    .col(
                        ColumnDef::new(Repositories::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp())
                            .not_null(),
                    )
                    .col(ColumnDef::new(Repositories::DeletedAt).timestamp_with_time_zone())
                    // A repository is named by its owner and its slug, so the
                    // pair is what has to be unique rather than either alone:
                    // two users can both have a `notes` repository.
                    .index(
                        Index::create()
                            .name("index_repositories_on_owner_id_and_slug")
                            .col(Repositories::OwnerId)
                            .col(Repositories::Slug)
                            .unique(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_repositories_owner_id")
                            .from(Repositories::Table, Repositories::OwnerId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

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

/// The repository table, as this migration knows it.
#[derive(Iden)]
pub enum Repositories {
    Table,
    Id,
    Uuid,
    OwnerId,
    Slug,
    Description,
    Visibility,
    DefaultBranch,
    CreatedAt,
    DeletedAt,
}

/// The users table, for the foreign key. Only the columns this migration
/// names are listed; the rest belong to the migration that created it.
#[derive(Iden)]
pub enum Users {
    Table,
    Id,
}
