<?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::table('collections', function (Blueprint $table): void {
            $table->foreignId('managed_by_user_id')
                ->nullable()
                ->after('user_id')
                ->constrained('users')
                ->nullOnDelete();
            $table->enum('editorial_owner_mode', ['creator', 'staff_account', 'system'])
                ->default('creator')
                ->after('type');
            $table->foreignId('editorial_owner_user_id')
                ->nullable()
                ->after('managed_by_user_id')
                ->constrained('users')
                ->nullOnDelete();
            $table->string('editorial_owner_label', 120)
                ->nullable()
                ->after('editorial_owner_user_id');
            $table->json('layout_modules_json')
                ->nullable()
                ->after('smart_rules_json');

            $table->index('managed_by_user_id', 'collections_managed_by_user_idx');
            $table->index(['type', 'editorial_owner_mode'], 'collections_editorial_owner_mode_idx');
        });
    }

    public function down(): void
    {
        Schema::table('collections', function (Blueprint $table): void {
            $table->dropIndex('collections_managed_by_user_idx');
            $table->dropIndex('collections_editorial_owner_mode_idx');
            $table->dropConstrainedForeignId('managed_by_user_id');
            $table->dropConstrainedForeignId('editorial_owner_user_id');
            $table->dropColumn(['editorial_owner_mode', 'editorial_owner_label', 'layout_modules_json']);
        });
    }
};
