<?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('group_projects', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->string('title', 180);
            $table->string('slug', 190)->unique();
            $table->string('summary', 320)->nullable();
            $table->longText('description')->nullable();
            $table->string('cover_path', 2048)->nullable();
            $table->string('status', 24)->default('planned');
            $table->string('visibility', 24)->default('public');
            $table->date('start_date')->nullable();
            $table->date('target_date')->nullable();
            $table->timestamp('released_at')->nullable();
            $table->foreignId('created_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('lead_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('linked_collection_id')->nullable()->constrained('collections')->nullOnDelete();
            $table->foreignId('linked_featured_artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
            $table->foreignId('pinned_post_id')->nullable()->constrained('group_posts')->nullOnDelete();
            $table->softDeletes();
            $table->timestamps();

            $table->index(['group_id', 'status', 'visibility'], 'group_projects_group_status_visibility_idx');
            $table->index(['group_id', 'released_at'], 'group_projects_group_released_idx');
        });

        Schema::create('group_project_artworks', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_project_id')->constrained('group_projects')->cascadeOnDelete();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['group_project_id', 'artwork_id'], 'group_project_artworks_unique');
            $table->index(['group_project_id', 'sort_order'], 'group_project_artworks_sort_idx');
        });

        Schema::create('group_project_members', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_project_id')->constrained('group_projects')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('role_label', 80)->nullable();
            $table->boolean('is_lead')->default(false);
            $table->timestamps();

            $table->unique(['group_project_id', 'user_id'], 'group_project_members_unique');
            $table->index(['group_project_id', 'is_lead'], 'group_project_members_lead_idx');
        });

        Schema::create('group_challenges', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->string('title', 180);
            $table->string('slug', 190)->unique();
            $table->string('summary', 320)->nullable();
            $table->longText('description')->nullable();
            $table->string('cover_path', 2048)->nullable();
            $table->string('visibility', 24)->default('public');
            $table->string('participation_scope', 24)->default('group_only');
            $table->string('status', 24)->default('draft');
            $table->timestamp('start_at')->nullable();
            $table->timestamp('end_at')->nullable();
            $table->text('rules_text')->nullable();
            $table->text('submission_instructions')->nullable();
            $table->string('judging_mode', 32)->nullable();
            $table->foreignId('linked_collection_id')->nullable()->constrained('collections')->nullOnDelete();
            $table->foreignId('linked_project_id')->nullable()->constrained('group_projects')->nullOnDelete();
            $table->foreignId('created_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('featured_artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
            $table->softDeletes();
            $table->timestamps();

            $table->index(['group_id', 'status', 'visibility'], 'group_challenges_group_status_visibility_idx');
            $table->index(['group_id', 'start_at', 'end_at'], 'group_challenges_group_window_idx');
        });

        Schema::create('group_challenge_artworks', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_challenge_id')->constrained('group_challenges')->cascadeOnDelete();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->foreignId('submitted_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['group_challenge_id', 'artwork_id'], 'group_challenge_artworks_unique');
            $table->index(['group_challenge_id', 'sort_order'], 'group_challenge_artworks_sort_idx');
        });

        Schema::create('group_events', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->string('title', 180);
            $table->string('slug', 190)->unique();
            $table->string('summary', 320)->nullable();
            $table->longText('description')->nullable();
            $table->string('event_type', 32)->default('launch');
            $table->string('visibility', 24)->default('public');
            $table->timestamp('start_at')->nullable();
            $table->timestamp('end_at')->nullable();
            $table->string('timezone', 80)->default('UTC');
            $table->string('cover_path', 2048)->nullable();
            $table->string('location', 180)->nullable();
            $table->string('external_url', 2048)->nullable();
            $table->foreignId('linked_project_id')->nullable()->constrained('group_projects')->nullOnDelete();
            $table->foreignId('linked_collection_id')->nullable()->constrained('collections')->nullOnDelete();
            $table->foreignId('linked_challenge_id')->nullable()->constrained('group_challenges')->nullOnDelete();
            $table->string('status', 24)->default('draft');
            $table->boolean('is_featured')->default(false);
            $table->foreignId('created_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->timestamp('published_at')->nullable();
            $table->softDeletes();
            $table->timestamps();

            $table->index(['group_id', 'status', 'visibility'], 'group_events_group_status_visibility_idx');
            $table->index(['group_id', 'start_at'], 'group_events_group_start_idx');
        });

        Schema::create('group_assets', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->string('title', 180);
            $table->text('description')->nullable();
            $table->string('category', 32)->default('misc');
            $table->string('file_path', 2048);
            $table->string('preview_path', 2048)->nullable();
            $table->string('visibility', 24)->default('members_only');
            $table->string('status', 24)->default('active');
            $table->foreignId('linked_project_id')->nullable()->constrained('group_projects')->nullOnDelete();
            $table->foreignId('uploaded_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('approved_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->boolean('is_featured')->default(false);
            $table->json('file_meta_json')->nullable();
            $table->softDeletes();
            $table->timestamps();

            $table->index(['group_id', 'visibility', 'status'], 'group_assets_group_visibility_status_idx');
            $table->index(['group_id', 'category'], 'group_assets_group_category_idx');
        });

        Schema::create('group_activity_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->string('type', 48);
            $table->string('visibility', 24)->default('public');
            $table->foreignId('actor_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('subject_type', 64);
            $table->unsignedBigInteger('subject_id')->nullable();
            $table->string('headline', 255);
            $table->string('summary', 500)->nullable();
            $table->boolean('is_pinned')->default(false);
            $table->timestamp('occurred_at');
            $table->timestamps();

            $table->index(['group_id', 'visibility', 'occurred_at'], 'group_activity_items_group_visibility_occurred_idx');
            $table->index(['group_id', 'type', 'occurred_at'], 'group_activity_items_group_type_occurred_idx');
            $table->index(['group_id', 'subject_type', 'subject_id'], 'group_activity_items_subject_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('group_activity_items');
        Schema::dropIfExists('group_assets');
        Schema::dropIfExists('group_events');
        Schema::dropIfExists('group_challenge_artworks');
        Schema::dropIfExists('group_challenges');
        Schema::dropIfExists('group_project_members');
        Schema::dropIfExists('group_project_artworks');
        Schema::dropIfExists('group_projects');
    }
};