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

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

            $table->unsignedTinyInteger('status');

            $table->timestamps();
        });

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

            $table->unsignedInteger('c_clave_prod_serv');
            $table->string('descripcion')->nullable()->fulltext();
            $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();

            $table->timestamps();

            // Indices
            $table->unique('c_clave_prod_serv');
        });

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

            $table->string('c_clave_unidad', 3)->unique();
            $table->string('nombre')->fulltext();
            $table->string('descripcion', 1024)->nullable();
            $table->string('categoria', 1024)->nullable();
            $table->string('nota', 1024)->nullable();
            $table->date('fecha_de_inicio_de_vigencia')->nullable();
            $table->date('fecha_de_fin_de_vigencia')->nullable();
            $table->string('simbolo')->nullable();
            $table->boolean('is_base')->nullable();

            $table->timestamps();
        });

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

            $table->unsignedTinyInteger('c_forma_pago');
            $table->string('descripcion');
            $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_de_vigencia')->nullable();
            $table->date('fecha_fin_de_vigencia')->nullable();

            $table->timestamps();

            // Indices
            $table->unique('c_forma_pago');
        });

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

            $table->char('c_moneda', 3)->charset('ascii')->collation('ascii_general_ci');
            $table->string('descripcion');
            $table->string('decimales')->nullable();
            $table->string('porcentaje_variacion')->nullable();
            $table->date('fecha_inicio_de_vigencia')->nullable();
            $table->date('fecha_fin_de_vigencia')->nullable();

            $table->timestamps();

            // Indices
            $table->unique('c_moneda');
        });

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

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

            $table->timestamps();

            // Indices
            $table->index('c_codigo_postal');
            $table->index('c_estado');
            $table->index('c_municipio');
            $table->index('c_localidad');

            $table->unique('c_codigo_postal');
        });

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

            $table->unsignedSmallInteger('c_regimen_fiscal');
            $table->string('descripcion');
            $table->char('fisica', 2)->nullable();
            $table->char('moral', 2)->nullable();
            $table->date('fecha_de_inicio_de_vigencia')->nullable();
            $table->date('fecha_de_fin_de_vigencia')->nullable();

            $table->timestamps();

            // Indices
            $table->unique('c_regimen_fiscal');
        });

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

            $table->char('c_pais', 3)->charset('ascii')->collation('ascii_general_ci');
            $table->string('descripcion');
            $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();

            // Indices
            $table->unique('c_pais');
        });

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

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

            $table->timestamps();

            // Indices
            $table->unique('c_uso_cfdi');
        });

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

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

            $table->timestamps();

            // Indices
            $table->index('c_colonia');
            $table->index('c_codigo_postal');

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

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

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

            $table->timestamps();

            // Indices
            $table->index('c_estado');
            $table->index('c_pais');

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

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

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

            $table->timestamps();

            // Indices
            $table->index('c_localidad');
            $table->index('c_estado');

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

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

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

            $table->timestamps();

            // Indices
            $table->index('c_municipio');
            $table->index('c_estado');

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

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

            $table->unsignedTinyInteger('c_deduccion')->index();
            $table->string('descripcion');
            $table->date('fecha_inicio_de_vigencia')->nullable();
            $table->date('fecha_fin_de_vigencia')->nullable();

            $table->timestamps();
        });

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

            $table->unsignedTinyInteger('c_percepcion')->index();
            $table->string('descripcion');
            $table->date('fecha_inicio_de_vigencia')->nullable();
            $table->date('fecha_fin_de_vigencia')->nullable();

            $table->timestamps();
        });


        Schema::table('users', function (Blueprint $table) {
            // Indices
            $table->foreign('c_regimen_fiscal')
                ->references('c_regimen_fiscal')
                ->on('sat_regimen_fiscal')
                ->onUpdate('restrict')
                ->onDelete('restrict');

            $table->foreign('domicilio_fiscal')
                ->references('c_codigo_postal')
                ->on('sat_codigo_postal')
                ->onUpdate('restrict')
                ->onDelete('restrict');

            $table->foreign('c_uso_cfdi')
                ->references('c_uso_cfdi')
                ->on('sat_uso_cfdi')
                ->onUpdate('restrict')
                ->onDelete('restrict');
        });

    }

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