<?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('addons', function (Blueprint $table) {
            $table->id();
            // URL-safe identifier, matches the addon folder name on disk
            // (e.g. "instagram", "facebook-messenger"). Unique so re-installing
            // an addon with the same slug upserts instead of duplicating.
            $table->string('slug', 100)->unique();
            $table->string('name');
            $table->string('version', 32)->default('1.0.0');
            // active = boot + run; inactive = files present but skipped.
            // Lets admin disable a misbehaving addon without uninstalling.
            $table->enum('status', ['active', 'inactive'])->default('inactive');
            $table->text('description')->nullable();
            $table->string('author')->nullable();
            $table->string('author_url')->nullable();
            // Full addon.json snapshot — provider class, declared routes,
            // migrations, channel, settings URL, etc. Read on boot to wire
            // the addon into the framework without re-parsing the folder.
            $table->json('manifest')->nullable();
            $table->timestamp('installed_at')->nullable();
            // Last time admin clicked Activate. Useful for "ran X migrations
            // since this date" diagnostics.
            $table->timestamp('activated_at')->nullable();
            $table->timestamps();

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

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