IpPoolType::class, 'cidr' => 'integer', 'is_active' => 'boolean', ]; } public function vms(): HasMany { return $this->hasMany(Customer::class, 'ip_pool_id'); } public function totalIps(): int { $start = ip2long($this->start_ip); $end = ip2long($this->end_ip); if ($start === false || $end === false || $end < $start) { return 0; } return (int) ($end - $start + 1); } public function usedIpsCount(): int { $query = Customer::query() ->where('ip_pool_id', $this->id) ->whereIn('status', ['pending', 'active']); if ($this->type === IpPoolType::Public) { return $query->whereNotNull('public_ip')->count(); } return $query->whereNotNull('ip_address')->count(); } public function freeIpsCount(): int { return max(0, $this->totalIps() - $this->usedIpsCount()); } public function containsIp(string $ip): bool { $long = ip2long($ip); $start = ip2long($this->start_ip); $end = ip2long($this->end_ip); return $long !== false && $long >= $start && $long <= $end; } }