<?php

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

class DeleteAndRenameFieldsFromContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('contacts', function (Blueprint $table) {

            // salutation and firstname are inverted because they filled salutation as firstname
            // middlename and initials are inverted because they filled initials as middlename
            $table->renameColumn('salutation_cp', 'firstname');
            $table->renameColumn('firstname_cp', 'salutation');
            $table->renameColumn('initials_cp', 'middlename');
            $table->renameColumn('middlename_cp', 'initials');

            $table->renameColumn('lastname_cp', 'lastname');
            $table->renameColumn('email_cp', 'email');
            $table->renameColumn('email2_cp', 'email2');
            $table->renameColumn('phone_cp', 'phone');
            $table->renameColumn('job_title_cp', 'job_title');
            $table->renameColumn('email3_cp', 'email3');
            $table->renameColumn('mobile_cp', 'mobile');

            $table->dropColumn('salutation_cc');
            $table->dropColumn('initials_cc');
            $table->dropColumn('lastname_cc');
            // $table->dropColumn('email_cc'); // rename to email2?
            $table->dropColumn('email2_cc');
            $table->dropColumn('email3_cc');
            $table->dropColumn('phone_cc');
            $table->dropColumn('firstname_cc');
            $table->dropColumn('middlename_cc');
            $table->dropColumn('job_title_cc');
            $table->dropColumn('mobile_cc');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('contacts', function (Blueprint $table) {
            $table->renameColumn('salutation', 'salutation_cp');
            $table->renameColumn('initials', 'initials_cp');
            $table->renameColumn('lastname', 'lastname_cp');
            $table->renameColumn('email', 'email_cp');
            $table->renameColumn('email2', 'email2_cp');
            $table->renameColumn('phone', 'phone_cp');
            $table->renameColumn('firstname', 'firstname_cp');
            $table->renameColumn('middlename', 'middlename_cp');
            $table->renameColumn('job_title', 'job_title_cp');
            $table->renameColumn('email3', 'email3_cp');
            $table->renameColumn('mobile', 'mobile_cp');

            $table->string('salutation_cc')->nullable();
            $table->string('initials_cc')->nullable();
            $table->string('lastname_cc')->nullable();
            // $table->string('email_cc')->nullable();
            $table->string('email2_cc')->nullable();
            $table->string('phone_cc')->nullable();
            $table->string('firstname_cc')->nullable();
            $table->string('middlename_cc')->nullable();
            $table->string('job_title_cc')->nullable();
            $table->string('email3_cc')->nullable();
            $table->string('mobile_cc')->nullable();

        });
    }
}
