<?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
    {
        Schema::create('users', function (Blueprint $table) {

            $table->id();

            // Informações básicas
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone', 20)->nullable()->unique();

            // Verificação de email
            $table->timestamp('email_verified_at')->nullable();
            $table->string('email_verification_code')->nullable();
            $table->timestamp('email_verification_expires_at')->nullable();

            // Autenticação
            $table->string('password');
            $table->rememberToken();

            // Recuperação de senha
            $table->string('password_reset_token')->nullable();
            $table->timestamp('password_reset_expires_at')->nullable();

            // Perfil
            $table->string('avatar')->nullable();

            // Função no sistema
            $table->enum('role', [
                'super_admin',
                'manager',
                'seller'
            ])->default('seller');

            // Loja associada
            $table->foreignId('store_id')
                ->nullable()
                ->constrained('stores')
                ->nullOnDelete();

            // Estado da conta
            $table->boolean('is_active')->default(true);

            // Auditoria
            $table->timestamp('last_login_at')->nullable();

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

            // Indexes
            $table->index('email');
            $table->index('phone');
            $table->index('role');
            $table->index('store_id');
            $table->index('is_active');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('users');
    }
};