<?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_tokens', function (Blueprint $table) {
            $table->id();

            // Cliente propietario del token
            $table->foreignId('client_id')
                ->constrained('clients')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();

            // Usuario creador del token (AUDITORÍA)
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->cascadeOnUpdate();

            // Token único que viaja al plugin
            $table->string('token', 100)->unique();

            // El token está operativo o no
            $table->boolean('active')->default(true);

            // Ventana temporal de uso
            $table->timestamp('start_at')->nullable();
            $table->timestamp('end_at')->nullable();

            // Modo general del token (free, trial, full...) opcional
            $table->string('mode', 50)->nullable();

            // Límites de uso (opcional)
            $table->integer('usage_limit')->nullable();

            // Datos adicionales no tipados
            $table->json('metadata')->nullable();

            // Observaciones internas
            $table->text('observation')->nullable();

            $table->timestamps();
            $table->softDeletes();
        });
    }

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