<?php

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

/*
 * The Mitigation - Attacks notification pipeline. Oculi is polled into a local state mirror, state
 * transitions become notification rows keyed per organisation, and report links carry an expiry.
 * This replaces report_awareness, pending_reports and report_links for the Oculi-backed pipeline;
 * the legacy tables stay untouched until the legacy pipeline is retired.
 */
return new class extends Migration
{

    public function up()
    {
        /**
         * What we last knew about each attack, per organisation - Oculi mints attack identifiers per
         * organisation, so the pair is the natural key and transitions are computed against this row.
         */
        Schema::create('mitigation_attack_state', function (Blueprint $table) {
            $table->id();

            $table->timestamps();

            $table->uuid('attack_id');
            $table->unsignedInteger('organisation_id');

            $table->ipAddress('destination')->nullable();

            $table->timestamp('started_at')->nullable();
            $table->timestamp('finished_at')->nullable();

            $table->boolean('ongoing')->default(false);
            $table->boolean('ready')->default(false);

            $table->decimal('gbps', 12, 2)->default(0);
            $table->decimal('mpps', 12, 2)->default(0);

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

            $table->unique([ 'attack_id', 'organisation_id' ]);
            $table->index([ 'organisation_id' ]);
        });

        /**
         * One row per (attack, organisation, moment) - organisation scoped keys are what stop one
         * organisation's notification state suppressing anybody else's, which the legacy
         * report_awareness table was prone to.
         */
        Schema::create('mitigation_notification', function (Blueprint $table) {
            $table->id();

            $table->timestamps();

            $table->uuid('attack_id');
            $table->unsignedInteger('organisation_id');

            $table->string('type');
            $table->string('status');

            $table->unique([ 'attack_id', 'organisation_id', 'type' ]);
            $table->index([ 'organisation_id', 'status' ]);
        });

        /**
         * Guest download tokens. Unlike the legacy report_links these expire rather than burn on first
         * click, so an unfurl bot or a curious middlebox cannot use up a customer's link.
         */
        Schema::create('mitigation_report_link', function (Blueprint $table) {
            $table->id();

            $table->timestamps();

            $table->string('token', 128)->unique();

            $table->uuid('attack_id');
            $table->unsignedInteger('organisation_id');

            $table->timestamp('expires_at')->nullable();
        });
    }

    public function down()
    {
        Schema::dropIfExists('mitigation_report_link');
        Schema::dropIfExists('mitigation_notification');
        Schema::dropIfExists('mitigation_attack_state');
    }
};
