use sea_orm_migration::prelude::*;

use crate::m20220101_000002_create_enums::{ChannelType, NotificationStatus};

#[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(DeliveryLog::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(DeliveryLog::Id)
                            .uuid()
                            .primary_key()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(DeliveryLog::NotificationId)
                            .uuid()
                            .not_null(),
                    )
                    .col(ColumnDef::new(DeliveryLog::AttemptNo).integer().not_null())
                    .col(
                        ColumnDef::new(DeliveryLog::Channel)
                            .custom(ChannelType::Type)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(DeliveryLog::Status)
                            .custom(NotificationStatus::Type)
                            .not_null(),
                    )
                    .col(ColumnDef::new(DeliveryLog::ResponseMeta).json_binary().null())
                    .col(
                        ColumnDef::new(DeliveryLog::CreatedAt)
                            .timestamp()
                            .not_null()
                            .extra("DEFAULT NOW()"),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_delivery_log_notification")
                            .from(DeliveryLog::Table, DeliveryLog::NotificationId)
                            .to(Notification::Table, Notification::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        // Create index
        manager
            .create_index(
                Index::create()
                    .name("IDX_delivery_log_notification_id")
                    .table(DeliveryLog::Table)
                    .col(DeliveryLog::NotificationId)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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

#[derive(DeriveIden)]
enum DeliveryLog {
    Table,
    Id,
    NotificationId,
    AttemptNo,
    Channel,
    Status,
    ResponseMeta,
    CreatedAt,
}

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