<?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('mode', ['manual', 'smart'])->default('manual')->after('visibility');
            $table->unsignedInteger('profile_order')->nullable()->after('is_featured');
            $table->unsignedInteger('views_count')->default(0)->after('profile_order');
            $table->unsignedInteger('likes_count')->default(0)->after('views_count');
            $table->unsignedInteger('followers_count')->default(0)->after('likes_count');
            $table->unsignedInteger('shares_count')->default(0)->after('followers_count');
            $table->string('subtitle', 160)->nullable()->after('description');
            $table->text('summary')->nullable()->after('subtitle');
            $table->json('smart_rules_json')->nullable()->after('summary');
            $table->timestamp('last_activity_at')->nullable()->after('smart_rules_json');
            $table->timestamp('featured_at')->nullable()->after('last_activity_at');

            $table->index(['user_id', 'is_featured', 'featured_at'], 'collections_user_featured_idx');
            $table->index(['user_id', 'profile_order'], 'collections_user_profile_order_idx');
            $table->index(['mode', 'visibility'], 'collections_mode_visibility_idx');
        });

        Schema::create('collection_follows', 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_follows_unique');
            $table->index('user_id');
        });

        Schema::create('collection_likes', 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_likes_unique');
            $table->index('user_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('collection_likes');
        Schema::dropIfExists('collection_follows');

        Schema::table('collections', function (Blueprint $table): void {
            $table->dropIndex('collections_user_featured_idx');
            $table->dropIndex('collections_user_profile_order_idx');
            $table->dropIndex('collections_mode_visibility_idx');
            $table->dropColumn([
                'mode',
                'profile_order',
                'views_count',
                'likes_count',
                'followers_count',
                'shares_count',
                'subtitle',
                'summary',
                'smart_rules_json',
                'last_activity_at',
                'featured_at',
            ]);
        });
    }
};
