<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::table('sensors_to_state_indexes', function (Blueprint $table) {
            $table->foreign('state_index_id', 'sensors_to_state_indexes_ibfk_1')->references('state_index_id')->on('state_indexes')->onUpdate('restrict')->onDelete('restrict');
            $table->foreign('sensor_id')->references('sensor_id')->on('sensors')->onUpdate('restrict')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        if (LibreNMS\DB\Eloquent::getDriver() !== 'sqlite') {
            Schema::table('sensors_to_state_indexes', function (Blueprint $table) {
                $table->dropForeign('sensors_to_state_indexes_ibfk_1');
                $table->dropForeign('sensors_to_state_indexes_sensor_id_foreign');
            });
        }
    }
};
