<?php

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

/**
 * Migration: Digital Health Advocacy - Poll Options Table
 * 
 * Stores the answer options for each poll. Supports ordering and custom colors.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_poll_options', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Key to Poll
            $table->foreignId('poll_id')
                  ->constrained('r_polls')
                  ->onDelete('cascade')
                  ->comment('Associated poll');
            
            // Option Content
            $table->string('option_text', 500)
                  ->comment('Option text/label');
            
            $table->text('description')
                  ->nullable()
                  ->comment('Optional description');
            
            // Display
            $table->unsignedInteger('sort_order')
                  ->default(0)
                  ->comment('Display order');
            
            $table->string('color', 20)
                  ->nullable()
                  ->comment('Display color (hex or name)');
            
            // Statistics (cached)
            $table->unsignedInteger('votes_count')
                  ->default(0)
                  ->comment('Number of votes for this option');
            
            // Is "Other" option
            $table->boolean('is_other')
                  ->default(false)
                  ->comment('Whether this is the "other" option');
            
            // Timestamps
            $table->timestamps();
            
            // Indexes
            $table->index('poll_id');
            $table->index('sort_order');
        });
    }

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