<?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::create('sat_aduana', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedTinyInteger('c_aduana')->unique();
            $table->string('descripcion')->nullable()->index();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();
        });

        Schema::create('sat_banco', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedSmallInteger('c_banco')->unique();
            $table->string('descripcion')->index();
            $table->string('razon_social')->nullable();
            $table->string('rfc', 13)->nullable();

            $table->unsignedTinyInteger('status')->index();

            $table->timestamps();
        });

        Schema::create('sat_clave_prod_serv', function (Blueprint $table) {
            $table->mediumIncrements('id');

            // Columna numérica original
            $table->unsignedInteger('c_clave_prod_serv')->unique();

            // Columna de texto con índice fulltext (inicial)
            $table->string('descripcion')->nullable();

            $table->string('incluir_iva_trasladado')->nullable();
            $table->string('incluir_ieps_trasladado')->nullable();
            $table->string('complemento_que_debe_incluir')->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();
            $table->unsignedTinyInteger('estimulo_franja_fronteriza')->nullable();
            $table->string('palabras_similares', 1024)->nullable();

            // Columna generada a partir de c_clave_prod_serv
            // (Convierte el INT a CHAR para poder incluirlo en el MATCH ... AGAINST)
            $table->string('c_clave_prod_serv_text', 8)
                  ->storedAs('CAST(`c_clave_prod_serv` AS CHAR)');

            // Añadir aquí el índice FULLTEXT en la misma migración
            // Con Laravel 8+ y MySQL 5.7+ / MariaDB 10.2+ se puede usar:
            $table->fullText(['c_clave_prod_serv_text', 'descripcion'], 'claveprodserv_fulltext_idx');

            $table->timestamps();
        });

        Schema::create('sat_clave_unidad', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->string('c_clave_unidad', 3)->unique();
            $table->string('nombre')->index();
            $table->string('descripcion', 1024)->nullable();
            $table->string('categoria', 1024)->nullable();
            $table->string('nota', 1024)->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();
            $table->string('simbolo')->nullable();
            $table->boolean('is_base')->default(false);

            $table->timestamps();
        });

        Schema::create('sat_codigo_postal', function (Blueprint $table) {
            $table->mediumIncrements('id');

            $table->unsignedMediumInteger('c_codigo_postal')->index();
            $table->string('c_estado', 3)->charset('ascii')->collation('ascii_general_ci')->index();
            $table->unsignedSmallInteger('c_municipio')->nullable()->index();
            $table->unsignedTinyInteger('c_localidad')->nullable()->index();
            $table->string('estimulo_franja_fronteriza')->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();
        });

        Schema::create('sat_colonia', function (Blueprint $table) {
            $table->mediumIncrements('id');

            $table->unsignedMediumInteger('c_colonia')->index();
            $table->unsignedMediumInteger('c_codigo_postal')->index();
            $table->string('nombre_del_asentamiento')->nullable()->index();

            $table->timestamps();

            // Indices
            $table->unique(['c_colonia', 'c_codigo_postal']);
        });

        Schema::create('sat_deduccion', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedTinyInteger('c_deduccion')->unique();
            $table->string('descripcion')->index();

            $table->timestamps();
        });

        Schema::create('sat_estado', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->string('c_estado', 3)->charset('ascii')->collation('ascii_general_ci')->index();
            $table->char('c_pais', 3)->charset('ascii')->collation('ascii_general_ci')->index();
            $table->string('nombre_del_estado')->index();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();

            // Indices
            $table->unique(['c_estado', 'c_pais']);
        });

        Schema::create('sat_forma_pago', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedTinyInteger('c_forma_pago')->unique();
            $table->string('descripcion')->index();
            $table->string('bancarizado')->nullable();
            $table->string('numero_de_operacion')->nullable();
            $table->string('rfc_del_emisor_de_la_cuenta_ordenante')->nullable();
            $table->string('cuenta_ordenante')->nullable();
            $table->string('patron_para_cuenta_ordenante')->nullable();
            $table->string('rfc_del_emisor_cuenta_de_beneficiario')->nullable();
            $table->string('cuenta_de_benenficiario')->nullable();
            $table->string('patron_para_cuenta_beneficiaria')->nullable();
            $table->string('tipo_cadena_pago')->nullable();
            $table->string('banco_emisor_de_la_cuenta_ordenante')->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();
        });

        Schema::create('sat_localidad', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedTinyInteger('c_localidad')->index();
            $table->string('c_estado', 3)->charset('ascii')->collation('ascii_general_ci')->index();
            $table->string('descripcion')->index();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();

            // Indices
            $table->unique(['c_localidad', 'c_estado']);
        });

        Schema::create('sat_municipio', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedSmallInteger('c_municipio')->index();
            $table->string('c_estado', 3)->charset('ascii')->collation('ascii_general_ci')->index();
            $table->string('descripcion')->index();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();

            // Indices
            $table->unique(['c_municipio', 'c_estado']);
        });

        Schema::create('sat_moneda', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->char('c_moneda', 3)->charset('ascii')->collation('ascii_general_ci')->unique();
            $table->string('descripcion')->index();
            $table->string('decimales')->nullable();
            $table->string('porcentaje_variacion')->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();
        });

        Schema::create('sat_pais', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->char('c_pais', 3)->charset('ascii')->collation('ascii_general_ci')->unique();
            $table->string('descripcion')->index();
            $table->string('formato_de_codigo_postal')->nullable();
            $table->string('formato_de_registro_de_identidad_tributaria')->nullable();
            $table->string('validacion_del_registro_de_identidad_tributaria')->nullable();
            $table->string('agrupaciones')->nullable();

            $table->timestamps();
        });

        Schema::create('sat_percepcion', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedTinyInteger('c_percepcion')->unique();
            $table->string('descripcion')->index();
            $table->enum('gravado_exento', ['Gravado', 'Gravado y Exento', 'Exento']);

            $table->timestamps();
        });

        Schema::create('sat_regimen_contratacion', function (Blueprint $table) {
            $table->tinyIncrements('id');

            $table->unsignedTinyInteger('c_tipo_regimen')->unique();
            $table->string('descripcion')->index();

            $table->timestamps();
        });

        Schema::create('sat_regimen_fiscal', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->unsignedSmallInteger('c_regimen_fiscal')->unique();
            $table->string('descripcion')->index();
            $table->char('fisica', 2)->nullable();
            $table->char('moral', 2)->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();

            $table->timestamps();
        });

        Schema::create('sat_uso_cfdi', function (Blueprint $table) {
            $table->smallIncrements('id');

            $table->char('c_uso_cfdi', 4)->charset('ascii')->collation('ascii_general_ci')->unique();
            $table->string('descripcion')->index();
            $table->char('aplica_para_tipo_persona_fisica', 2)->nullable();
            $table->char('aplica_para_tipo_persona_moral', 2)->nullable();
            $table->date('fecha_inicio_vigencia')->nullable();
            $table->date('fecha_fin_vigencia')->nullable();
            $table->string('regimen_fiscal_receptor')->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('sat_banco');
        Schema::dropIfExists('sat_clave_prod_serv');
        Schema::dropIfExists('sat_clave_unidad');
        Schema::dropIfExists('sat_codigo_postal');
        Schema::dropIfExists('sat_colonia');
        Schema::dropIfExists('sat_deduccion');
        Schema::dropIfExists('sat_estado');
        Schema::dropIfExists('sat_forma_pago');
        Schema::dropIfExists('sat_localidad');
        Schema::dropIfExists('sat_municipio');
        Schema::dropIfExists('sat_moneda');
        Schema::dropIfExists('sat_pais');
        Schema::dropIfExists('sat_percepcion');
        Schema::dropIfExists('sat_regimen_contratacion');
        Schema::dropIfExists('sat_regimen_fiscal');
        Schema::dropIfExists('sat_uso_cfdi');
    }
};
