<?php

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

/**
 * Migration: Digital Health Advocacy - Health Stories Table
 * 
 * Stores impact and success stories submitted by both authenticated users and guests.
 * Supports public submission with moderation workflow.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_health_stories', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Key to Campaign (optional)
            $table->foreignId('campaign_id')
                  ->nullable()
                  ->constrained('r_campaigns')
                  ->nullOnDelete()
                  ->comment('Associated campaign (optional)');
            
            // Author Information (for both registered users and guests)
            $table->foreignId('user_id')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('Registered user author (null for guests)');
            
            $table->string('author_name', 191)
                  ->nullable()
                  ->comment('Guest author name');
            
            $table->string('author_email', 191)
                  ->nullable()
                  ->comment('Guest author email');
            
            $table->string('author_phone', 50)
                  ->nullable()
                  ->comment('Guest author phone');
            
            // Story Content
            $table->string('title', 191)
                  ->comment('Story title');
            
            $table->string('slug', 191)
                  ->unique()
                  ->comment('URL-friendly identifier');
            
            $table->text('excerpt')
                  ->nullable()
                  ->comment('Short summary for listings');
            
            $table->text('content')
                  ->comment('Full story content');
            
            // Location
            $table->string('location', 191)
                  ->nullable()
                  ->comment('Story location/region');
            
            // Media
            $table->string('featured_image', 191)
                  ->nullable()
                  ->comment('Featured image path');
            
            $table->json('gallery')
                  ->nullable()
                  ->comment('Additional images gallery');
            
            $table->string('video_url', 191)
                  ->nullable()
                  ->comment('YouTube/Vimeo video URL');
            
            // Status & Moderation
            $table->enum('status', ['draft', 'pending_review', 'approved', 'rejected', 'published'])
                  ->default('pending_review')
                  ->comment('Story status');
            
            $table->boolean('is_featured')
                  ->default(false)
                  ->comment('Featured story flag');
            
            $table->text('rejection_reason')
                  ->nullable()
                  ->comment('Reason for rejection if applicable');
            
            $table->foreignId('moderated_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('User who moderated the story');
            
            $table->timestamp('moderated_at')
                  ->nullable()
                  ->comment('When the story was moderated');
            
            $table->timestamp('published_at')
                  ->nullable()
                  ->comment('When the story was published');
            
            // SEO
            $table->string('meta_title', 191)
                  ->nullable();
            
            $table->text('meta_description')
                  ->nullable();
            
            // Statistics
            $table->unsignedInteger('views_count')
                  ->default(0);
            
            $table->unsignedInteger('shares_count')
                  ->default(0);
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('status');
            $table->index('campaign_id');
            $table->index('user_id');
            $table->index('slug');
            $table->index('is_featured');
            $table->index('published_at');
            $table->index(['status', 'published_at'], 'r_health_stories_published_index');
        });
    }

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