<?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_join_requests', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')
                ->constrained('groups')
                ->cascadeOnDelete();
            $table->foreignId('user_id')
                ->constrained('users')
                ->cascadeOnDelete();
            $table->text('message')->nullable();
            $table->string('portfolio_url', 2048)->nullable();
            $table->string('desired_role', 32)->nullable();
            $table->json('skills_json')->nullable();
            $table->string('status', 24)->default('pending');
            $table->foreignId('reviewed_by_user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->text('review_notes')->nullable();
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();

            $table->index(['group_id', 'status', 'created_at'], 'group_join_requests_group_status_idx');
            $table->index(['group_id', 'user_id', 'status'], 'group_join_requests_group_user_status_idx');
        });

        Schema::create('group_posts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')
                ->constrained('groups')
                ->cascadeOnDelete();
            $table->foreignId('author_user_id')
                ->constrained('users')
                ->cascadeOnDelete();
            $table->string('type', 32)->default('announcement');
            $table->string('title', 180);
            $table->string('slug', 190)->unique();
            $table->string('excerpt', 320)->nullable();
            $table->longText('content')->nullable();
            $table->string('cover_path', 2048)->nullable();
            $table->string('status', 24)->default('draft');
            $table->boolean('is_pinned')->default(false);
            $table->timestamp('published_at')->nullable();
            $table->softDeletes();
            $table->timestamps();

            $table->index(['group_id', 'status', 'published_at'], 'group_posts_group_status_published_idx');
            $table->index(['group_id', 'is_pinned', 'published_at'], 'group_posts_group_pinned_idx');
        });

        Schema::create('group_recruitment_profiles', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')
                ->constrained('groups')
                ->cascadeOnDelete();
            $table->boolean('is_recruiting')->default(false);
            $table->string('headline', 180)->nullable();
            $table->text('description')->nullable();
            $table->json('roles_json')->nullable();
            $table->json('skills_json')->nullable();
            $table->string('contact_mode', 32)->nullable();
            $table->string('visibility', 24)->default('public');
            $table->timestamps();

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

        Schema::create('group_histories', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('group_id')
                ->constrained('groups')
                ->cascadeOnDelete();
            $table->foreignId('actor_user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();
            $table->string('action_type', 64);
            $table->string('target_type', 64)->nullable();
            $table->unsignedBigInteger('target_id')->nullable();
            $table->string('summary', 255)->nullable();
            $table->json('before_json')->nullable();
            $table->json('after_json')->nullable();
            $table->timestamp('created_at')->nullable();

            $table->index(['group_id', 'action_type', 'created_at'], 'group_histories_group_action_idx');
            $table->index(['group_id', 'target_type', 'target_id'], 'group_histories_target_idx');
        });

        Schema::table('group_members', function (Blueprint $table): void {
            if (! Schema::hasColumn('group_members', 'permission_overrides_json')) {
                $table->json('permission_overrides_json')->nullable()->after('note');
            }
        });

        Schema::table('artworks', function (Blueprint $table): void {
            if (! Schema::hasColumn('artworks', 'group_review_status')) {
                $table->string('group_review_status', 24)->default('none')->after('artwork_status');
            }

            if (! Schema::hasColumn('artworks', 'group_review_submitted_at')) {
                $table->timestamp('group_review_submitted_at')->nullable()->after('group_review_status');
            }

            if (! Schema::hasColumn('artworks', 'group_reviewed_by_user_id')) {
                $table->foreignId('group_reviewed_by_user_id')
                    ->nullable()
                    ->after('group_review_submitted_at')
                    ->constrained('users')
                    ->nullOnDelete();
            }

            if (! Schema::hasColumn('artworks', 'group_reviewed_at')) {
                $table->timestamp('group_reviewed_at')->nullable()->after('group_reviewed_by_user_id');
            }

            if (! Schema::hasColumn('artworks', 'group_review_notes')) {
                $table->text('group_review_notes')->nullable()->after('group_reviewed_at');
            }

            $table->index(['group_id', 'group_review_status', 'group_review_submitted_at'], 'artworks_group_review_queue_idx');
        });
    }

    public function down(): void
    {
        Schema::table('artworks', function (Blueprint $table): void {
            if (Schema::hasColumn('artworks', 'group_reviewed_by_user_id')) {
                $table->dropConstrainedForeignId('group_reviewed_by_user_id');
            }

            if (Schema::hasColumn('artworks', 'group_review_status')) {
                $table->dropIndex('artworks_group_review_queue_idx');
                $table->dropColumn([
                    'group_review_status',
                    'group_review_submitted_at',
                    'group_reviewed_at',
                    'group_review_notes',
                ]);
            }
        });

        Schema::table('group_members', function (Blueprint $table): void {
            if (Schema::hasColumn('group_members', 'permission_overrides_json')) {
                $table->dropColumn('permission_overrides_json');
            }
        });

        Schema::dropIfExists('group_histories');
        Schema::dropIfExists('group_recruitment_profiles');
        Schema::dropIfExists('group_posts');
        Schema::dropIfExists('group_join_requests');
    }
};