<?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::create('kb_categories', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('parent_id')->default(0)->index();
            $table->string('name', 100);
            $table->integer('sort')->default(0);
            $table->tinyInteger('status')->default(1);
            $table->timestamps();
        });

        Schema::create('kb_articles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('store_id')->index();
            $table->unsignedBigInteger('category_id')->nullable()->index();
            $table->string('title');
            $table->longText('content')->nullable();
            $table->unsignedBigInteger('author_id')->nullable();
            $table->tinyInteger('status')->default(0)->comment('0=草稿 1=待审核 2=已发布 3=已下架');
            $table->tinyInteger('visibility')->default(1)->comment('1=内部 2=公开');
            $table->string('cover_image')->nullable();
            $table->json('attachments')->nullable();
            $table->unsignedInteger('views')->default(0);
            $table->unsignedInteger('likes')->default(0);
            $table->unsignedInteger('favorites')->default(0);
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
            $table->fullText(['title', 'content']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kb_articles');
        Schema::dropIfExists('kb_categories');
    }
};
