namespace BackOffice.Services.DiscountOrder;

public interface IDiscountOrderService
{
    Task<(List<DiscountOrderDto> Orders, int TotalCount, int TotalPages)> GetOrdersAsync(OrderFilterDto? filter = null);
    Task<DiscountOrderDetailsDto?> GetByIdAsync(long orderId, long? userId = null);
    Task UpdateStatusAsync(long orderId, UpdateOrderStatusDto dto);
    Task<DiscountSalesReportDto> GetSalesReportAsync(DateTime fromDate, DateTime toDate);
}

public class OrderFilterDto
{
    public long? UserId { get; set; }
    public bool? PaymentCompleted { get; set; }
    public int? PaymentStatus { get; set; }
    public OrderStatus? Status { get; set; }
    public string? UserMobile { get; set; }
    public string? TrackingCode { get; set; }
    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get; set; }
    public long? MinAmount { get; set; }
    public long? MaxAmount { get; set; }
    public int PageNumber { get; set; } = 1;
    public int PageSize { get; set; } = 20;
}

// For backward compatibility with UI - maps to proto DeliveryStatus
public enum OrderStatus
{
    Pending = 0,              // DELIVERY_PENDING
    Paid = 1,                 // Not in proto - handle in service
    Processing = 2,           // DELIVERY_PROCESSING
    Shipped = 3,              // DELIVERY_SHIPPED
    Delivered = 4,            // DELIVERY_DELIVERED
    Cancelled = 5,            // DELIVERY_CANCELLED
    Returned = 6,             // DELIVERY_RETURNED
    ReadyForOfficePickup = 7  // DELIVERY_READY_FOR_OFFICE
}

public class DiscountOrderDto
{
    public long Id { get; set; }
    public string OrderNumber { get; set; } = string.Empty;
    public long UserId { get; set; }
    public string UserFullName { get; set; } = string.Empty;
    public string UserMobile { get; set; } = string.Empty;
    public long TotalPrice { get; set; }
    public long DiscountBalanceUsed { get; set; }
    public long GatewayAmount { get; set; }
    public long VatAmount { get; set; }
    public bool PaymentCompleted { get; set; }
    public int PaymentStatusValue { get; set; }
    public OrderStatus Status { get; set; }
    public string? TrackingCode { get; set; }
    public string? ShippingAddress { get; set; }
    public int ItemsCount { get; set; }
    public DateTime? PaymentDate { get; set; }
    public DateTime? Created { get; set; }
}

public class DiscountOrderDetailsDto
{
    public long Id { get; set; }
    public long UserId { get; set; }
    public string OrderNumber { get; set; } = string.Empty;
    /// <summary>از ردیف لیست پر می‌شود (GetOrderById این فیلدها را ندارد)</summary>
    public string UserFullName { get; set; } = string.Empty;
    public string UserMobile { get; set; } = string.Empty;
    public AddressInfoDto? Address { get; set; }
    public long TotalPrice { get; set; }
    public long DiscountBalanceUsed { get; set; }
    public long GatewayAmount { get; set; }
    public long VatAmount { get; set; }
    public bool PaymentCompleted { get; set; }
    /// <summary>مقدار proto PaymentStatus: 0 Pending, 1 Completed, 2 Failed, 3 Refunded</summary>
    public int PaymentStatusValue { get; set; }
    public string? TransactionId { get; set; }
    public OrderStatus Status { get; set; }
    public string? TrackingCode { get; set; }
    public string? Notes { get; set; }
    public string? AdminNotes { get; set; }
    public List<OrderItemDto> Items { get; set; } = new();
    public DateTime? Created { get; set; }
    public DateTime? LastModified { get; set; }
}

public class AddressInfoDto
{
    public long Id { get; set; }
    public string Title { get; set; } = string.Empty;
    public string Address { get; set; } = string.Empty;
    public string PostalCode { get; set; } = string.Empty;
    public string? Phone { get; set; }
}

public class OrderItemDto
{
    public long ProductId { get; set; }
    public string ProductTitle { get; set; } = string.Empty;
    public long UnitPrice { get; set; }
    public int MaxDiscountPercent { get; set; }
    public int Count { get; set; }
    public long TotalPrice { get; set; }
    public long DiscountAmount { get; set; }
    public long FinalPrice { get; set; }
    public string ImagePath { get; set; } = string.Empty;
    public string ThumbnailPath { get; set; } = string.Empty;
}

public class UpdateOrderStatusDto
{
    public OrderStatus Status { get; set; }
    public string? TrackingCode { get; set; }
    public string? AdminNotes { get; set; }
}

// گزارش فروش — از GetDiscountSalesReport RPC
public class DiscountSalesReportDto
{
    public SalesSummaryDto Summary { get; set; } = new();
    public List<SalesPeriodItemDto> Periods { get; set; } = new();
    public List<TopSellingProductItemDto> TopProducts { get; set; } = new();
}

public class SalesSummaryDto
{
    public int TotalOrders { get; set; }
    public int CompletedOrders { get; set; }
    public int PendingOrders { get; set; }
    public int CancelledOrders { get; set; }
    public long TotalSalesAmount { get; set; }
    public long TotalDiscountUsed { get; set; }
    public long TotalGatewayPaid { get; set; }
    public long TotalVatAmount { get; set; }
    public long AverageOrderValue { get; set; }
    public int UniqueCustomers { get; set; }
    public int TotalProductsSold { get; set; }
}

public class SalesPeriodItemDto
{
    public string PeriodLabel { get; set; } = string.Empty;
    public int OrdersCount { get; set; }
    public long TotalAmount { get; set; }
    public long DiscountUsed { get; set; }
    public long GatewayPaid { get; set; }
}

public class TopSellingProductItemDto
{
    public long ProductId { get; set; }
    public string ProductTitle { get; set; } = string.Empty;
    public string? ImagePath { get; set; }
    public int QuantitySold { get; set; }
    public long TotalRevenue { get; set; }
    public int OrdersCount { get; set; }
}
