using Meezi.Core.Enums;

namespace Meezi.API.Models.Orders;

public record OrderItemDto(
    string Id,
    string MenuItemId,
    string MenuItemName,
    int Quantity,
    decimal UnitPrice,
    string? Notes,
    bool IsVoided = false,
    DateTime? VoidedAt = null,
    // Prep station the item routes to (Kitchen/Bar). Populated on the live/KDS
    // path only; null elsewhere (= the branch kitchen / no station).
    string? StationId = null,
    string? StationName = null);

public record TransferTableRequest(string TargetTableId);

public record OrderDto(
    string Id,
    string CafeId,
    string? BranchId,
    string? TableId,
    string? TableNumber,
    string? GuestName,
    string? GuestPhone,
    string? CustomerName,
    string? CustomerPhone,
    string? CustomerId,
    string? EmployeeId,
    OrderType OrderType,
    OrderSource Source,
    OrderStatus Status,
    decimal Subtotal,
    decimal TaxTotal,
    decimal DiscountAmount,
    decimal Total,
    decimal PaidAmount,
    DateTime CreatedAt,
    int DisplayNumber,
    IReadOnlyList<OrderItemDto> Items,
    IReadOnlyList<PaymentDto> Payments);

public record AppendOrderItemsRequest(IReadOnlyList<CreateOrderItemRequest> Items);

public record UpdateOrderSessionRequest(
    string? GuestName,
    string? GuestPhone,
    string? CustomerId);


public record CreateOrderItemRequest(string MenuItemId, int Quantity, string? Notes);

public record CreateOrderRequest(
    OrderType OrderType,
    string? BranchId,
    string? TableId,
    string? ReservationId,
    string? GuestName,
    string? GuestPhone,
    string? CustomerId,
    string? CouponId,
    IReadOnlyList<CreateOrderItemRequest> Items);

public record UpdateOrderStatusRequest(OrderStatus Status);

public record CancelOrderRequest(string? Reason);

public record CreatePaymentRequest(PaymentMethod Method, decimal Amount, string? Reference);

public record RecordPaymentsRequest(
    IReadOnlyList<CreatePaymentRequest> Payments,
    int? LoyaltyPointsToRedeem = null);

/// <summary>
/// اصلاح سند — amend the payments of an order after the fact (wrong method,
/// wrong amount, or payment recorded on the wrong order). Voids the listed
/// payments (marked Refunded, never deleted) and records the replacements in
/// one atomic operation. A reason is mandatory; the whole change is audit-logged.
/// </summary>
public record CorrectPaymentsRequest(
    IReadOnlyList<string> VoidPaymentIds,
    IReadOnlyList<CreatePaymentRequest> Replacements,
    string Reason);

public record PaymentDto(string Id, PaymentMethod Method, decimal Amount, PaymentStatus Status, string? Reference);

public record LiveOrderDto(
    string Id,
    int DisplayNumber,
    OrderStatus Status,
    string? TableNumber,
    OrderType OrderType,
    decimal Total,
    DateTime CreatedAt,
    IReadOnlyList<OrderItemDto> Items,
    OrderSource Source);
