<?php
 namespace App\Models; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Facades\Crypt; class Server extends Model { use HasFactory; protected $fillable = [ 'name', 'description', 'module', 'hostname', 'ip_address', 'username', 'password', 'port', 'status', 'maximum_hosts', 'current_hosts', 'stock', 'server_type', 'serial_number', 'asset_tag', 'manufacturer', 'model_name', 'specs', 'purchase_date', 'warranty_expiry', 'asset_status', 'datacenter_id', 'rack_id', 'u_position', 'u_size', 'client_product_id', ]; protected $casts = [ 'port' => 'integer', 'maximum_hosts' => 'integer', 'current_hosts' => 'integer', 'stock' => 'integer', 'specs' => 'array', 'purchase_date' => 'date', 'warranty_expiry' => 'date', 'u_position' => 'integer', 'u_size' => 'integer', ]; protected $hidden = [ 'password', ]; public function getPasswordAttribute(?string $value): ?string { if ($value === null || $value === '') { return $value; } try { return Crypt::decryptString($value); } catch (DecryptException $e) { return $value; } } public function setPasswordAttribute(?string $value): void { if ($value === null || $value === '') { $this->attributes['password'] = $value; return; } $this->attributes['password'] = Crypt::encryptString($value); } public function clientProducts(): HasMany { return $this->hasMany(ClientProduct::class); } public function settings(): HasMany { return $this->hasMany(ServerSetting::class); } public function groups(): BelongsToMany { return $this->belongsToMany(ServerGroup::class, 'server_group_relations', 'server_id', 'group_id') ->withTimestamps(); } public function datacenter(): BelongsTo { return $this->belongsTo(Datacenter::class); } public function rack(): BelongsTo { return $this->belongsTo(Rack::class); } public function clientProduct(): BelongsTo { return $this->belongsTo(ClientProduct::class); } public function scopeProvisionable($query) { return $query->whereIn('server_type', ['provisioning', 'both']); } public function scopePhysical($query) { return $query->whereIn('server_type', ['physical', 'both']); } } 