<?php

namespace App\Models\Stats;

use Illuminate\Database\Eloquent\Model;

class StatSystemDaily extends Model
{
    protected $connection = 'sam_stat';

    protected $table = 'stat_system_daily';

    protected $casts = [
        'stat_date' => 'date',
        'active_user_count' => 'integer',
        'login_count' => 'integer',
        'api_request_count' => 'integer',
        'api_error_count' => 'integer',
        'api_avg_response_ms' => 'integer',
    ];

    public function scopeForTenant($query, ?int $tenantId)
    {
        if ($tenantId) {
            return $query->where('tenant_id', $tenantId);
        }

        return $query;
    }

    public function scopeForDateRange($query, string $from, string $to)
    {
        return $query->whereBetween('stat_date', [$from, $to]);
    }

    public function scopeForDate($query, string $date)
    {
        return $query->where('stat_date', $date);
    }
}
