<?php

namespace App\Services;

use App\Models\AuditLogs;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class AuditLogService
{
    public function record(
        string $action,
        Model $subject,
        ?int $company_id,
        array $oldvalues = [],
        array $newvalues = [],
        array $metadata = [],


    ):AuditLogs {
        $request = app()->bound('request') ? request() : null;

        $user = Auth::user();

        return AuditLogs::create([
            'actor_id' => $user?->id,
            'company_id' => $company_id,

            'subject_type' => $subject->getMorphClass(),
            'subject_id' => $subject->getKey(),

            'action'  => $action,

            'old_values' => $oldvalues ?: null,
            'new_values' => $newvalues ?: null,

            'metadata' => array_merge([
                'actor_name' => $user?->name,
                'actor_email' => $user?->email,
            ], $metadata),

            'ip_address' => $request?->ip(),
            'user_agent' => $request?->userAgent()



        ]);






    }



    



}

