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

            $table->foreignId('post_id')
                ->constrained('posts')
                ->cascadeOnDelete()
                ->comment('文章');

            // kind：檔案用途
            // - image：圖片（可做多張圖）
            // - file：檔案（PDF 等）
            $table->string('kind', 20)->comment('類型');

            $table->string('file_path', 255)->comment('路徑');
            $table->string('file_name', 255)->nullable()->comment('檔名');
            $table->string('mime_type', 100)->nullable()->comment('MIME');
            $table->unsignedInteger('file_size')->nullable()->comment('大小');

            $table->string('alt_text', 255)->nullable()->comment('替代文字');

            $table->unsignedInteger('sort_order')->default(0)->comment('排序');
            $table->boolean('is_active')->default(true)->comment('啟用');

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

            $table->index(['post_id', 'kind', 'sort_order']);
        });
    }

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