<?php

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

class CreatePromotionUserProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('promotion_user_products', function (Blueprint $table) {
            
            $table->increments('id');

            $table->unsignedInteger('promotion_admin_id')->index();
            $table->unsignedInteger('promotion_user_id')->index();

            $table->unsignedInteger('promotion_admin_product_id')->index();
            $table->unsignedInteger('product_id')->index();

            $table->unsignedSmallInteger('open_items')->nullable();
            $table->unsignedSmallInteger('sell_items')->nullable();

            $table->decimal('used_budget_total', 13, 2)->nullable();

            $table->boolean('active')->default(false);

            $table->foreign('promotion_admin_id')
                ->references('id')
                ->on('promotion_admins');     

            $table->foreign('promotion_user_id')
                ->references('id')
                ->on('promotion_users');     

            $table->foreign('promotion_admin_product_id')
                ->references('id')
                ->on('promotion_admin_products');

            $table->foreign('product_id')
                ->references('id')
                ->on('products');


            $table->timestamps();
        });
    }

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