<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class ElectionPosition extends Model
{
    use HasFactory;

    // =========================================================================
    // Model Configuration
    // =========================================================================

    protected $fillable = [
        'uuid',
        'election_id',
        'association_id',
        'title',
        'description',
        'available_seats',
        'max_candidates_per_voter',
        'display_order',
        'is_active',
        'voting_strategy_override',
        'metadata',
    ];

    protected $casts = [
        'available_seats' => 'integer',
        'max_candidates_per_voter' => 'integer',
        'display_order' => 'integer',
        'is_active' => 'boolean',
        'metadata' => 'json',
    ];

    // =========================================================================
    // Boot
    // =========================================================================

    protected static function boot()
    {
        parent::boot();

        static::creating(function (ElectionPosition $position) {
            if (!$position->uuid) {
                $position->uuid = (string) Str::uuid();
            }
        });
    }

    // =========================================================================
    // Relationships
    // =========================================================================

    public function election()
    {
        return $this->belongsTo(Election::class);
    }

    public function association()
    {
        return $this->belongsTo(Association::class);
    }

    public function candidates()
    {
        return $this->hasMany(ElectionCandidate::class, 'position_id');
    }

    public function approvedCandidates()
    {
        return $this->hasMany(ElectionCandidate::class, 'position_id')
            ->where('status', ElectionCandidate::STATUS_APPROVED);
    }

    public function votes()
    {
        return $this->hasMany(ElectionVote::class, 'position_id');
    }

    public function results()
    {
        return $this->hasMany(ElectionResult::class, 'position_id');
    }

    // =========================================================================
    // Scopes
    // =========================================================================

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('display_order');
    }

    // =========================================================================
    // Accessors
    // =========================================================================

    /**
     * Effective voting strategy (override or inherited from election).
     */
    public function getEffectiveStrategyAttribute(): string
    {
        return $this->voting_strategy_override ?? $this->election->voting_strategy;
    }
}
