<?php

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

/**
 * Migration: Digital Health Advocacy - Campaigns Table
 * 
 * Creates the campaigns table for managing active health drives and advocacy campaigns.
 * Supports campaign banners, targeting, and status tracking.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_campaigns', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Key to Creator
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('Campaign creator/manager');
            
            // Campaign Information
            $table->string('title', 191)
                  ->comment('Campaign title');
            
            $table->string('slug', 191)
                  ->unique()
                  ->comment('URL-friendly identifier');
            
            $table->text('description')
                  ->comment('Campaign description and objectives');
            
            $table->text('objectives')
                  ->nullable()
                  ->comment('Specific campaign objectives');
            
            // Media
            $table->string('banner_image', 191)
                  ->nullable()
                  ->comment('Campaign banner image path');
            
            $table->string('thumbnail', 191)
                  ->nullable()
                  ->comment('Campaign thumbnail for cards');
            
            // Targeting
            $table->string('target_audience', 191)
                  ->nullable()
                  ->comment('Target audience description');
            
            $table->json('regions')
                  ->nullable()
                  ->comment('Target regions/locations');
            
            // Campaign Duration
            $table->date('start_date')
                  ->comment('Campaign start date');
            
            $table->date('end_date')
                  ->nullable()
                  ->comment('Campaign end date (null for ongoing)');
            
            // Status
            $table->enum('status', ['draft', 'active', 'paused', 'completed', 'archived'])
                  ->default('draft')
                  ->comment('Campaign status');
            
            $table->boolean('is_featured')
                  ->default(false)
                  ->comment('Featured campaign flag');
            
            // SEO Metadata
            $table->string('meta_title', 191)
                  ->nullable()
                  ->comment('SEO meta title');
            
            $table->text('meta_description')
                  ->nullable()
                  ->comment('SEO meta description');
            
            $table->json('meta_keywords')
                  ->nullable()
                  ->comment('SEO keywords');
            
            // Statistics (cached)
            $table->unsignedInteger('views_count')
                  ->default(0)
                  ->comment('Total campaign views');
            
            $table->unsignedInteger('stories_count')
                  ->default(0)
                  ->comment('Associated success stories count');
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('status');
            $table->index('start_date');
            $table->index('end_date');
            $table->index('is_featured');
            $table->index('slug');
            $table->index(['status', 'start_date', 'end_date'], 'r_campaigns_active_index');
        });
    }

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