using MilitaryTrainingManagement.Models.Entities;

namespace MilitaryTrainingManagement.Services.Interfaces;

/// <summary>
/// 审计服务接口
/// </summary>
public interface IAuditService
{
    /// <summary>
    /// 记录审计日志
    /// </summary>
    Task LogAsync(AuditLogEntry entry);
    
    /// <summary>
    /// 记录审计日志（简化版本）
    /// </summary>
    Task LogAsync(string entityType, int entityId, string action, string? oldValues, string? newValues, int? userId, string? ipAddress);
    
    /// <summary>
    /// 记录创建操作
    /// </summary>
    Task LogCreateAsync<T>(T entity, int? userId, int? organizationalUnitId, string? ipAddress = null, string? userAgent = null, string? requestPath = null) where T : class;
    
    /// <summary>
    /// 记录更新操作
    /// </summary>
    Task LogUpdateAsync<T>(T originalEntity, T updatedEntity, int? userId, int? organizationalUnitId, string? ipAddress = null, string? userAgent = null, string? requestPath = null) where T : class;
    
    /// <summary>
    /// 记录删除操作
    /// </summary>
    Task LogDeleteAsync<T>(T entity, int? userId, int? organizationalUnitId, string? ipAddress = null, string? userAgent = null, string? requestPath = null) where T : class;
    
    /// <summary>
    /// 记录审批操作
    /// </summary>
    Task LogApprovalAsync(string entityType, int entityId, string action, string? description, int? userId, int? organizationalUnitId, string? ipAddress = null);
    
    /// <summary>
    /// 记录失败操作
    /// </summary>
    Task LogFailureAsync(string entityType, int entityId, string action, string errorMessage, int? userId, int? organizationalUnitId, string? ipAddress = null);
    
    /// <summary>
    /// 获取审计日志
    /// </summary>
    Task<IEnumerable<AuditLog>> GetLogsAsync(AuditLogQueryParameters parameters);
    
    /// <summary>
    /// 获取审计日志总数
    /// </summary>
    Task<int> GetLogCountAsync(AuditLogQueryParameters parameters);
    
    /// <summary>
    /// 根据ID获取审计日志
    /// </summary>
    Task<AuditLog?> GetLogByIdAsync(int id);
    
    /// <summary>
    /// 获取审计日志（简化版本）
    /// </summary>
    Task<IEnumerable<AuditLog>> GetLogsAsync(string? entityType = null, int? entityId = null, DateTime? fromDate = null, DateTime? toDate = null);
    
    /// <summary>
    /// 获取实体的审计历史
    /// </summary>
    Task<IEnumerable<AuditLog>> GetEntityHistoryAsync(string entityType, int entityId);
    
    /// <summary>
    /// 获取用户的操作历史
    /// </summary>
    Task<IEnumerable<AuditLog>> GetUserActivityAsync(int userId, DateTime? fromDate = null, DateTime? toDate = null);
    
    /// <summary>
    /// 获取组织单位的操作历史
    /// </summary>
    Task<IEnumerable<AuditLog>> GetOrganizationalUnitActivityAsync(int organizationalUnitId, DateTime? fromDate = null, DateTime? toDate = null);
    
    /// <summary>
    /// 获取审计日志统计
    /// </summary>
    Task<AuditLogStatistics> GetStatisticsAsync(DateTime? fromDate = null, DateTime? toDate = null);
}

/// <summary>
/// 审计日志条目
/// </summary>
public class AuditLogEntry
{
    public string EntityType { get; set; } = string.Empty;
    public int EntityId { get; set; }
    public string Action { get; set; } = string.Empty;
    public string? Description { get; set; }
    public string? OldValues { get; set; }
    public string? NewValues { get; set; }
    public string? ChangedFields { get; set; }
    public int? UserId { get; set; }
    public int? OrganizationalUnitId { get; set; }
    public string? IpAddress { get; set; }
    public string? UserAgent { get; set; }
    public string? RequestPath { get; set; }
    public bool IsSuccess { get; set; } = true;
    public string? ErrorMessage { get; set; }
}

/// <summary>
/// 审计日志查询参数
/// </summary>
public class AuditLogQueryParameters
{
    public string? EntityType { get; set; }
    public int? EntityId { get; set; }
    public string? Action { get; set; }
    public int? UserId { get; set; }
    public int? OrganizationalUnitId { get; set; }
    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get; set; }
    public bool? IsSuccess { get; set; }
    public int PageNumber { get; set; } = 1;
    public int PageSize { get; set; } = 50;
}

/// <summary>
/// 审计日志统计
/// </summary>
public class AuditLogStatistics
{
    public int TotalLogs { get; set; }
    public int CreateOperations { get; set; }
    public int UpdateOperations { get; set; }
    public int DeleteOperations { get; set; }
    public int ApprovalOperations { get; set; }
    public int FailedOperations { get; set; }
    public Dictionary<string, int> OperationsByEntityType { get; set; } = new();
    public Dictionary<string, int> OperationsByAction { get; set; } = new();
}
