<?php

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

class GoliathSnapshots extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {        
        Schema::create('goliath_snapshots', function (Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();

            // Foreign identifiers
            $table->foreignId('region_id')->cascadeOnDelete()->nullable()->constrained('goliath_regions');
            $table->foreignId('subregion_id')->cascadeOnDelete()->nullable()->constrained('goliath_subregions');
            $table->foreignId('metro_id')->cascadeOnDelete()->nullable()->constrained('goliath_metro');

            $table->text('checksum');
            $table->longText('compressed');
        });

        Schema::create('goliath_appliance_rollbacks', function (Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Foreign identifiers
            $table->foreignId('region_id')->cascadeOnDelete()->nullable()->constrained('goliath_regions');
            $table->foreignId('subregion_id')->cascadeOnDelete()->nullable()->constrained('goliath_subregions');
            $table->foreignId('metro_id')->cascadeOnDelete()->nullable()->constrained('goliath_metro');
            
            $table->foreignId('rollback_id')->cascadeOnDelete()->constrained('goliath_snapshots');

        });

        Schema::create('goliath_appliance_terminations', function (Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Foreign identifiers
            $table->foreignId('appliance_id')->cascadeOnDelete()->constrained('goliath_appliances');

            // Characteristics
            $table->timestamp('expiration_at')->nullable();
        });

        Schema::create('goliath_appliance_captures', function (Blueprint $table) {
            // General characteristics - PK, timestamps, soft-deletable
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Foreign identifiers
            $table->foreignId('appliance_id')->cascadeOnDelete()->constrained('goliath_appliances');

            // Characteristics
            $table->timestamp('issued_at')->nullable();
            $table->tinyInteger('prescrubbed')->nullable();
            $table->string('bpf')->nullable();
            $table->string('location')->nullable();
        });


    }

    public function down()
    {
        Schema::dropIfExists('goliath_appliance_terminations');
        Schema::dropIfExists('goliath_appliance_rollbacks');
        Schema::dropIfExists('goliath_snapshots');
        Schema::dropIfExists('goliath_appliance_captures');
    }
}
