// EN: DTOs for Inventory Service API.
// VI: DTOs cho Inventory Service API.

namespace InventoryService.API.Application.DTOs;

/// <summary>
/// EN: DTO for inventory item.
/// VI: DTO cho inventory item.
/// </summary>
public record InventoryItemDto(
    Guid Id,
    Guid ProductId,
    Guid ShopId,
    string? Name,
    string ItemType,
    int ItemTypeId,
    string Unit,
    decimal CostPerUnit,
    string? SupplierName,
    DateTime? ExpiryDate,
    int Quantity,
    int ReservedQuantity,
    int AvailableQuantity,
    int ReorderLevel,
    DateTime? UpdatedAt);

/// <summary>
/// EN: DTO for inventory transaction.
/// VI: DTO cho inventory transaction.
/// </summary>
public record InventoryTransactionDto(
    Guid Id,
    Guid InventoryItemId,
    string TransactionType,
    int Quantity,
    Guid? ReferenceId,
    string? Notes,
    string? InvoiceImageUrl,
    decimal? UnitCost,
    DateTime CreatedAt);

/// <summary>
/// EN: DTO for stock level response (used by POS for cart validation).
/// VI: DTO cho response mức tồn kho (dùng bởi POS cho xác thực giỏ hàng).
/// </summary>
public record StockLevelDto(
    Guid ProductId,
    int Available,
    int Reserved,
    int Minimum,
    bool IsLowStock);

/// <summary>
/// EN: Request for bulk stock check (for cart validation).
/// VI: Request cho kiểm tra stock hàng loạt (cho xác thực giỏ hàng).
/// </summary>
public record BulkStockCheckRequest(
    Guid ShopId,
    List<Guid> ProductIds);

/// <summary>
/// EN: Request for setting low stock alert threshold.
/// VI: Request cho cài đặt ngưỡng cảnh báo stock thấp.
/// </summary>
public record SetLowStockAlertRequest(
    Guid ShopId,
    Guid ProductId,
    int MinimumLevel);

/// <summary>
/// EN: Request for stock in operation.
/// VI: Request cho thao tác nhập kho.
/// </summary>
public record StockInRequest(
    Guid ProductId,
    Guid ShopId,
    int Amount,
    string? Notes,
    Guid? ReferenceId,
    string? InvoiceImageUrl,
    decimal? UnitCost);

/// <summary>
/// EN: Request for stock out operation.
/// VI: Request cho thao tác xuất kho.
/// </summary>
public record StockOutRequest(
    Guid ProductId,
    Guid ShopId,
    int Amount,
    string? Notes,
    Guid? ReferenceId);

/// <summary>
/// EN: Request for stock reservation.
/// VI: Request cho đặt trước stock.
/// </summary>
public record ReserveStockRequest(
    Guid ProductId,
    Guid ShopId,
    int Amount,
    Guid OrderId);

/// <summary>
/// EN: Request for releasing reservation.
/// VI: Request cho giải phóng đặt trước.
/// </summary>
public record ReleaseReservationRequest(
    Guid ProductId,
    Guid ShopId,
    int Amount,
    Guid OrderId);

/// <summary>
/// EN: Request for stock adjustment.
/// VI: Request cho điều chỉnh stock.
/// </summary>
public record AdjustStockRequest(
    Guid ProductId,
    Guid ShopId,
    int NewQuantity,
    string Notes);

/// <summary>
/// EN: Request for creating a new inventory item (raw materials / consumables).
/// VI: Request cho tạo inventory item mới (nguyên liệu thô / vật tư tiêu hao).
/// </summary>
public record StockOutByIdRequest(
    Guid InventoryItemId,
    int Amount,
    string? Notes = null,
    Guid? ReferenceId = null);

public record CreateInventoryItemRequest(
    Guid ShopId,
    string Name,
    int ItemTypeId,
    string Unit,
    decimal CostPerUnit,
    int InitialQuantity = 0,
    int ReorderLevel = 10,
    string? SupplierName = null,
    DateTime? ExpiryDate = null);

/// <summary>
/// EN: Standard API response wrapper.
/// VI: Wrapper response API chuẩn.
/// </summary>
public record RecordWastageRequest(Guid InventoryItemId, int Amount, string Reason, string? Notes);

public record StocktakeRequest(Guid ShopId, List<StocktakeItemRequest> Items);

public record StocktakeItemRequest(Guid InventoryItemId, int CountedQuantity);

/// <summary>
/// EN: Request for bulk inventory deduction (from kitchen ticket served).
/// VI: Request cho trừ kho hàng loạt (từ phiếu bếp đã phục vụ).
/// </summary>
public record DeductInventoryRequest(
    Guid ShopId,
    Guid ReferenceId,
    string ReferenceType,
    string? Reason,
    List<DeductionItemRequest> Items);

/// <summary>
/// EN: Individual deduction item in a bulk deduction request.
/// VI: Item trừ kho riêng lẻ trong yêu cầu trừ kho hàng loạt.
/// </summary>
public record DeductionItemRequest(
    Guid InventoryItemId,
    int Amount,
    string Unit,
    string IngredientName);

public class ApiResponse<T>
{
    public bool Success { get; set; }
    public T? Data { get; set; }
    public string? Error { get; set; }

    public static ApiResponse<T> Ok(T data) => new() { Success = true, Data = data };
    public static ApiResponse<T> Fail(string error) => new() { Success = false, Error = error };
}

/// <summary>
/// EN: Paged result for queries.
/// VI: Kết quả phân trang cho queries.
/// </summary>
public record PagedResult<T>(
    IReadOnlyList<T> Items,
    int TotalCount);
