<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('tbl_provinces')) {
            Schema::create('tbl_provinces', function (Blueprint $table) {
                $table->increments('id_province');
                $table->string('name', 255)->unique();
                $table->timestamps();
            });

            return;
        }

        Schema::table('tbl_provinces', function (Blueprint $table) {
            if (! Schema::hasColumn('tbl_provinces', 'id_province')) {
                $table->increments('id_province');
            }

            if (! Schema::hasColumn('tbl_provinces', 'name')) {
                $table->string('name', 255)->unique();
            }

            if (! Schema::hasColumn('tbl_provinces', 'created_at')) {
                $table->timestamps();
            }
        });
    }

    public function down(): void
    {
        // No hacemos drop para evitar pérdida de datos.
    }
};
