using TakeoutSaaS.Application.App.Personal.Dto;
using TakeoutSaaS.Shared.Abstractions.Results;

namespace TakeoutSaaS.Application.App.Personal;

/// <summary>
/// 个人中心映射辅助。
/// </summary>
public static class PersonalMapping
{
    /// <summary>
    /// 映射分页结果。
    /// </summary>
    /// <typeparam name="TSource">源类型。</typeparam>
    /// <typeparam name="TDestination">目标类型。</typeparam>
    /// <param name="source">源分页结果。</param>
    /// <param name="mapper">单项映射函数。</param>
    /// <returns>目标分页结果。</returns>
    public static PagedResult<TDestination> ToPagedResult<TSource, TDestination>(
        this PagedResult<TSource> source,
        Func<TSource, TDestination> mapper)
    {
        // 1. 映射分页项集合
        var items = source.Items.Select(mapper).ToList();

        // 2. 返回目标分页对象
        return new PagedResult<TDestination>(items, source.Page, source.PageSize, source.TotalCount);
    }

    /// <summary>
    /// 便捷构建模块状态 DTO。
    /// </summary>
    /// <param name="module">模块名称。</param>
    /// <param name="status">模块状态。</param>
    /// <param name="errorCode">错误码。</param>
    /// <param name="errorMessage">错误信息。</param>
    /// <param name="traceId">追踪标识。</param>
    /// <returns>模块状态 DTO。</returns>
    public static PersonalModuleStatusDto ToModuleStatus(
        this string module,
        string status,
        string? errorCode = null,
        string? errorMessage = null,
        string? traceId = null)
    {
        // 1. 构建通用模块状态
        return new PersonalModuleStatusDto
        {
            Module = module,
            Status = status,
            ErrorCode = errorCode,
            ErrorMessage = errorMessage,
            TraceId = traceId
        };
    }
}
