<?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('conversation_participants', function (Blueprint $table): void {
            if (! Schema::hasColumn('conversation_participants', 'is_pinned')) {
                $table->boolean('is_pinned')->default(false)->after('is_archived');
            }

            if (! Schema::hasColumn('conversation_participants', 'pinned_at')) {
                $table->timestamp('pinned_at')->nullable()->after('is_pinned');
            }

            $table->index(['user_id', 'is_pinned']);
        });
    }

    public function down(): void
    {
        Schema::table('conversation_participants', function (Blueprint $table): void {
            if (Schema::hasColumn('conversation_participants', 'pinned_at')) {
                $table->dropColumn('pinned_at');
            }
            if (Schema::hasColumn('conversation_participants', 'is_pinned')) {
                $table->dropColumn('is_pinned');
            }
        });
    }
};
