<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('environments', function (Blueprint $table) {

            $table->id();

            // Información base del entorno
            $table->string('name');
            $table->text('description')->nullable();
            $table->string('domain')->unique();

            // Versión actual del Moodle
            $table->string('version');

            // Entorno del entorno (local, develop, pre, pro)
            $table->string('env');

            // Cliente asociado
            $table->foreignId('client_id')
                ->nullable()
                ->constrained('clients')
                ->nullOnDelete()
                ->cascadeOnUpdate();

            // Tipo de plataforma (moodle, workplace, goodle...)
            $table->foreignId('type_id')
                ->nullable()
                ->constrained('types')
                ->nullOnDelete()
                ->cascadeOnUpdate();

            // Indica si el entorno tiene soporte activo
            $table->boolean('has_support')->default(false);

            // Soporte actual contratado.
            $table->foreignId('environment_support_id')->nullable();

            // Información de actualización (Moodle)
            $table->string('lastversion')->nullable();
            $table->string('lastminor')->nullable();
            $table->string('moodletoken')->nullable();

            // Última sincronización con el manager
            $table->timestamp('refresh_at')->nullable();

            // Estado del entorno
            $table->boolean('active')->default(true);

            // Clave interna (identificador único opcional)
            $table->string('key')->nullable();

            // Relación con el token de licencia (1 token - N environments)
            $table->foreignId('license_token_id')
                ->nullable()
                ->constrained('license_tokens')
                ->nullOnDelete()
                ->cascadeOnUpdate();

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

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('environments');
    }
};
