<?php

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

class CreateDiscountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('discounts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('company_id');
            $table->string('description',200);
            $table->string('conditions',200);
            $table->integer('price')->nullable();
            $table->double('percentatge')->nullable();
            $table->boolean('active')->index();
            $table->timestamps();

            $table->foreign("company_id")->references("id")->on("companies")->onDelete("cascade"); //si borren un box, borra les entrades amb aquest box

        });

        Schema::table('box_product_variants',function (Blueprint $table){
            $table->foreign("discount_id")->references("id")->on("discounts")->onDelete("set null"); //si un box es borra, aquest es queda sense idBox
        });
        Schema::table('box_orders',function (Blueprint $table){
            $table->foreign("discount_id")->references("id")->on("discounts")->onDelete("set null"); //si un box es borra, aquest es queda sense idBox
        });
        Schema::table('box_order_lines',function (Blueprint $table){
            $table->foreign("discount_id")->references("id")->on("discounts")->onDelete("set null"); //si un box es borra, aquest es queda sense idBox
        });
    }

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