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

            $table->string('contact_code', 50)->unique()->nullable()->comment('Código único del contacto');

            $table->string('name', 100)->comment('Nombre')->index();
            $table->string('last_name', 100)->comment('Apellidos')->nullable()->index();

            $table->string('email')->unique()->index();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password')->nullable();
            $table->rememberToken();
            $table->string('profile_photo_path', 2048)->nullable();

            $table->string('company', 100)->nullable()->comment('Nombre de la empresa')->index();

            $table->date('birth_date')->nullable()->comment('Fecha de nacimiento');
            $table->date('hire_date')->nullable()->comment('Fecha de contratación');

            $table->string('curp', 50)->nullable()->comment('Clave Única de Registro de Población (CURP)')->index();
            $table->string('nss', 11)->nullable()->comment('Número de seguridad social')->index();

            $table->string('job_title', 100)->nullable()->comment('Cargo del contacto en la empresa');

            $table->json('face_vector')->nullable()->comment('Vector de características faciales');

            $table->string('rfc', 13)->unique()->nullable()->index();
            $table->string('nombre_fiscal')->nullable()->index();

            $table->unsignedTinyInteger('tipo_persona')->nullable()->index();

            $table->unsignedSmallInteger('c_regimen_fiscal')->nullable()->index(); // sat_regimen_fiscal.c_regimen_fiscal

            $table->unsignedMediumInteger('domicilio_fiscal')->nullable()->index(); // sat_codigo_postal.c_codigo_postal

            $table->char('c_uso_cfdi', 4)->charset('ascii')->collation('ascii_general_ci')->nullable()->index(); // sat_uso_cfdi.c_uso_cfdi

            $table->unsignedTinyInteger('is_partner')->nullable()->index();
            $table->unsignedTinyInteger('is_employee')->nullable()->index();
            $table->unsignedTinyInteger('is_prospect')->nullable()->index();
            $table->unsignedTinyInteger('is_customer')->nullable()->index();
            $table->unsignedTinyInteger('is_provider')->nullable()->index();
            $table->unsignedTinyInteger('is_user')->nullable()->index();

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

            // Auditoría
            $table->dateTime('last_login_at')->nullable();
            $table->ipAddress('last_login_ip')->nullable();

            $table->unsignedMediumInteger('created_by')->nullable()->index(); // users.id
            $table->timestamps();

            // Relaciones
            $table->foreign('created_by')->references('id')->on('users')->onUpdate('restrict')->onDelete('restrict');
        });

        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->longText('payload');
            $table->integer('last_activity')->index();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
    }
};
