// EN: Mapper for Inventory domain entities to DTOs.
// VI: Mapper từ domain entities sang DTOs.

using InventoryService.Domain.AggregatesModel.InventoryAggregate;
using InventoryService.API.Application.DTOs;

namespace InventoryService.API.Application.Mappers;

/// <summary>
/// EN: Mapper from domain entities to DTOs.
/// VI: Mapper từ domain entities sang DTOs.
/// </summary>
public static class InventoryMapper
{
    /// <summary>
    /// EN: Convert InventoryItem to DTO.
    /// VI: Chuyển InventoryItem sang DTO.
    /// </summary>
    public static InventoryItemDto ToDto(this InventoryItem item) => new(
        item.Id,
        item.ProductId,
        item.ShopId,
        item.Name,
        item.ItemType?.Name ?? ResolveItemTypeName(item.ItemTypeId),
        item.ItemTypeId,
        item.Unit,
        item.CostPerUnit,
        item.SupplierName,
        item.ExpiryDate,
        item.Quantity,
        item.ReservedQuantity,
        item.AvailableQuantity,
        item.ReorderLevel,
        item.UpdatedAt);

    /// <summary>
    /// EN: Convert InventoryTransaction to DTO.
    /// VI: Chuyển InventoryTransaction sang DTO.
    /// </summary>
    public static InventoryTransactionDto ToDto(this InventoryTransaction transaction) => new(
        transaction.Id,
        transaction.InventoryItemId,
        transaction.Type?.Name ?? ResolveTransactionTypeName(transaction.TypeId),
        transaction.Quantity,
        transaction.ReferenceId,
        transaction.Notes,
        transaction.InvoiceImageUrl,
        transaction.UnitCost,
        transaction.CreatedAt);

    private static string ResolveItemTypeName(int itemTypeId) => itemTypeId switch
    {
        1 => "RawMaterial",
        2 => "FinishedGood",
        3 => "Consumable",
        _ => "Unknown"
    };

    /// <summary>
    /// EN: Resolve transaction type name from TypeId when navigation property is null.
    /// VI: Phân giải tên loại giao dịch từ TypeId khi navigation property là null.
    /// </summary>
    private static string ResolveTransactionTypeName(int typeId) => typeId switch
    {
        1 => "In",
        2 => "Out",
        3 => "Adjustment",
        4 => "Reserve",
        5 => "Release",
        6 => "Wastage",
        _ => "Unknown"
    };
}
