using System.Text.Json;
using Backend.Common;
using Backend.Domain;
using Backend.Repositories;

namespace Backend.Services;

public sealed class AdminAuditService(IAdminAuditRepository adminAuditRepository) : IAdminAuditService
{
    public void AddEntry(
        string adminTwitchUserId,
        string actionType,
        string entityType,
        string entityId,
        string summary,
        object? metadata = null,
        RequestMetadata? requestMetadata = null)
    {
        adminAuditRepository.Add(new AdminAuditEntry
        {
            AdminTwitchUserId = adminTwitchUserId,
            ActionType = actionType,
            EntityType = entityType,
            EntityId = entityId,
            Summary = summary,
            MetadataJson = JsonSerializer.Serialize(metadata ?? new { }),
            CreatedFromIp = requestMetadata?.ClientIp ?? string.Empty,
            UserAgent = requestMetadata?.UserAgent ?? string.Empty,
            CreatedAt = DateTimeOffset.UtcNow,
        });
    }
}
