<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
		Schema::table('system_logs',function (Blueprint $table) {
			$table->bigInteger('mailer_id')->nullable()->after('system_id');
			$table->foreign('mailer_id')->references('id')->on('mailers');
		});

		DB::update('UPDATE system_logs SET mailer_id=? WHERE sessiontype=?',[1,2]);
		DB::update('UPDATE system_logs SET mailer_id=? WHERE sessiontype=?',[2,0]);
		DB::update('ALTER TABLE system_logs ALTER COLUMN mailer_id SET NOT NULL');

		Schema::table('system_logs',function (Blueprint $table) {
			$table->dropColumn(['sessiontype']);
		});
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
		Schema::table('system_logs',function (Blueprint $table) {
			$table->integer('sessiontype')->nullable();
		});

		DB::update('UPDATE system_logs SET sessiontype=? WHERE mailer_id=?',[0,2]);
		DB::update('UPDATE system_logs SET sessiontype=? WHERE mailer_id=?',[2,1]);
		DB::update('ALTER TABLE system_logs ALTER COLUMN sessiontype SET NOT NULL');

		Schema::table('system_logs',function (Blueprint $table) {
			$table->dropForeign(['mailer_id']);
			$table->dropColumn(['mailer_id']);
		});
    }
};