<?php

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

class Webhooks extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('webhooks', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Data
            $table->string('name')->nullable();
            $table->string('hook');
            $table->enum('hook_type', ['slack', 'google', 'discord', 'raw', 'other_1', 'other_2', 'other_3', 'other_4', 'other_5', 'other_6', 'other_7', 'other_8', 'other_9']);
            $table->enum('usage_type', ['reporting', 'other_1', 'other_2', 'other_3', 'other_4', 'other_5', 'other_6', 'other_7', 'other_8', 'other_9']);


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

            // Indexes
            $table->index(['organisation_id']);
        });

        Schema::create('webhook_messages', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();

            // Data
            $table->timestamp(('sent_at'))->nullable();
            $table->string('key');
            $table->enum('message_type', ['reporting', 'other_1', 'other_2', 'other_3', 'other_4', 'other_5', 'other_6', 'other_7', 'other_8', 'other_9']);
            $table->enum('status', ['pending', 'sent', 'error', 'no-hook']);
            $table->json('payload')->nullable();

            // Foreign Keys
            $table->foreignId('organisation_id')->constrained('organisations')->cascadeOnDelete();
            $table->foreignId('webhook_id')->constrained('webhooks')->cascadeOnDelete();

            // Indexes
            $table->index(['key']);
            $table->index(['organisation_id']);
            $table->index(['message_type']);
            $table->index(['status']);
            $table->index(['webhook_id']);
        });

    }

    public function down() {

        Schema::dropIfExists('webhook_messages');
        Schema::dropIfExists('webhooks');

    }
}
