<?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('license_activations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('license_id')->constrained()->cascadeOnDelete();

            // Activation identifiers
            $table->string('activation_id', 64)->unique(); // unique per activation
            $table->string('domain')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->string('installation_id', 128)->nullable(); // sha256 fingerprint
            $table->string('machine_fingerprint', 128)->nullable();
            $table->string('server_hostname')->nullable();
            $table->string('os_platform')->nullable();

            // Status
            $table->enum('status', ['active', 'deactivated', 'suspended', 'expired'])->default('active');
            $table->timestamp('activated_at')->useCurrent();
            $table->timestamp('deactivated_at')->nullable();
            $table->timestamp('last_validated_at')->nullable();
            $table->timestamp('last_heartbeat_at')->nullable();

            // Product version at activation time
            $table->string('product_version')->nullable();

            // Anti-tamper
            $table->string('activation_token', 128)->unique(); // rotatable token
            $table->string('activation_checksum', 64); // HMAC of activation data

            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['license_id', 'status']);
            $table->index('domain');
            $table->index('installation_id');
            $table->index('activation_token');
        });
    }

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