'array', 'blocked' => 'boolean', 'monthly_budget_limit' => 'decimal:2', 'current_month_spending' => 'decimal:2', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; // Relationships public function apiKeys() { return $this->hasMany(ApiKey::class, 'gateway_user_id', 'user_id'); } public function credentials() { return $this->hasMany(GatewayUserCredential::class, 'user_id', 'user_id'); } public function usageLogs() { return $this->hasMany(UsageLog::class, 'gateway_user_id', 'user_id'); } public function llmRequests() { return $this->hasMany(LlmRequest::class, 'gateway_user_id', 'user_id'); } // Scopes public function scopeActive($query) { return $query->where('blocked', false); } public function scopeBlocked($query) { return $query->where('blocked', true); } // Helper methods for budget management public function isBlocked(): bool { return $this->blocked; } public function hasExceededBudget(): bool { if (!$this->monthly_budget_limit) { return false; } return $this->current_month_spending >= $this->monthly_budget_limit; } public function incrementSpending(float $amount): void { $this->increment('current_month_spending', $amount); } public function resetMonthlySpending(): void { $this->update(['current_month_spending' => 0]); } public function getBudgetUsagePercentage(): ?float { if (!$this->monthly_budget_limit || $this->monthly_budget_limit == 0) { return null; } return ($this->current_month_spending / $this->monthly_budget_limit) * 100; } public function shouldSendBudgetAlert(): bool { if (!$this->budget_alert_threshold || !$this->monthly_budget_limit) { return false; } $percentage = $this->getBudgetUsagePercentage(); return $percentage !== null && $percentage >= $this->budget_alert_threshold; } }