using Microsoft.Extensions.Logging;

namespace TakeoutSaaS.Application.App.Personal.Services;

/// <summary>
/// 个人中心敏感查询审计服务。
/// </summary>
public sealed class PersonalAuditService(
    PersonalContextService personalContextService,
    ILogger<PersonalAuditService> logger)
{
    /// <summary>
    /// 记录敏感查询审计日志。
    /// </summary>
    /// <param name="module">模块名称。</param>
    /// <param name="isSuccess">是否成功。</param>
    /// <param name="detail">审计说明。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>异步任务。</returns>
    public Task RecordSensitiveQueryAsync(string module, bool isSuccess, string? detail, CancellationToken cancellationToken = default)
    {
        // 1. 获取当前上下文
        var context = personalContextService.GetRequiredContext();

        // 2. 写入结构化审计日志
        logger.LogInformation(
            "Personal sensitive query audited. Module={Module}, TenantId={TenantId}, UserId={UserId}, Success={Success}, TraceId={TraceId}, Detail={Detail}",
            module,
            context.TenantId,
            context.UserId,
            isSuccess,
            context.TraceId,
            detail);

        // 3. 返回完成任务
        return Task.CompletedTask;
    }
}
