// EN: DTOs for Order Service.
// VI: DTOs cho Order Service.

namespace OrderService.API.Application.DTOs;

/// <summary>
/// EN: Complete order details with items.
/// VI: Chi tiết order đầy đủ với items.
/// </summary>
public record OrderDto(
    Guid Id,
    Guid ShopId,
    Guid? CustomerId,
    string Status,
    decimal TotalAmount,
    string? PaymentMethod,
    string? TransactionId,
    decimal? AmountTendered,
    decimal? ChangeAmount,
    DateTime CreatedAt,
    DateTime? UpdatedAt,
    List<OrderItemDto> Items
);

/// <summary>
/// EN: Order item details.
/// VI: Chi tiết order item.
/// </summary>
public record OrderItemDto(
    Guid Id,
    Guid ProductId,
    string ProductName,
    string ProductType,
    int Quantity,
    decimal UnitPrice,
    decimal TotalPrice,
    string Status
);

/// <summary>
/// EN: Order summary for list views (property-based for Dapper materialization).
/// VI: Tóm tắt order cho danh sách (dựa trên property để Dapper materialize).
/// </summary>
public record OrderSummaryDto
{
    public Guid Id { get; init; }
    public Guid ShopId { get; init; }
    public Guid? CustomerId { get; init; }
    public Guid? TableId { get; init; }
    public string Status { get; init; } = string.Empty;
    public decimal TotalAmount { get; init; }
    public string? PaymentMethod { get; init; }
    public long ItemCount { get; init; }
    public DateTime CreatedAt { get; init; }
}

/// <summary>
/// EN: Paged result wrapper.
/// VI: Wrapper kết quả phân trang.
/// </summary>
public record PagedResult<T>(
    List<T> Items,
    int TotalCount,
    int PageSize,
    int CurrentPage
)
{
    /// <summary>
    /// EN: Total number of pages.
    /// VI: Tổng số trang.
    /// </summary>
    public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);

    /// <summary>
    /// EN: Whether there are more pages.
    /// VI: Có thêm trang hay không.
    /// </summary>
    public bool HasNextPage => CurrentPage < TotalPages;

    /// <summary>
    /// EN: Whether there are previous pages.
    /// VI: Có trang trước hay không.
    /// </summary>
    public bool HasPreviousPage => CurrentPage > 1;
}

/// <summary>
/// EN: DTO for return item in a return/exchange request.
/// VI: DTO cho item trả hàng trong yêu cầu trả/đổi hàng.
/// </summary>
public record ReturnItemDto(Guid OrderItemId, int Quantity, string? Reason);

/// <summary>
/// EN: DTO for exchange item in an exchange request (new items to receive).
/// VI: DTO cho item đổi hàng trong yêu cầu đổi hàng (item mới nhận về).
/// </summary>
public record ExchangeItemDto(Guid ProductId, int Quantity, decimal UnitPrice);

/// <summary>
/// EN: Return order details DTO.
/// VI: DTO chi tiết đơn trả hàng.
/// </summary>
public record ReturnOrderDto(
    Guid Id,
    Guid OriginalOrderId,
    string Reason,
    decimal RefundAmount,
    string Status,
    DateTime ReturnedAt,
    List<OrderItemDto> Items);

/// <summary>
/// EN: Result for create return command.
/// VI: Kết quả cho command tạo trả hàng.
/// </summary>
public record CreateReturnResult(
    bool Success,
    Guid? ReturnOrderId,
    decimal? RefundAmount,
    string? ErrorMessage);

/// <summary>
/// EN: Result for create exchange command.
/// VI: Kết quả cho command tạo đổi hàng.
/// </summary>
public record CreateExchangeResult(
    bool Success,
    Guid? ReturnOrderId,
    Guid? NewOrderId,
    decimal? PriceDifference,
    string? ErrorMessage);
