<?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('posts', function (Blueprint $table) {
            // Pinned posts
            $table->boolean('is_pinned')->default(false)->after('meta');
            $table->unsignedTinyInteger('pinned_order')->nullable()->after('is_pinned');

            // Scheduled posts
            $table->timestamp('publish_at')->nullable()->after('pinned_order');
            $table->string('status', 16)->default('published')->after('publish_at'); // draft | scheduled | published

            // Analytics
            $table->unsignedBigInteger('impressions_count')->default(0)->after('comments_count');
            $table->float('engagement_score')->default(0.0)->after('impressions_count');
            $table->unsignedInteger('saves_count')->default(0)->after('engagement_score');

            $table->index(['is_pinned', 'user_id', 'pinned_order']);
            $table->index(['status', 'publish_at']);
            $table->index(['status', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropIndex(['is_pinned', 'user_id', 'pinned_order']);
            $table->dropIndex(['status', 'publish_at']);
            $table->dropIndex(['status', 'created_at']);
            $table->dropColumn(['is_pinned', 'pinned_order', 'publish_at', 'status',
                                 'impressions_count', 'engagement_score', 'saves_count']);
        });
    }
};
