<?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_releases', 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', 32)->default('planned');
            $table->string('current_stage', 32)->default('concept');
            $table->string('visibility', 24)->default('public');
            $table->timestamp('planned_release_at')->nullable();
            $table->timestamp('released_at')->nullable();
            $table->foreignId('lead_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('linked_project_id')->nullable()->constrained('group_projects')->nullOnDelete();
            $table->foreignId('linked_collection_id')->nullable()->constrained('collections')->nullOnDelete();
            $table->foreignId('featured_artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
            $table->longText('release_notes')->nullable();
            $table->foreignId('created_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->timestamp('published_at')->nullable();
            $table->boolean('is_featured')->default(false);
            $table->softDeletes();
            $table->timestamps();

            $table->index(['group_id', 'status', 'visibility'], 'group_releases_group_status_visibility_idx');
            $table->index(['group_id', 'current_stage'], 'group_releases_group_stage_idx');
            $table->index(['group_id', 'planned_release_at'], 'group_releases_group_planned_idx');
            $table->index(['group_id', 'released_at'], 'group_releases_group_released_idx');
        });

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

            $table->unique(['group_release_id', 'artwork_id'], 'group_release_artworks_unique');
            $table->index(['group_release_id', 'sort_order'], 'group_release_artworks_sort_idx');
        });

        Schema::create('group_release_contributors', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_release_id')->constrained('group_releases')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('role_label', 80)->nullable();
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['group_release_id', 'user_id'], 'group_release_contributors_unique');
            $table->index(['group_release_id', 'sort_order'], 'group_release_contributors_sort_idx');
        });

        Schema::create('group_project_milestones', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_project_id')->constrained('group_projects')->cascadeOnDelete();
            $table->string('title', 180);
            $table->string('summary', 320)->nullable();
            $table->string('status', 24)->default('pending');
            $table->date('due_date')->nullable();
            $table->foreignId('owner_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['group_project_id', 'status'], 'group_project_milestones_project_status_idx');
            $table->index(['group_project_id', 'due_date'], 'group_project_milestones_project_due_idx');
        });

        Schema::create('group_release_milestones', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_release_id')->constrained('group_releases')->cascadeOnDelete();
            $table->string('title', 180);
            $table->string('summary', 320)->nullable();
            $table->string('status', 24)->default('pending');
            $table->date('due_date')->nullable();
            $table->foreignId('owner_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['group_release_id', 'status'], 'group_release_milestones_release_status_idx');
            $table->index(['group_release_id', 'due_date'], 'group_release_milestones_release_due_idx');
        });

        Schema::create('group_contributor_stats', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->unsignedInteger('credited_artworks_count')->default(0);
            $table->unsignedInteger('release_count')->default(0);
            $table->unsignedInteger('project_count')->default(0);
            $table->unsignedInteger('review_actions_count')->default(0);
            $table->unsignedInteger('approved_submissions_count')->default(0);
            $table->json('reputation_meta_json')->nullable();
            $table->timestamps();

            $table->unique(['group_id', 'user_id'], 'group_contributor_stats_group_user_unique');
            $table->index(['group_id', 'release_count'], 'group_contributor_stats_group_release_idx');
            $table->index(['group_id', 'credited_artworks_count'], 'group_contributor_stats_group_artworks_idx');
        });

        Schema::create('group_badges', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->string('badge_key', 80);
            $table->timestamp('awarded_at');
            $table->json('meta_json')->nullable();
            $table->timestamps();

            $table->unique(['group_id', 'badge_key'], 'group_badges_group_badge_unique');
        });

        Schema::create('group_member_badges', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('badge_key', 80);
            $table->timestamp('awarded_at');
            $table->json('meta_json')->nullable();
            $table->timestamps();

            $table->unique(['group_id', 'user_id', 'badge_key'], 'group_member_badges_group_user_badge_unique');
            $table->index(['group_id', 'user_id'], 'group_member_badges_group_user_idx');
        });

        Schema::create('group_discovery_metrics', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')->constrained('groups')->cascadeOnDelete();
            $table->decimal('freshness_score', 8, 2)->default(0);
            $table->decimal('activity_score', 8, 2)->default(0);
            $table->decimal('release_score', 8, 2)->default(0);
            $table->decimal('trust_score', 8, 2)->default(0);
            $table->decimal('collaboration_score', 8, 2)->default(0);
            $table->timestamp('last_calculated_at')->nullable();
            $table->timestamps();

            $table->unique('group_id', 'group_discovery_metrics_group_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('group_discovery_metrics');
        Schema::dropIfExists('group_member_badges');
        Schema::dropIfExists('group_badges');
        Schema::dropIfExists('group_contributor_stats');
        Schema::dropIfExists('group_release_milestones');
        Schema::dropIfExists('group_project_milestones');
        Schema::dropIfExists('group_release_contributors');
        Schema::dropIfExists('group_release_artworks');
        Schema::dropIfExists('group_releases');
    }
};