<?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('user_devices', function (Blueprint $table): void {
            $table->id();
            $table->integer('user_id');
            $table->string('device_id');
            $table->string('fcm_token')->nullable();
            $table->string('name')->nullable();
            $table->string('model')->nullable();
            $table->string('platform', 32)->nullable();
            $table->string('operating_system', 32)->nullable();
            $table->string('os_version', 64)->nullable();
            $table->string('manufacturer')->nullable();
            $table->boolean('is_virtual')->default(false);
            $table->string('web_view_version')->nullable();
            $table->string('app_version', 64)->nullable();
            $table->string('app_build', 64)->nullable();
            $table->string('locale', 35)->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->string('api_token', 64)->nullable()->unique();
            $table->timestamp('last_login_at')->nullable();
            $table->timestamp('last_seen_at')->nullable();
            $table->timestamp('last_logout_at')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'last_seen_at']);
        });
    }

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