<?php

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

class ReportAwareness extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Extend report_organisation_attributes table
        Schema::table('report_organisation_attributes', function (Blueprint $table) {
            $table->boolean('send_start')->default(false);
            $table->boolean('send_finish')->default(false);
        });

        // Create new table for report awareness
        Schema::create('report_awareness', function (Blueprint $table) {
            
            // Metadata
            $table->id();
            $table->timestamps();
            $table->softDeletes();
            
            // General
            $table->enum('type', ['start', 'finish']);
            $table->enum('status', ['pending', 'sent']);

            $table->unsignedBigInteger('report_id')->nullable();

            $table->float('mpps')->nullable();
            $table->float('gbps')->nullable();

            $table->string('address')->nullable();

            $table->foreignId('organisation_id')->constrained('organisations')->cascadeOnDelete();

            // Indexes
            $table->index(['organisation_id', 'status']);
            $table->index('type');
            $table->index('report_id');
        });
    }

    public function down() {
        Schema::table('report_organisation_attributes', function (Blueprint $table) {
            $table->dropColumn('send_start');
            $table->dropColumn('send_finish');
        });

        Schema::dropIfExists('report_awareness');
    }
}
