<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Leaderboard extends Model
{
    protected $fillable = [
        'type',
        'period',
        'period_start',
        'period_end',
        'user_id',
        'rank',
        'score',
        'metadata',
        'region',
        'country',
    ];

    protected $casts = [
        'metadata' => 'array',
        'period_start' => 'date',
        'period_end' => 'date',
        'score' => 'decimal:2',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function scopeByType($query, string $type)
    {
        return $query->where('type', $type);
    }

    public function scopeByPeriod($query, string $period)
    {
        return $query->where('period', $period);
    }

    public function scopeByCountry($query, string $country)
    {
        return $query->where('country', $country);
    }

    public function scopeTopRanked($query, int $limit = 10)
    {
        return $query->orderBy('rank', 'asc')->limit($limit);
    }

    public function scopeCurrent($query)
    {
        return $query->where('period_start', '<=', now())
            ->where('period_end', '>=', now());
    }
}
