<?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('devices', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->constrained()->restrictOnDelete();
            $table->string('device_name');
            $table->string('device_brand')->nullable()->index();
            $table->string('device_model')->nullable()->index();
            $table->string('device_type', 50)->nullable();
            $table->string('manufacturer')->nullable()->index();
            $table->string('android_version', 30)->nullable()->index();
            $table->unsignedSmallInteger('api_level')->nullable();
            $table->string('build_number')->nullable();
            $table->string('serial_number')->nullable()->index();
            $table->string('imei_1', 32)->nullable()->unique();
            $table->string('imei_2', 32)->nullable()->unique();
            $table->string('meid', 32)->nullable()->unique();
            $table->string('device_id')->unique();
            $table->string('android_id')->unique();
            $table->string('mac_address', 32)->nullable();
            $table->string('bluetooth_mac', 32)->nullable();
            $table->string('wifi_mac', 32)->nullable();
            $table->string('sim_serial_number')->nullable()->index();
            $table->unsignedTinyInteger('sim_slot')->nullable();
            $table->string('phone_number', 30)->nullable()->index();
            $table->string('network_operator')->nullable();
            $table->string('carrier_name')->nullable();
            $table->string('country', 100)->nullable()->index();
            $table->string('timezone', 64)->nullable();
            $table->string('language', 12)->nullable();
            $table->unsignedTinyInteger('battery_percentage')->nullable()->index();
            $table->string('battery_health', 30)->nullable()->index();
            $table->decimal('battery_temperature', 5, 2)->nullable();
            $table->string('battery_status', 30)->nullable();
            $table->string('charging_status', 30)->nullable();
            $table->string('power_source', 30)->nullable();
            $table->unsignedBigInteger('storage_total')->nullable();
            $table->unsignedBigInteger('storage_used')->nullable();
            $table->unsignedBigInteger('storage_free')->nullable();
            $table->unsignedBigInteger('ram_total')->nullable();
            $table->unsignedBigInteger('ram_used')->nullable();
            $table->unsignedBigInteger('ram_free')->nullable();
            $table->string('cpu_architecture', 50)->nullable();
            $table->date('security_patch')->nullable();
            $table->string('play_services_version', 50)->nullable();
            $table->boolean('screen_lock_enabled')->default(false);
            $table->boolean('fingerprint_enabled')->default(false);
            $table->boolean('face_unlock_enabled')->default(false);
            $table->boolean('developer_options')->default(false)->index();
            $table->boolean('usb_debugging')->default(false);
            $table->string('root_status', 30)->default('unknown')->index();
            $table->string('bootloader_status', 30)->default('unknown');
            $table->string('encryption_status', 30)->default('unknown');
            $table->timestamp('last_online')->nullable()->index();
            $table->json('last_location')->nullable();
            $table->ipAddress('last_ip')->nullable();
            $table->string('status', 30)->default('offline')->index();
            $table->string('device_status', 30)->default('inactive')->index();
            $table->string('health_status', 30)->default('good')->index();
            $table->string('image_path')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'status']);
            $table->index(['user_id', 'device_status']);
            $table->index(['created_at', 'deleted_at']);
        });
    }

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