using TakeoutSaaS.Application.App.Products.Dto;
using TakeoutSaaS.Domain.Products.Entities;

namespace TakeoutSaaS.Application.App.Products;

/// <summary>
/// 商品时段规则 DTO 映射工厂。
/// </summary>
internal static class ProductScheduleDtoFactory
{
    /// <summary>
    /// 将时段规则实体映射为页面 DTO。
    /// </summary>
    public static ProductScheduleItemDto ToDto(
        ProductSchedule schedule,
        IReadOnlyDictionary<long, List<long>> productIdsLookup)
    {
        var productIds = productIdsLookup.TryGetValue(schedule.Id, out var ids)
            ? ids
            : [];

        return new ProductScheduleItemDto
        {
            Id = schedule.Id,
            Name = schedule.Name,
            StartTime = ProductScheduleMapping.ToTimeText(schedule.StartTime),
            EndTime = ProductScheduleMapping.ToTimeText(schedule.EndTime),
            WeekDays = ProductScheduleMapping.FromWeekDaysMask(schedule.WeekDaysMask),
            Status = ProductScheduleMapping.ToStatusText(schedule.IsEnabled),
            ProductCount = productIds.Count,
            ProductIds = productIds,
            UpdatedAt = schedule.UpdatedAt ?? schedule.CreatedAt
        };
    }
}
