<?php

namespace App\Models;

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

class PlayerRanking extends Model
{
    use HasFactory;

    protected $table = 'player_rankings';

    protected $fillable = [
        'user_id',
        'role_id',
        'role_name',
        'cls',
        'level',
        'reputation',
        'pk_count',
        'faction_id',
        'faction_name',
        'last_update'
    ];

    protected $casts = [
        'role_id' => 'integer',
        'cls' => 'integer',
        'level' => 'integer',
        'reputation' => 'integer',
        'pk_count' => 'integer',
        'faction_id' => 'integer',
        'last_update' => 'datetime',
    ];

    /**
     * Get top players by reputation and level
     */
    public static function getTopRanking($limit = 50)
    {
        return self::orderBy('reputation', 'DESC')
            ->orderBy('level', 'DESC')
            ->limit($limit)
            ->get();
    }

    /**
     * Get player by role ID
     */
    public static function getByRoleId($roleId)
    {
        return self::where('role_id', $roleId)->first();
    }

    /**
     * Search players by name
     */
    public static function searchByName($name, $limit = 50)
    {
        return self::where('role_name', 'like', "%{$name}%")
            ->orderBy('reputation', 'DESC')
            ->orderBy('level', 'DESC')
            ->limit($limit)
            ->get();
    }
}
