<?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('device_security', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('device_id')->unique()->constrained()->cascadeOnDelete();
            $table->string('play_integrity_status', 30)->default('unknown');
            $table->boolean('root_detected')->default(false)->index();
            $table->boolean('sim_change_detection')->default(true);
            $table->boolean('anti_theft_enabled')->default(true);
            $table->boolean('remote_lock_enabled')->default(true);
            $table->boolean('location_enabled')->default(true);
            $table->boolean('camera_capture_enabled')->default(false);
            $table->boolean('microphone_capture_enabled')->default(false);
            $table->timestamp('last_security_scan_at')->nullable();
            $table->json('integrity_details')->nullable();
            $table->timestamps();
        });

        Schema::create('device_locations', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->decimal('latitude', 10, 7);
            $table->decimal('longitude', 10, 7);
            $table->decimal('accuracy', 10, 2)->nullable();
            $table->decimal('speed', 10, 2)->nullable();
            $table->decimal('heading', 8, 2)->nullable();
            $table->decimal('altitude', 10, 2)->nullable();
            $table->string('address')->nullable();
            $table->string('city', 100)->nullable();
            $table->string('state', 100)->nullable();
            $table->string('country', 100)->nullable();
            $table->string('source', 30)->default('gps');
            $table->boolean('is_mocked')->default(false);
            $table->timestamp('recorded_at')->index();
            $table->timestamps();

            $table->index(['device_id', 'recorded_at']);
            $table->index(['latitude', 'longitude']);
        });

        Schema::create('device_sim_history', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('slot')->default(1);
            $table->string('iccid')->nullable()->index();
            $table->string('imsi')->nullable();
            $table->string('phone_number', 30)->nullable();
            $table->string('carrier')->nullable();
            $table->string('operator')->nullable();
            $table->string('network_type', 50)->nullable();
            $table->smallInteger('signal_strength')->nullable();
            $table->boolean('is_roaming')->default(false);
            $table->timestamp('detected_at')->index();
            $table->timestamp('removed_at')->nullable();
            $table->timestamps();

            $table->index(['device_id', 'slot', 'detected_at']);
        });

        Schema::create('device_remote_commands', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->foreignId('admin_id')->nullable()->constrained()->nullOnDelete();
            $table->string('command', 50)->index();
            $table->json('payload')->nullable();
            $table->string('status', 30)->default('queued')->index();
            $table->text('result')->nullable();
            $table->timestamp('queued_at');
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('acknowledged_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamps();

            $table->index(['device_id', 'created_at']);
        });

        Schema::create('device_alerts', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->string('type', 50)->index();
            $table->string('severity', 20)->default('warning')->index();
            $table->string('title');
            $table->text('message')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamp('detected_at')->index();
            $table->timestamp('acknowledged_at')->nullable();
            $table->foreignId('acknowledged_by')->nullable()->constrained('admins')->nullOnDelete();
            $table->timestamp('resolved_at')->nullable();
            $table->timestamps();

            $table->index(['device_id', 'detected_at']);
            $table->index(['device_id', 'type', 'resolved_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('device_alerts');
        Schema::dropIfExists('device_remote_commands');
        Schema::dropIfExists('device_sim_history');
        Schema::dropIfExists('device_locations');
        Schema::dropIfExists('device_security');
    }
};
