<?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->enum('type', ['personal', 'community', 'editorial'])->default('personal')->after('slug');
            $table->enum('collaboration_mode', ['closed', 'invite_only', 'open'])->default('closed')->after('summary');
            $table->boolean('allow_submissions')->default(false)->after('collaboration_mode');
            $table->boolean('allow_comments')->default(true)->after('allow_submissions');
            $table->boolean('allow_saves')->default(true)->after('allow_comments');
            $table->enum('moderation_status', ['active', 'under_review', 'restricted', 'hidden'])->default('active')->after('allow_saves');
            $table->unsignedInteger('comments_count')->default(0)->after('artworks_count');
            $table->unsignedInteger('saves_count')->default(0)->after('shares_count');
            $table->unsignedInteger('collaborators_count')->default(1)->after('saves_count');
            $table->timestamp('published_at')->nullable()->after('featured_at');
            $table->timestamp('unpublished_at')->nullable()->after('published_at');
            $table->string('event_key', 80)->nullable()->after('unpublished_at');
            $table->string('event_label', 120)->nullable()->after('event_key');
            $table->string('season_key', 80)->nullable()->after('event_label');
            $table->string('badge_label', 80)->nullable()->after('season_key');

            $table->index(['type', 'visibility', 'moderation_status'], 'collections_type_visibility_mod_idx');
            $table->index(['collaboration_mode', 'allow_submissions'], 'collections_collab_submission_idx');
        });

        Schema::create('collection_members', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('invited_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->enum('role', ['owner', 'editor', 'contributor', 'viewer'])->default('contributor');
            $table->enum('status', ['pending', 'active', 'revoked'])->default('pending');
            $table->string('note', 320)->nullable();
            $table->timestamp('invited_at')->nullable();
            $table->timestamp('accepted_at')->nullable();
            $table->timestamp('revoked_at')->nullable();
            $table->timestamps();

            $table->unique(['collection_id', 'user_id'], 'collection_members_unique');
            $table->index(['collection_id', 'status'], 'collection_members_status_idx');
        });

        Schema::create('collection_submissions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->text('message')->nullable();
            $table->enum('status', ['pending', 'approved', 'rejected', 'withdrawn'])->default('pending');
            $table->foreignId('reviewed_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamps();

            $table->index(['collection_id', 'status'], 'collection_submissions_status_idx');
            $table->unique(['collection_id', 'artwork_id', 'user_id'], 'collection_submissions_unique');
        });

        Schema::create('collection_saves', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['collection_id', 'user_id'], 'collection_saves_unique');
            $table->index('user_id');
        });

        Schema::create('collection_comments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('parent_id')->nullable()->constrained('collection_comments')->cascadeOnDelete();
            $table->text('body');
            $table->text('rendered_body');
            $table->enum('status', ['visible', 'hidden', 'flagged'])->default('visible');
            $table->timestamps();
            $table->softDeletes();

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

    public function down(): void
    {
        Schema::dropIfExists('collection_comments');
        Schema::dropIfExists('collection_saves');
        Schema::dropIfExists('collection_submissions');
        Schema::dropIfExists('collection_members');

        Schema::table('collections', function (Blueprint $table): void {
            $table->dropIndex('collections_type_visibility_mod_idx');
            $table->dropIndex('collections_collab_submission_idx');
            $table->dropColumn([
                'type',
                'collaboration_mode',
                'allow_submissions',
                'allow_comments',
                'allow_saves',
                'moderation_status',
                'comments_count',
                'saves_count',
                'collaborators_count',
                'published_at',
                'unpublished_at',
                'event_key',
                'event_label',
                'season_key',
                'badge_label',
            ]);
        });
    }
};
