<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Jalankan migrasi untuk menambahkan kolom verifikasi & OTP.
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            // Token untuk verifikasi via email
            if (!Schema::hasColumn('users', 'verification_token')) {
                $table->string('verification_token')->nullable();
            }

            // OTP dan waktu kedaluwarsa / pengiriman ulang
            if (!Schema::hasColumn('users', 'email_otp')) {
                $table->string('email_otp', 6)->nullable();
            }
            if (!Schema::hasColumn('users', 'otp_expires_at')) {
                $table->timestamp('otp_expires_at')->nullable();
            }
            if (!Schema::hasColumn('users', 'otp_last_sent_at')) {
                $table->timestamp('otp_last_sent_at')->nullable();
            }
        });
    }

    /**
     * Batalkan migrasi jika di-rollback.
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn([
                'verification_token',
                'email_otp',
                'otp_expires_at',
                'otp_last_sent_at',
            ]);
        });
    }
};

