<?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()
    {
		DB::statement('RENAME TABLE service__generic TO service_generic');
		Schema::create('costs', function (Blueprint $table) {
			$table->integer('id',TRUE,TRUE);
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->date('billed_at');
			$table->boolean('active');
			$table->integer('supplier_id')->unsigned();
			$table->string('invoice_num')->nullable();

			$table->unique(['id','site_id']);
			$table->foreign(['site_id'])->references(['site_id'])->on('sites');
			$table->foreign(['supplier_id'])->references(['id'])->on('suppliers');
		});

		Schema::create('cost_broadband', function (Blueprint $table) {
			$table->integer('id',TRUE,TRUE);
			$table->integer('site_id')->unsigned();
			$table->integer('cost_id')->unsigned();
			$table->boolean('active');
			$table->integer('service_broadband_id')->unsigned()->nullable();
			$table->integer('supplier_broadband_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_broadband_id','site_id'])->references(['id','site_id'])->on('service_broadband');
			$table->foreign(['supplier_broadband_id','site_id'])->references(['id','site_id'])->on('supplier_broadband');
		});

		Schema::create('cost_phone', function (Blueprint $table) {
			$table->integer('id',TRUE,TRUE);
			$table->integer('site_id')->unsigned();
			$table->integer('cost_id')->unsigned();
			$table->boolean('active');
			$table->integer('service_phone_id')->unsigned()->nullable();
			$table->integer('supplier_phone_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_phone_id','site_id'])->references(['id','site_id'])->on('service_phone');
			$table->foreign(['supplier_phone_id','site_id'])->references(['id','site_id'])->on('supplier_phone');
		});

		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('cost_phone');
		Schema::dropIfExists('cost_broadband');
		Schema::dropIfExists('costs');
    }
};