<?php
namespace App\Services;use App\Models\{Keyword,KeywordAudit,User};use Illuminate\Support\Str;
final class KeywordAuditLogger{private const SAFE=['phrase','search_engine','country_code','language_code','device','status','archived_at'];public function record(string $action,Keyword $keyword,User $actor,array $before=[]):void{KeywordAudit::create(['actor_user_id'=>$actor->id,'keyword_id'=>$keyword->id,'project_id'=>$keyword->project_id,'action'=>$action,'correlation_id'=>(string)Str::uuid(),'before'=>array_intersect_key($before,array_flip(self::SAFE))?:null,'after'=>array_intersect_key($keyword->fresh()->attributesToArray(),array_flip(self::SAFE))]);}}
