<?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('location_sessions', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->timestamp('started_at')->index();
            $table->timestamp('ended_at')->nullable();
            $table->decimal('total_distance', 14, 3)->default(0);
            $table->decimal('average_speed', 10, 3)->default(0);
            $table->decimal('maximum_speed', 10, 3)->default(0);
            $table->decimal('minimum_speed', 10, 3)->default(0);
            $table->unsignedInteger('moving_time')->default(0);
            $table->unsignedInteger('idle_time')->default(0);
            $table->unsignedInteger('route_points')->default(0);
            $table->string('status', 30)->default('active')->index();
            $table->json('analytics')->nullable();
            $table->timestamps();

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

        Schema::create('geofences', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->string('geofence_name');
            $table->string('category', 30)->default('safe_zone')->index();
            $table->decimal('latitude', 10, 7);
            $table->decimal('longitude', 10, 7);
            $table->unsignedInteger('radius');
            $table->boolean('entry_notification')->default(true);
            $table->boolean('exit_notification')->default(true);
            $table->unsignedInteger('stay_duration')->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['device_id', 'is_active']);
            $table->index(['latitude', 'longitude']);
            $table->unique(['device_id', 'geofence_name', 'deleted_at']);
        });

        Schema::create('tracking_settings', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('device_id')->unique()->constrained()->cascadeOnDelete();
            $table->unsignedInteger('tracking_interval')->default(30);
            $table->string('accuracy_mode', 30)->default('balanced');
            $table->boolean('background_tracking')->default(true);
            $table->boolean('battery_saver')->default(true);
            $table->boolean('offline_sync')->default(true);
            $table->boolean('geofence_enabled')->default(true);
            $table->boolean('auto_upload')->default(true);
            $table->unsignedInteger('batch_size')->default(50);
            $table->unsignedInteger('max_offline_days')->default(7);
            $table->timestamps();
        });

        Schema::create('geofence_events', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('geofence_id')->constrained()->cascadeOnDelete();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->foreignId('device_location_id')->nullable()->constrained('device_locations')->nullOnDelete();
            $table->string('event_type', 20)->index();
            $table->timestamp('occurred_at')->index();
            $table->timestamp('notified_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();

            $table->index(['device_id', 'occurred_at']);
            $table->index(['geofence_id', 'event_type', 'occurred_at']);
        });

        Schema::create('location_sync_batches', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('device_id')->constrained()->cascadeOnDelete();
            $table->string('client_batch_id', 64);
            $table->unsignedInteger('received_points')->default(0);
            $table->unsignedInteger('accepted_points')->default(0);
            $table->unsignedInteger('rejected_points')->default(0);
            $table->string('status', 30)->default('processing')->index();
            $table->json('validation_errors')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();

            $table->unique(['device_id', 'client_batch_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('location_sync_batches');
        Schema::dropIfExists('geofence_events');
        Schema::dropIfExists('tracking_settings');
        Schema::dropIfExists('geofences');
        Schema::dropIfExists('location_sessions');
    }
};
