<?php

declare(strict_types=1);


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::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50);
            $table->string('slug', 120)->nullable()->unique();
            $table->tinyInteger('display_order')->default(0);
            $table->unsignedInteger('post_count')->default(0);
            $table->timestamps();
        });

        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('category_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('title');
            $table->string('slug', 255)->nullable()->unique();
            $table->text('excerpt')->nullable();
            $table->longText('content');
            $table->string('keywords', 255)->nullable();
            $table->string('status', 20)->default('published')->index();
            $table->boolean('is_sticky')->default(false);
            $table->boolean('close_comment')->default(false);
            $table->string('read_password', 255)->nullable();
            $table->unsignedBigInteger('views')->default(0);
            $table->unsignedInteger('comment_count')->default(0);
            $table->json('meta')->nullable();
            $table->timestamp('published_at')->nullable()->index();
            $table->timestamps();

            $table->index(['status', 'published_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('posts');
        Schema::dropIfExists('categories');
    }
};
