<?php  namespace App\Traits;  use App\Models\UserActivity; use App\Enums\UserActivityType; use Illuminate\Database\Eloquent\Relations\MorphMany;  trait HasUserActivity { 	public function activities(): MorphMany{}  	public function recordReadActivity(?bool $every = null, ?bool $update = null): self{}  	public function recordEveryReadActivity(): bool{}  	public function updateReadActivity(): bool{}  	public function getUserActivityTitle(): ?string{}  	public function getUserActivityUrl(): ?string{}  	public function shouldRecordUserActivity(UserActivityType $type): bool{}  	public function getUserActivityLinkedRelations(): array{}  	protected static function bootHasUserActivity(): void 	{ 		static::deleting(function (object $model): void { 			$model->activities->each(function (UserActivity $item): void { 				$item->delete(); 			});  			$user = auth()->user();  			if (!$user || !$model->shouldRecordUserActivity(UserActivityType::Delete)) { 				return; 			}  			foreach ($model->getUserActivityLinkedRelations() as $relation) { 				$relation->activities()->create([ 					'user_id' => $user->id, 					'ip_address' => request()->ip() ?? null, 					'model_type' => $model::class, 					'model_id' => $model->id, 					'type' => UserActivityType::Delete, 				]); 			} 		});  		static::created(function (object $model): void { 			$user = auth()->user();  			if (!$user || !$model->shouldRecordUserActivity(UserActivityType::Create)) { 				return; 			}  			$model->activities()->create([ 				'user_id' => $user->id, 				'ip_address' => request()->ip() ?? null, 				'type' => UserActivityType::Create, 			]);  			foreach ($model->getUserActivityLinkedRelations() as $relation) { 				$relation->activities()->create([ 					'user_id' => $user->id, 					'ip_address' => request()->ip() ?? null, 					'model_type' => $model::class, 					'model_id' => $model->id, 					'type' => UserActivityType::Create, 				]); 			} 		});  		static::updated(function (object $model): void { 			$user = auth()->user();  			if (!$user || !$model->shouldRecordUserActivity(UserActivityType::Update)) { 				return; 			}  			$fields = $model->getChanges(); 			unset($fields[$model->getUpdatedAtColumn()]); 			$fields = array_keys($fields); 			sort($fields);  			$model->activities()->create([ 				'user_id' => $user->id, 				'ip_address' => request()->ip() ?? null, 				'type' => UserActivityType::Update, 				'updated_fields' => $fields, 			]);  			foreach ($model->getUserActivityLinkedRelations() as $relation) { 				$relation->activities()->create([ 					'user_id' => $user->id, 					'ip_address' => request()->ip() ?? null, 					'model_type' => $model::class, 					'model_id' => $model->id, 					'type' => UserActivityType::Update, 					'updated_fields' => $fields, 				]); 			} 		}); 	} }