<?php

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

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

			$table->string('file');
			$table->string('magic')->nullable();
			$table->integer('size');
			$table->bigInteger('crc');
			$table->datetime('datetime');
			$table->string('lfile')->nullable();
			$table->string('desc')->nullable();
			$table->text('ldesc')->nullable();

			$table->jsonb('rogue_path')->nullable();
			$table->jsonb('rogue_seenby')->nullable();
			$table->jsonb('kludges')->nullable();

			$table->bigInteger('filearea_id');
			$table->foreign('filearea_id')->references('id')->on('fileareas');

			$table->bigInteger('fftn_id');
			$table->foreign('fftn_id')->references('id')->on('addresses');
		});

		Schema::create('file_seenby', function (Blueprint $table) {
			$table->bigInteger('address_id');
			$table->foreign('address_id')->references('id')->on('addresses');

			$table->bigInteger('file_id');
			$table->foreign('file_id')->references('id')->on('files');

			$table->datetime('export_at')->nullable();
			$table->datetime('sent_at')->nullable();

			$table->unique(['address_id','file_id']);
		});

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

			$table->bigInteger('address_id');
			$table->foreign('address_id')->references('id')->on('addresses');

			$table->unique(['id','file_id']);
			$table->unique(['file_id','address_id','parent_id']);

			$table->bigInteger('parent_id')->nullable();
			$table->foreign(['parent_id','file_id'])->references(['id','file_id'])->on('file_path');

			$table->bigInteger('file_id');
			$table->foreign('file_id')->references('id')->on('files');

			$table->datetime('datetime');
			$table->string('extra')->nullable();
		});
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('file_path');
		Schema::dropIfExists('file_seenby');
		Schema::dropIfExists('files');
    }
};