<?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('lifecycle_state', ['draft', 'scheduled', 'published', 'featured', 'archived', 'hidden', 'restricted', 'under_review', 'expired'])
                ->default('draft')
                ->after('slug');
            $table->decimal('quality_score', 6, 2)->nullable()->after('spotlight_style');
            $table->decimal('ranking_score', 6, 2)->nullable()->after('quality_score');
            $table->boolean('analytics_enabled')->default(true)->after('ranking_score');
            $table->enum('presentation_style', ['standard', 'editorial_grid', 'hero_grid', 'masonry'])->default('standard')->after('analytics_enabled');
            $table->enum('emphasis_mode', ['cover_heavy', 'balanced', 'artwork_first'])->default('balanced')->after('presentation_style');
            $table->string('theme_token', 40)->nullable()->after('emphasis_mode');
            $table->string('series_key', 80)->nullable()->after('theme_token');
            $table->unsignedInteger('series_order')->nullable()->after('series_key');
            $table->string('campaign_key', 80)->nullable()->after('series_order');
            $table->string('campaign_label', 120)->nullable()->after('campaign_key');
            $table->boolean('commercial_eligibility')->default(false)->after('campaign_label');
            $table->string('promotion_tier', 40)->nullable()->after('commercial_eligibility');
            $table->string('sponsorship_label', 120)->nullable()->after('promotion_tier');
            $table->string('partner_label', 120)->nullable()->after('sponsorship_label');
            $table->string('monetization_ready_status', 40)->nullable()->after('partner_label');
            $table->string('brand_safe_status', 40)->nullable()->after('monetization_ready_status');
            $table->timestamp('archived_at')->nullable()->after('unpublished_at');
            $table->timestamp('expired_at')->nullable()->after('archived_at');
            $table->unsignedInteger('history_count')->default(0)->after('expired_at');

            $table->index(['lifecycle_state', 'moderation_status', 'visibility'], 'collections_lifecycle_visibility_idx');
            $table->index(['series_key', 'series_order'], 'collections_series_idx');
            $table->index(['campaign_key', 'lifecycle_state'], 'collections_campaign_lifecycle_idx');
        });

        Schema::create('collection_history', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->foreignId('actor_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('action_type', 80);
            $table->string('summary', 255)->nullable();
            $table->json('before_json')->nullable();
            $table->json('after_json')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index('collection_id');
            $table->index('action_type');
            $table->index('actor_user_id');
        });

        Schema::create('collection_surface_definitions', function (Blueprint $table): void {
            $table->id();
            $table->string('surface_key', 120)->unique();
            $table->string('title', 120);
            $table->string('description', 255)->nullable();
            $table->enum('mode', ['manual', 'automatic', 'hybrid'])->default('manual');
            $table->json('rules_json')->nullable();
            $table->string('ranking_mode', 60)->nullable();
            $table->unsignedInteger('max_items')->default(12);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('collection_surface_placements', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->string('surface_key', 120);
            $table->string('placement_type', 60);
            $table->unsignedInteger('priority')->default(0);
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->boolean('is_active')->default(true);
            $table->string('campaign_key', 80)->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('created_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index('surface_key');
            $table->index('starts_at');
            $table->index('ends_at');
            $table->index('is_active');
        });

        Schema::create('collection_daily_stats', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->date('stat_date');
            $table->unsignedInteger('views_count')->default(0);
            $table->unsignedInteger('likes_count')->default(0);
            $table->unsignedInteger('follows_count')->default(0);
            $table->unsignedInteger('saves_count')->default(0);
            $table->unsignedInteger('comments_count')->default(0);
            $table->unsignedInteger('shares_count')->default(0);
            $table->unsignedInteger('submissions_count')->default(0);
            $table->timestamps();

            $table->unique(['collection_id', 'stat_date'], 'collection_daily_stats_unique');
        });

        Schema::create('collection_saved_lists', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('title', 120);
            $table->string('slug', 140);
            $table->timestamps();

            $table->unique(['user_id', 'slug'], 'collection_saved_lists_user_slug_unique');
        });

        Schema::create('collection_saved_list_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('saved_list_id')->constrained('collection_saved_lists')->cascadeOnDelete();
            $table->foreignId('collection_id')->constrained('collections')->cascadeOnDelete();
            $table->unsignedInteger('order_num')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['saved_list_id', 'collection_id'], 'collection_saved_list_items_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('collection_saved_list_items');
        Schema::dropIfExists('collection_saved_lists');
        Schema::dropIfExists('collection_daily_stats');
        Schema::dropIfExists('collection_surface_placements');
        Schema::dropIfExists('collection_surface_definitions');
        Schema::dropIfExists('collection_history');

        Schema::table('collections', function (Blueprint $table): void {
            $table->dropIndex('collections_lifecycle_visibility_idx');
            $table->dropIndex('collections_series_idx');
            $table->dropIndex('collections_campaign_lifecycle_idx');
            $table->dropColumn([
                'lifecycle_state',
                'quality_score',
                'ranking_score',
                'analytics_enabled',
                'presentation_style',
                'emphasis_mode',
                'theme_token',
                'series_key',
                'series_order',
                'campaign_key',
                'campaign_label',
                'commercial_eligibility',
                'promotion_tier',
                'sponsorship_label',
                'partner_label',
                'monetization_ready_status',
                'brand_safe_status',
                'archived_at',
                'expired_at',
                'history_count',
            ]);
        });
    }
};
