<?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('theft_events', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('incident_number', 40)->unique();
            $table->foreignId('device_id')->constrained()->restrictOnDelete();
            $table->foreignId('user_id')->constrained()->restrictOnDelete();
            $table->string('event_type', 80)->index();
            $table->string('risk_level', 20)->index();
            $table->unsignedTinyInteger('risk_score')->default(0)->index();
            $table->string('event_title');
            $table->text('event_description')->nullable();
            $table->decimal('latitude', 10, 7)->nullable();
            $table->decimal('longitude', 10, 7)->nullable();
            $table->string('address')->nullable();
            $table->unsignedTinyInteger('battery_percentage')->nullable();
            $table->string('network_status', 30)->nullable();
            $table->string('gps_status', 30)->nullable();
            $table->boolean('wifi_status')->nullable();
            $table->boolean('mobile_data_status')->nullable();
            $table->boolean('device_locked')->default(false);
            $table->boolean('alarm_triggered')->default(false);
            $table->boolean('camera_triggered')->default(false);
            $table->boolean('notification_sent')->default(false);
            $table->boolean('sms_sent')->default(false);
            $table->boolean('email_sent')->default(false);
            $table->string('status', 30)->default('pending')->index();
            $table->string('source', 30)->default('device');
            $table->string('client_event_id', 64)->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->foreignId('resolved_by')->nullable()->constrained('admins')->nullOnDelete();
            $table->text('resolution_notes')->nullable();
            $table->json('context')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['device_id', 'client_event_id']);
            $table->index(['device_id', 'detected_at']);
            $table->index(['user_id', 'detected_at']);
            $table->index(['status', 'risk_level', 'detected_at']);
            $table->index(['latitude', 'longitude']);
        });

        Schema::create('security_rules', function (Blueprint $table): void {
            $table->id();
            $table->string('rule_name');
            $table->string('rule_code', 80)->unique();
            $table->text('rule_description')->nullable();
            $table->boolean('is_enabled')->default(true)->index();
            $table->unsignedSmallInteger('priority')->default(100)->index();
            $table->unsignedInteger('trigger_delay')->default(0);
            $table->string('risk_level', 20)->default('medium');
            $table->boolean('auto_alarm')->default(false);
            $table->boolean('capture_image')->default(false);
            $table->boolean('capture_video')->default(false);
            $table->boolean('capture_audio')->default(false);
            $table->boolean('lock_device')->default(false);
            $table->boolean('notify_owner')->default(true);
            $table->boolean('notify_admin')->default(true);
            $table->boolean('send_sms')->default(false);
            $table->boolean('send_email')->default(false);
            $table->json('conditions')->nullable();
            $table->json('additional_actions')->nullable();
            $table->boolean('requires_device_owner')->default(false);
            $table->boolean('is_system')->default(true);
            $table->timestamps();
        });

        Schema::create('device_security_logs', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('device_id')->constrained()->restrictOnDelete();
            $table->foreignId('theft_event_id')->nullable()->constrained('theft_events')->nullOnDelete();
            $table->string('security_event', 80)->index();
            $table->string('security_level', 20)->index();
            $table->string('event_source', 30)->index();
            $table->longText('event_data')->nullable();
            $table->string('payload_hash', 64)->nullable();
            $table->timestamp('created_at')->useCurrent()->index();

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

        Schema::create('alarm_history', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('device_id')->constrained()->restrictOnDelete();
            $table->foreignId('theft_event_id')->nullable()->constrained('theft_events')->nullOnDelete();
            $table->string('alarm_type', 40)->index();
            $table->timestamp('started_at')->index();
            $table->timestamp('stopped_at')->nullable();
            $table->unsignedInteger('duration')->nullable();
            $table->string('trigger_source', 30);
            $table->string('status', 30)->default('active')->index();
            $table->unsignedTinyInteger('volume_percentage')->default(100);
            $table->foreignId('stopped_by')->nullable()->constrained('admins')->nullOnDelete();
            $table->timestamps();

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

        Schema::create('theft_event_actions', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('theft_event_id')->constrained()->cascadeOnDelete();
            $table->string('action', 50)->index();
            $table->string('status', 30)->default('queued')->index();
            $table->unsignedSmallInteger('attempts')->default(0);
            $table->json('payload')->nullable();
            $table->text('result')->nullable();
            $table->timestamp('queued_at');
            $table->timestamp('executed_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamps();

            $table->index(['theft_event_id', 'status']);
        });

        Schema::create('theft_event_media', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('theft_event_id')->constrained()->cascadeOnDelete();
            $table->foreignId('device_id')->constrained()->restrictOnDelete();
            $table->string('media_type', 20)->index();
            $table->string('camera_facing', 20)->nullable();
            $table->string('disk', 30);
            $table->string('path');
            $table->string('thumbnail_path')->nullable();
            $table->string('mime_type', 100);
            $table->unsignedBigInteger('size')->default(0);
            $table->unsignedInteger('duration')->nullable();
            $table->string('checksum', 64);
            $table->json('metadata')->nullable();
            $table->timestamp('captured_at')->index();
            $table->timestamps();

            $table->index(['theft_event_id', 'media_type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('theft_event_media');
        Schema::dropIfExists('theft_event_actions');
        Schema::dropIfExists('alarm_history');
        Schema::dropIfExists('device_security_logs');
        Schema::dropIfExists('security_rules');
        Schema::dropIfExists('theft_events');
    }
};
