<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Migration: Forum Attachments Table
 *
 * Stores file attachments for forum threads and replies.
 * Supports images, PDFs, Word documents, and Excel files.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('r_forum_attachments', function (Blueprint $table) {
            $table->id();

            // Polymorphic: thread or reply
            $table->string('attachable_type', 100)->comment('App\\Models\\RForumThread or App\\Models\\RForumReply');
            $table->unsignedBigInteger('attachable_id')->comment('ID of the thread or reply');

            $table->foreignId('user_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('Uploader');

            $table->string('original_name', 191)->comment('Original file name');
            $table->string('stored_name', 191)->comment('Stored file name on disk');
            $table->string('path', 500)->comment('Storage path');
            $table->string('disk', 50)->default('public')->comment('Storage disk');
            $table->string('mime_type', 100)->nullable()->comment('MIME type');
            $table->unsignedBigInteger('size')->default(0)->comment('File size in bytes');
            $table->string('extension', 20)->nullable()->comment('File extension');

            $table->enum('type', ['image', 'pdf', 'document', 'spreadsheet', 'other'])
                  ->default('other')
                  ->comment('File category');

            $table->timestamps();

            $table->index(['attachable_type', 'attachable_id'], 'r_forum_attachments_poly_index');
            $table->index('user_id');
        });
    }

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