<?php

namespace Kanakku\Listeners\Updates\v3;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Kanakku\Models\Setting;
use Kanakku\Models\Unit;
use Kanakku\Models\PaymentMethod;
use Kanakku\Models\Currency;
use Kanakku\Models\Payment;
use Kanakku\Models\Item;
use Kanakku\Models\User;
use Kanakku\Listeners\Updates\Listener;
use Illuminate\Database\Schema\Blueprint;

class Version300 extends Listener
{
    const VERSION = '3.0.0';

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        if ($this->isListenerFired($event)) {
            return;
        }

        $this->changeMigrations();

        $this->addSeederData();

        $this->databaseChanges();

        $this->changeMigrations(true);

        Setting::setSetting('version', static::VERSION);
    }

    public function changeMigrations($removeColumn = false)
    {
        if ($removeColumn) {
            \Schema::table('items', function (Blueprint $table) {
                $table->dropColumn('unit');
            });

            \Schema::table('payments', function (Blueprint $table) {
                $table->dropColumn('payment_mode');
            });

            return true;
        }

        \Schema::create('units', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('company_id')->unsigned()->nullable();
            $table->foreign('company_id')->references('id')->on('companies');
            $table->timestamps();
        });

        \Schema::table('items', function (Blueprint $table) {
            $table->integer('unit_id')->unsigned()->nullable();
            $table->foreign('unit_id')->references('id')->on('units')->onDelete('cascade');
        });

        \Schema::create('payment_methods', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('company_id')->unsigned()->nullable();
            $table->foreign('company_id')->references('id')->on('companies');
            $table->timestamps();
        });

        \Schema::table('payments', function (Blueprint $table) {
            $table->string('unique_hash')->nullable();
            $table->integer('payment_method_id')->unsigned()->nullable();
            $table->foreign('payment_method_id')->references('id')->on('payment_methods')->onDelete('cascade');
        });

        return true;
    }

    public function addSeederData()
    {
        $company_id = User::where('role', 'admin')->first()->company_id;

        Unit::create(['name' => 'box', 'company_id' => $company_id]);
        Unit::create(['name' => 'cm', 'company_id' => $company_id]);
        Unit::create(['name' => 'dz', 'company_id' => $company_id]);
        Unit::create(['name' => 'ft', 'company_id' => $company_id]);
        Unit::create(['name' => 'g', 'company_id' => $company_id]);
        Unit::create(['name' => 'in', 'company_id' => $company_id]);
        Unit::create(['name' => 'kg', 'company_id' => $company_id]);
        Unit::create(['name' => 'km', 'company_id' => $company_id]);
        Unit::create(['name' => 'lb', 'company_id' => $company_id]);
        Unit::create(['name' => 'mg', 'company_id' => $company_id]);
        Unit::create(['name' => 'pc', 'company_id' => $company_id]);

        PaymentMethod::create(['name' => 'Cash', 'company_id' => $company_id]);
        PaymentMethod::create(['name' => 'Check', 'company_id' => $company_id]);
        PaymentMethod::create(['name' => 'Credit Card', 'company_id' => $company_id]);
        PaymentMethod::create(['name' => 'Bank Transfer', 'company_id' => $company_id]);

        Currency::create([
            'name' => 'Serbian Dinar',
            'code' => 'RSD',
            'symbol' => 'RSD',
            'precision' => '2',
            'thousand_separator' => '.',
            'decimal_separator' => ','
        ]);
    }

    public function databaseChanges()
    {
        $payments = Payment::all();

        if ($payments) {
            foreach ($payments as $payment) {
                $payment->unique_hash = str_random(60);
                $payment->save();

                $paymentMethod = PaymentMethod::where('name', $payment->payment_mode)
                    ->first();

                if ($paymentMethod) {
                    $payment->payment_method_id = $paymentMethod->id;
                    $payment->save();
                }
            }
        }

        $items = Item::all();

        if ($items) {
            foreach ($items as $item) {
                $unit = Unit::where('name', $item->unit)
                    ->first();

                if ($unit) {
                    $item->unit_id = $unit->id;
                    $item->save();
                }
            }
        }
    }
}
