<?php

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

/**
 * Migration: Research and Innovation Centre - Publications Table
 * 
 * Creates the main publications table for the Research and Innovation Centre module.
 * This table stores all research submissions including scholarly works and innovative projects.
 * 
 * Features:
 * - Full-text indexing for search functionality (MySQL 5.7 compatible)
 * - Status workflow tracking (draft, under_review, revision_required, approved, featured)
 * - Category classification (clinical, tech, social)
 * - Soft deletes for content preservation
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_publications', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Key to Users (Author)
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('The author/creator of the publication');
            
            // Core Publication Fields
            $table->string('title', 191)
                  ->comment('Publication title, indexed for search');
            
            $table->text('abstract')
                  ->comment('Brief summary of the research');
            
            $table->text('methodology')
                  ->nullable()
                  ->comment('Research methodology description');
            
            $table->text('findings')
                  ->nullable()
                  ->comment('Research findings and results');
            
            $table->text('references')
                  ->nullable()
                  ->comment('Academic references and citations');
            
            // Classification
            $table->enum('category', ['clinical', 'tech', 'social'])
                  ->default('tech')
                  ->comment('Research category classification');
            
            $table->enum('status', ['draft', 'under_review', 'revision_required', 'approved', 'featured'])
                  ->default('draft')
                  ->comment('Publication workflow status');
            
            // Featured Flag (for Innovation Showcase)
            $table->boolean('is_featured')
                  ->default(false)
                  ->comment('Flag for featured publications in showcase');
            
            // Cover Image
            $table->string('cover_image', 191)
                  ->nullable()
                  ->comment('Path to cover image for public display');
            
            // Timestamps for Workflow
            $table->timestamp('submitted_at')
                  ->nullable()
                  ->comment('When the publication was submitted for review');
            
            $table->timestamp('approved_at')
                  ->nullable()
                  ->comment('When the publication was approved');
            
            $table->foreignId('approved_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('User who approved the publication');
            
            // Standard Timestamps
            $table->timestamps();
            
            // Soft Deletes
            $table->softDeletes();
            
            // Indexes for Performance
            $table->index('title');
            $table->index('status');
            $table->index('category');
            $table->index('user_id');
            $table->index('created_at');
            $table->index(['status', 'category'], 'r_publications_status_category_index');
            $table->index('is_featured');
        });
        
        // Add Full-Text Index for MySQL (title, abstract, methodology, findings)
        // Note: SQLite doesn't support FULLTEXT, so this is wrapped in a check
        if (config('database.default') === 'mysql') {
            DB::statement(
                'ALTER TABLE r_publications ADD FULLTEXT INDEX r_publications_fulltext_index (title, abstract, methodology, findings)'
            );
        }
    }

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