<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
		Schema::create('supplier_generic',function (Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->boolean('active')->default(false);

			$table->string('product_id',16)->nullable();
			$table->string('product_desc',128)->nullable();
			$table->float('base_cost');
			$table->float('setup_cost')->nullable();
			$table->integer('contract_term')->unsigned()->nullable();

			$table->integer('supplier_detail_id')->unsigned();
			$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');

			$table->unique(['id','site_id']);
		});

		Schema::create('product_generic',function (Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->boolean('active')->default(false);

			$table->string('name',64);
			$table->integer('contract_term')->unsigned()->nullable();

			$table->integer('supplier_item_id')->unsigned();
			$table->foreign(['supplier_item_id','site_id'])->references(['id','site_id'])->on('supplier_generic');

			$table->unique(['id','site_id']);
		});

		Schema::create('service_generic', function (Blueprint $table)
		{
			$table->id();
			$table->integer('site_id')->unsigned();

			$table->integer('service_id')->unsigned();
			$table->foreign(['service_id','site_id'])->references(['id','site_id'])->on('services');

			$table->integer('product_id')->unsigned()->nullable();
			$table->foreign(['product_id','site_id'])->references(['id','site_id'])->on('products');

			$table->unique(['id','site_id']);
			$table->unique(['site_id','service_id']);
		});

		Schema::create('cost_generic', function (Blueprint $table)
		{
			$table->integer('id',TRUE,TRUE);
			$table->integer('site_id')->unsigned();
			$table->integer('cost_id')->unsigned();
			$table->boolean('active');
			$table->integer('service_generic_id')->unsigned()->nullable();
			$table->integer('supplier_generic_id')->unsigned()->nullable();
			$table->date('start_at')->nullable();
			$table->date('end_at')->nullable();
			$table->float('base')->nullable();
			$table->float('excess')->nullable();
			$table->string('reference')->nullable();
			$table->text('notes')->nullable();

			$table->foreign(['cost_id','site_id'])->references(['id','site_id'])->on('costs');
			$table->foreign(['service_generic_id','site_id'])->references(['id','site_id'])->on('service_generic');
			$table->foreign(['supplier_generic_id','site_id'])->references(['id','site_id'])->on('supplier_generic');
		});
    }

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