<?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('proveedors', function (Blueprint $table) {
            // Evitar operaciones que SQLite no maneja correctamente en tests
            $driver = Schema::getConnection()->getDriverName();

            // Agregar nuevos campos si no existen
            if (! Schema::hasColumn('proveedors', 'nit')) {
                $table->string('nit')->nullable();
            }

            if (! Schema::hasColumn('proveedors', 'categoria')) {
                $table->string('categoria')->nullable();
            }

            // En bases que no son SQLite, eliminar campos antiguos
            if ($driver !== 'sqlite') {
                $table->dropColumn(['contacto', 'correo']);
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('proveedors', function (Blueprint $table) {
            $driver = Schema::getConnection()->getDriverName();

            // Restaurar columnas eliminadas si no existen
            if (! Schema::hasColumn('proveedors', 'contacto')) {
                $table->string('contacto')->nullable();
            }

            if (! Schema::hasColumn('proveedors', 'correo')) {
                $table->string('correo')->unique()->nullable();
            }

            // En bases no SQLite, eliminar las columnas nuevas
            if ($driver !== 'sqlite') {
                $table->dropColumn(['nit', 'categoria']);
            }
        });
    }
};
