<?php

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

class AddCodeToPaymentMethodsAndInsertData extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasColumn('payment_methods', 'code')) {
            Schema::table('payment_methods', function (Blueprint $table) {
                $table->string('code', 10)->nullable()->after('type');
            });
        }

        $typeValue = 'ALL';

        $paymentMethods = [
            [
                'bank_id' => null,
                'name' => 'Contanti',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP01'
            ],
            [
                'bank_id' => null,
                'name' => 'Assegno',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP02'
            ],
            [
                'bank_id' => null,
                'name' => 'Assegno circolare',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP03'
            ],
            [
                'bank_id' => null,
                'name' => 'Contanti presso Tesoreria',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP04'
            ],
            [
                'bank_id' => null,
                'name' => 'Bonifico',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP05'
            ],
            [
                'bank_id' => null,
                'name' => 'Vaglia cambiario',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP06'
            ],
            [
                'bank_id' => null,
                'name' => 'Bollettino bancario',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP07'
            ],
            [
                'bank_id' => null,
                'name' => 'Carta di credito',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP08'
            ],
            [
                'bank_id' => null,
                'name' => 'RID',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP09'
            ],
            [
                'bank_id' => null,
                'name' => 'RID utenze',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP10'
            ],
            [
                'bank_id' => null,
                'name' => 'RID veloce',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP11'
            ],
            [
                'bank_id' => null,
                'name' => 'Riba',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP12'
            ],
            [
                'bank_id' => null,
                'name' => 'MAV',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP13'
            ],
            [
                'bank_id' => null,
                'name' => 'Quietanza erario stato',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP14'
            ],
            [
                'bank_id' => null,
                'name' => 'Giroconto su conti di contabilità speciale',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP15'
            ],
            [
                'bank_id' => null,
                'name' => 'Domiciliazione bancaria',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP16'
            ],
            [
                'bank_id' => null,
                'name' => 'Domiciliazione postale',
                'money' => 0,
                'enabled' => 1,
                'deleted_at' => null,
                'created_at' => now(),
                'updated_at' => now(),
                'corrispettivo_fiscale' => null,
                'type' => $typeValue,
                'code' => 'MP17'
            ],
        ];

        foreach ($paymentMethods as $method) {
            $exists = DB::table('payment_methods')->where('code', $method['code'])->exists();

            if ($exists) {
                DB::table('payment_methods')
                    ->where('code', $method['code'])
                    ->update($method);
            } else {
                DB::table('payment_methods')->insert($method);
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (Schema::hasColumn('payment_methods', 'code')) {
            Schema::table('payment_methods', function (Blueprint $table) {
                $table->dropColumn('code');
            });
        }


    }
}
