<?php

use App\Packages\User\Models\User;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UserPhoneHash extends Migration
{
    const TABLE_USER = 'table_user';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table(static::TABLE_USER, function (Blueprint $table) {

            $table->string('phone_hash')->nullable();

            $table->index('phone_hash');
        });

        $users = User::repository()->all();
        $users->each(function(User $user) {
            $user->phone_hash = phoneHash($user->phone_code, $user->phone_number);
            $user->save();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table(static::TABLE_USER, function (Blueprint $table) {

            $table->dropIndex(['phone_hash']);
            $table->dropColumn('phone_hash');
        });
    }
}
