<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('members', function (Blueprint $table) {
            // first check if old column exists
            if (Schema::hasColumn('members', 'finger_id')) {
                $table->string('fingerprint_id')->nullable()->after('package_type');
            }
        });

        // copy data from old column
        \DB::statement('UPDATE members SET fingerprint_id = finger_id');

        // drop the old column
        Schema::table('members', function (Blueprint $table) {
            if (Schema::hasColumn('members', 'finger_id')) {
                $table->dropColumn('finger_id');
            }
        });
    }

    public function down(): void
    {
        Schema::table('members', function (Blueprint $table) {
            $table->string('finger_id')->nullable()->after('package_type');
        });

        \DB::statement('UPDATE members SET finger_id = fingerprint_id');

        Schema::table('members', function (Blueprint $table) {
            $table->dropColumn('fingerprint_id');
        });
    }
};

