<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class PoliticalParty extends Model
{
    use SoftDeletes;

    protected $fillable = ['name', 'acronym', 'logo'];

    public function candidates(): HasMany
    {
        return $this->hasMany(Candidate::class, 'political_party_id');
    }

    public function personeros(): HasMany
    {
        return $this->hasMany(Personero::class, 'political_party_id');
    }

    public function scopeSearch($query, ?string $term)
    {
        if ($term) {
            $query->where(function ($q) use ($term) {
                $q->where('name', 'like', "%{$term}%")
                  ->orWhere('acronym', 'like', "%{$term}%");
            });
        }
        return $query;
    }
}
