<?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::dropIfExists('trackbacks');

        Schema::table('articles', function (Blueprint $table) {
            if (Schema::hasColumn('articles', 'trackbacks_count')) {
                $table->dropColumn('trackbacks_count');
            }
            if (Schema::hasColumn('articles', 'close_trackback')) {
                $table->dropColumn('close_trackback');
            }
            if (! Schema::hasColumn('articles', 'slug')) {
                $table->string('slug')->nullable()->unique()->after('keywords');
            }
        });
    }

    public function down(): void
    {
        Schema::table('articles', function (Blueprint $table) {
            if (Schema::hasColumn('articles', 'slug')) {
                $table->dropColumn('slug');
            }
            $table->unsignedInteger('trackbacks_count')->default(0);
            $table->boolean('close_trackback')->default(false);
        });

        Schema::create('trackbacks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('article_id')->constrained()->cascadeOnDelete();
            $table->string('title');
            $table->text('excerpt')->nullable();
            $table->string('url');
            $table->string('blog_name')->nullable();
            $table->string('ip', 45)->nullable();
            $table->boolean('visible')->default(false);
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
        });
    }
};
