<?php

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

class CreateCouponsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('coupons', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->float('percentage')->nullable();
            $table->float('amount')->nullable();
            $table->float('limit')->nullable();
            $table->timestamp('expire_at')->nullable();
            $table->timestamps();
        });

        Schema::table('current_contracts', function (Blueprint $table) {
            //
            $table->unsignedBigInteger('coupon_id')->after('en')->nullable();
            $table->foreign('coupon_id')->references('id')->on('coupons')->onDelete('set null');
        });

        Schema::table('finished_contracts', function (Blueprint $table) {
            //
            $table->unsignedBigInteger('coupon_id')->after('en')->nullable();
            $table->foreign('coupon_id')->references('id')->on('coupons')->onDelete('set null');
        });
    }

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

        Schema::table('current_contracts', function (Blueprint $table) {
            //
            $table->dropForeign(['coupon_id']);
            $table->dropColumn('coupon_id');
        });

        Schema::table('finished_contracts', function (Blueprint $table) {
            //
            $table->dropForeign(['coupon_id']);
            $table->dropColumn('coupon_id');
        });
    }
}
