<?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('productos', function (Blueprint $table) {
            $table->dropColumn('tenant_id');
        });

        Schema::table('productos', function (Blueprint $table) {
            $table->string('tenant_codigo')->after('id');
            $table->foreign('tenant_codigo')->references('codigo')->on('tenants')->cascadeOnUpdate()->restrictOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('productos', function (Blueprint $table) {
            if (Schema::getConnection()->getDriverName() !== 'sqlite') {
                $table->dropForeign(['tenant_codigo']);
            }
            $table->dropColumn('tenant_codigo');
        });

        Schema::table('productos', function (Blueprint $table) {
            $table->unsignedBigInteger('tenant_id')->after('id');
        });
    }
};
