<?php

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

/**
 * Migration: Digital Health Advocacy - Polls Table
 * 
 * Creates the main polls table for community sentiment and feedback polling.
 * Supports single-choice and multiple-choice polls with real-time results.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_polls', 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)');
            
            // Creator
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('Poll creator');
            
            // Poll Information
            $table->string('question', 500)
                  ->comment('Poll question');
            
            $table->text('description')
                  ->nullable()
                  ->comment('Additional context/description');
            
            // Poll Type
            $table->enum('poll_type', ['single_choice', 'multiple_choice'])
                  ->default('single_choice')
                  ->comment('Type of poll');
            
            $table->unsignedTinyInteger('max_selections')
                  ->default(1)
                  ->comment('Maximum selections for multiple choice');
            
            // Settings
            $table->boolean('allow_other')
                  ->default(false)
                  ->comment('Allow write-in "other" option');
            
            $table->boolean('show_results')
                  ->default(true)
                  ->comment('Show results after voting');
            
            $table->boolean('results_public')
                  ->default(true)
                  ->comment('Results visible to public');
            
            // Duration
            $table->timestamp('starts_at')
                  ->nullable()
                  ->comment('Poll start time');
            
            $table->timestamp('ends_at')
                  ->nullable()
                  ->comment('Poll end time (null for indefinite)');
            
            // Status
            $table->enum('status', ['draft', 'active', 'closed', 'archived'])
                  ->default('draft')
                  ->comment('Poll status');
            
            $table->boolean('is_featured')
                  ->default(false)
                  ->comment('Featured poll flag');
            
            // Display Position
            $table->string('display_position', 50)
                  ->default('sidebar')
                  ->comment('Where to display the poll (sidebar, inline, modal)');
            
            // Statistics (cached)
            $table->unsignedInteger('total_responses')
                  ->default(0)
                  ->comment('Total number of responses');
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('status');
            $table->index('campaign_id');
            $table->index('starts_at');
            $table->index('ends_at');
            $table->index('is_featured');
            $table->index(['status', 'starts_at', 'ends_at'], 'r_polls_active_index');
        });
    }

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