using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Domain.Entities.Store;
///
/// 주문 라인. Order N:1 + Product N:1.
/// Digital 상품인 경우 IssuedCouponCodeID 로 발급된 쿠폰 코드 추적.
///
public class OrderItem
{
[ForeignKey(nameof(OrderID))]
public virtual Order? Order { get; private set; }
[ForeignKey(nameof(ProductID))]
public virtual Product? Product { get; private set; }
[ForeignKey(nameof(IssuedCouponCodeID))]
public virtual CouponCode? IssuedCouponCode { get; private set; }
[Key]
public int ID { get; private set; }
public int OrderID { get; private set; }
public int ProductID { get; private set; }
public int Quantity { get; private set; }
/// 주문 시점 상품 단가 (KRW). Product.Price 변경되어도 보존.
public int UnitPrice { get; private set; }
/// Digital 상품 발급 쿠폰 코드 (Physical 상품은 null).
public int? IssuedCouponCodeID { get; private set; }
public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
private OrderItem() { }
public static OrderItem Create(int productID, int quantity, int unitPrice)
{
if (productID <= 0)
{
throw new ArgumentOutOfRangeException(nameof(productID));
}
if (quantity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(quantity), "Quantity must be positive.");
}
if (unitPrice < 0)
{
throw new ArgumentOutOfRangeException(nameof(unitPrice), "UnitPrice must be non-negative.");
}
return new OrderItem
{
ProductID = productID,
Quantity = quantity,
UnitPrice = unitPrice
};
}
public void AssignCouponCode(int couponCodeID)
{
if (couponCodeID <= 0)
{
throw new ArgumentOutOfRangeException(nameof(couponCodeID));
}
IssuedCouponCodeID = couponCodeID;
}
}