<?php

namespace App\Modules\Admin\ActivityLog\Services;

use App\Modules\Admin\ActivityLog\DTOs\ActivityLogWriteData;
use App\Modules\Admin\ActivityLog\Models\ActivityLog;
use App\Modules\Admin\ActivityLog\Repositories\ActivityLogRepository;
use Illuminate\Http\Request;

class ActivityLogService
{
    public function __construct(
        private readonly ActivityLogRepository $repository,
    ) {}

    public function log(ActivityLogWriteData $data, ?Request $request = null): ActivityLog
    {
        $request ??= request();
        $ip = $request?->ip();
        $ua = $request?->userAgent();

        return $this->repository->insert($data->toAttributes($ip, $ua));
    }
}
