<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table)
        {
            $table->string('normalized_name')->nullable()->after('name');
            $table->string('normalized_phone')->nullable()->after('phone');
        });
        DB::statement('UPDATE users SET normalized_name = LOWER(name)
            , normalized_phone = 
                REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE(
                                REPLACE(phone, " ", "")
                                , "-", "")
                            , "+",   "")
                        , "(", "")                 
                     , ")", "")
            ');
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table)
        {
            $table->dropColumn('normalized_name');
            $table->dropColumn('normalized_phone');
        });
    }
};
