using Tiku.Application.Backoffice;
using Tiku.Domain.Operations;
using Tiku.Infrastructure.Persistence;

namespace Tiku.Infrastructure.Backoffice;

internal sealed class OperationAuditService(TikuDbContext dbContext) : IOperationAuditService
{
    public async Task WriteAsync(
        BackofficeOperationAuditCommand command,
        CancellationToken cancellationToken = default)
    {
        dbContext.AuditLogs.Add(new AuditLog
        {
            TenantId = command.TenantId,
            ActorUserId = command.ActorUserId,
            Action = command.Action.Trim(),
            TargetType = command.TargetType?.Trim(),
            TargetId = command.TargetId?.Trim(),
            Details = command.Details,
            IpAddress = command.IpAddress,
            UserAgent = command.UserAgent
        });

        await dbContext.SaveChangesAsync(cancellationToken);
    }
}
