import FluentSQLiteDriver

struct UserTableMigration: Migration {

    func prepare(on database: FluentSQLiteDriver.Database) -> EventLoopFuture<Void> {
        let one = database.schema(User.schema)
            .id()
            .field(User.Key.name.key, .string, .required)
            .field(User.Key.hash.key, .string, .required)
            .field(User.Key.points.key, .int, .required)
            .field(User.Key.table.key, .uuid, .references(Table.schema, Table.Key.id.key))
            .create()
        let two = database.enum(Table.Key.language.rawValue)
            .case(SupportedLanguage.german.rawValue)
            .case(SupportedLanguage.english.rawValue)
            .create()

        let three = database.enum(Table.Key.language.rawValue).read().flatMap { lang in
            database.schema(Table.schema)
                .id()
                .field(Table.Key.name.key, .string, .required)
                .field(Table.Key.isPublic.key, .bool, .required)
                .field(Table.Key.language.key, lang, .required)
                .create()
        }
        return one.and(two).and(three).map { _ in }
    }

    func revert(on database: FluentSQLiteDriver.Database) -> EventLoopFuture<Void> {
        database.eventLoop.makeCompletedFuture(.success(()))
    }
}
