<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (DB::getDriverName() === 'sqlite') {
            return;
        }

        Schema::table('tickets', function (Blueprint $table): void {
            $table->dropForeign(['source_catalog_item_id']);
            $table->dropForeign(['source_variant_id']);

            $table->foreign('source_catalog_item_id')
                ->references('id')
                ->on('catalog_items')
                ->cascadeOnUpdate()
                ->restrictOnDelete();
            $table->foreign('source_variant_id')
                ->references('id')
                ->on('variantes')
                ->cascadeOnUpdate()
                ->restrictOnDelete();
        });
    }

    public function down(): void
    {
        if (DB::getDriverName() === 'sqlite') {
            return;
        }

        Schema::table('tickets', function (Blueprint $table): void {
            $table->dropForeign(['source_catalog_item_id']);
            $table->dropForeign(['source_variant_id']);

            $table->foreign('source_catalog_item_id')
                ->references('id')
                ->on('catalog_items')
                ->cascadeOnUpdate()
                ->nullOnDelete();
            $table->foreign('source_variant_id')
                ->references('id')
                ->on('variantes')
                ->cascadeOnUpdate()
                ->nullOnDelete();
        });
    }
};
