<?php

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

class RenameTraffic extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
		DB::statement('ALTER TABLE ab_service__adsl_traffic RENAME TO usage_broadband');
		DB::statement('ALTER TABLE usage_broadband RENAME COLUMN ab_service_adsl_id TO service_item_id');
		DB::statement('ALTER TABLE usage_broadband MODIFY supplier_id int unsigned NOT NULL');	// @todo This should be deleted, it could be obtained by the product
		DB::statement('ALTER TABLE usage_broadband MODIFY service_item_id int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE usage_broadband MODIFY site_id int unsigned NOT NULL');

		Schema::table('usage_broadband', function (Blueprint $table) {
			$table->unique(['service_item_id','site_id','date','time']);
			$table->foreign(['service_item_id','site_id'])->references(['id','site_id'])->on('service_broadband');
			$table->foreign(['supplier_id'])->references(['id'])->on('suppliers');
		});

		Schema::table('suppliers', function (Blueprint $table) {
			$table->date('usage_last')->nullable();
		});
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
		abort(500,'Cant go back');
    }
}