<?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('posts', function (Blueprint $table) {
            $table->id()->comment('主鍵');

            // section：用來區分不同內容管理項目
            // - club_news：最新消息
            // - spa_news：怡SPA-最新消息
            $table->string('section', 50)->comment('區塊');
            $table->foreignId('venue_id')
                ->nullable()
                ->constrained('venues')
                ->nullOnDelete()
                ->comment('場館');

            // slug：同一 section 下唯一
            // content：可存 HTML/編輯器輸出
            $table->string('title', 200)->comment('標題');
            $table->string('slug', 200)->comment('Slug');
            $table->string('cover_image_path', 255)->nullable()->comment('封面');
            $table->string('summary', 500)->nullable()->comment('摘要');
            $table->longText('content')->nullable()->comment('內容');

            $table->timestamp('published_at')->nullable()->comment('發布');
            $table->boolean('is_published')->default(false)->comment('發布中');
            $table->boolean('is_pinned')->default(false)->comment('首頁顯示');

            $table->unsignedInteger('sort_order')->default(0)->comment('排序');

            $table->timestamp('created_at')->nullable()->comment('建立');
            $table->timestamp('updated_at')->nullable()->comment('更新');
            $table->timestamp('deleted_at')->nullable()->comment('刪除');

            $table->unique(['section', 'slug']);
            $table->index(['section', 'is_published', 'published_at']);
            $table->index(['section', 'is_pinned']);
            $table->index(['venue_id']);
        });
    }

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