<?php

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

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;

class CreateTransactionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->morphs('owner');
            $table->string('type')->default(TransactionType::PAYMENT);
            $table->foreignId('issuer')->unsigned();
            $table->foreignId('receiver')->unsigned();
            $table->foreignId('recipient_bank_account_id')->unsigned();
            $table->string('payment_method')->nullable();
            $table->string('payment_reference')->nullable();
            $table->string('bill_no')->unique();
            $table->decimal('amount', 25, 5)->default(0.00);
            $table->decimal('original_amount', 25, 5)->default(0.00);
            $table->foreignId('currency_id')->unsigned();
            $table->foreignId('original_currency_id')->unsigned();
            $table->decimal('currency_rate', 14, 5)->default(0.00);
            $table->decimal('tax', 14, 5)->default(0.00);
            $table->decimal('service_charge', 14, 5)->default(0.00);
            $table->timestamp('expires_on')->nullable();
            $table->integer('status')->default(ApprovalStatus::PENDING_SUBMISSION);
            $table->softDeletes();
            $table->timestamps();

            $table->foreign('currency_id')->references('id')->on('currencies');
            $table->foreign('issuer')->references('id')->on('companies');
            $table->foreign('receiver')->references('id')->on('companies');
            $table->foreign('original_currency_id')->references('id')->on('currencies');

        });

        //In-case the model name lengthy
        Schema::table('transactions', function (Blueprint $table) {
            $table->string('owner_type', 250)->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('transactions');
    }
}
