<?php

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

/**
 * Migration: Research and Innovation Centre - Publication Files Table
 * 
 * Stores file attachments for research publications including PDFs, documents,
 * and supporting materials. Supports version control for revision workflows.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_publication_files', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Key to Publications
            $table->foreignId('publication_id')
                  ->constrained('r_publications')
                  ->onDelete('cascade')
                  ->comment('Associated publication');
            
            // File Information
            $table->string('original_name', 191)
                  ->comment('Original filename from upload');
            
            $table->string('stored_name', 191)
                  ->comment('Server-side stored filename');
            
            $table->string('file_path', 191)
                  ->comment('Full path to stored file');
            
            $table->unsignedBigInteger('file_size')
                  ->default(0)
                  ->comment('File size in bytes');
            
            $table->string('mime_type', 100)
                  ->comment('MIME type of the file');
            
            $table->enum('file_type', ['pdf', 'doc', 'docx', 'image', 'other'])
                  ->default('pdf')
                  ->comment('Categorized file type');
            
            // Access Control
            $table->boolean('is_public')
                  ->default(false)
                  ->comment('Whether file is publicly accessible');
            
            // Version Control
            $table->unsignedInteger('version')
                  ->default(1)
                  ->comment('File version number for revisions');
            
            $table->foreignId('replaces_file_id')
                  ->nullable()
                  ->constrained('r_publication_files')
                  ->nullOnDelete()
                  ->comment('Previous version of this file');
            
            // Description
            $table->string('description', 500)
                  ->nullable()
                  ->comment('Optional file description');
            
            // Standard Timestamps
            $table->timestamps();
            
            // Indexes
            $table->index('publication_id');
            $table->index('file_type');
            $table->index('is_public');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('r_publication_files');
    }
};
