<?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('workspace_folders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('name', 160);
            $table->foreignId('parent_id')->nullable()->constrained('workspace_folders')->nullOnDelete();
            $table->timestamps();

            $table->index(['user_id', 'parent_id']);
        });

        Schema::create('workspace_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('folder_id')->nullable()->constrained('workspace_folders')->nullOnDelete();
            $table->string('type', 32);
            $table->unsignedBigInteger('ref_id')->nullable();
            $table->string('title', 255);
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'folder_id']);
            $table->index(['user_id', 'type']);
        });
    }

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