<?php

namespace App\Models;

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

class FactionRanking extends Model
{
    use HasFactory;

    protected $table = 'faction_rankings';

    protected $fillable = [
        'faction_id',
        'faction_name',
        'level',
        'member_count',
        'total_pk_count',
        'master_name',
        'master_role_id',
        'announce',
        'last_update',
    ];

    protected $casts = [
        'faction_id' => 'integer',
        'level' => 'integer',
        'member_count' => 'integer',
        'total_pk_count' => 'integer',
        'master_role_id' => 'integer',
        'last_update' => 'datetime',
    ];

    public static function getTopRanking($limit = 50)
    {
        return self::orderBy('level', 'DESC')
            ->orderBy('member_count', 'DESC')
            ->limit($limit)
            ->get();
    }

    public static function getTopRankingByPkCount($limit = 50)
    {
        return self::orderBy('total_pk_count', 'DESC')
            ->orderBy('level', 'DESC')
            ->limit($limit)
            ->get();
    }

    public static function searchByName($name, $limit = 50)
    {
        return self::where('faction_name', 'like', "%{$name}%")
            ->orderBy('level', 'DESC')
            ->orderBy('member_count', 'DESC')
            ->limit($limit)
            ->get();
    }
}
