using System.Threading.Tasks;
using MDL.Models;
using MDL.DTOs;

namespace BLL.Interfaces
{
    /// <summary>
    /// 标签替换服务接口
    /// </summary>
    public interface ILabelReplaceService
    {
        /// <summary>
        /// 处理标签替换请求
        /// </summary>
        /// <param name="request">标签替换请求</param>
        /// <param name="customerCode">客户代码</param>
        /// <param name="apiKey">API密钥</param>
        /// <returns>标签替换结果</returns>
        Task<LabelReplaceResult> ProcessLabelReplaceAsync(LabelReplaceMessage request, string customerCode, string apiKey);
        
        /// <summary>
        /// 根据跟踪单号获取标签替换请求记录
        /// </summary>
        /// <param name="trackingNumber">跟踪单号</param>
        /// <returns>标签替换请求实体列表</returns>
        Task<List<LabelReplaceEntity>> GetLabelReplaceRequestsByTrackingNumberAsync(string trackingNumber);
        
        /// <summary>
        /// 根据中性面单单号获取标签替换请求记录
        /// </summary>
        /// <param name="waybillNumber">中性面单单号</param>
        /// <returns>标签替换请求实体</returns>
        Task<LabelReplaceEntity?> GetLabelReplaceRequestByWaybillNumberAsync(string waybillNumber);

        /// <summary>
        /// 获取所有标签替换请求记录
        /// </summary>
        /// <returns>标签替换请求实体列表</returns>
        Task<List<LabelReplaceEntity>> GetAllLabelReplaceRequestsAsync();

        /// <summary>
        /// 分页获取标签替换请求记录
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="sortBy">排序字段</param>
        /// <param name="sortOrder">排序顺序</param>
        /// <param name="billOfLadingNumber">提单号</param>
        /// <param name="masterPackageNumber">大包号</param>
        /// <param name="referenceNumber">参考号</param>
        /// <param name="neutralWaybillNumber">中性面单单号</param>
        /// <param name="finalMileTrackingNumber">尾程跟踪单号</param>
        /// <param name="replaceStatus">换单状态</param>
        /// <param name="customerId">客户ID</param>
        /// <returns>标签替换请求实体列表和总记录数</returns>
        Task<(List<LabelReplaceWithCustomerDto>, int)> GetLabelReplaceRequestsByPageAsync(int page, int pageSize, string sortBy, string sortOrder, string billOfLadingNumber, string masterPackageNumber, string referenceNumber, string neutralWaybillNumber, string finalMileTrackingNumber, string replaceStatus, int? customerId, string startCreatedAt, string endCreatedAt, string startReplacedAt, string endReplacedAt);

        /// <summary>
        /// 批量查询换单状态
        /// </summary>
        /// <param name="customerCode">客户代码</param>
        /// <param name="waybillNumbers">中性面单单号列表</param>
        /// <param name="trackingNumbers">尾程跟踪单号列表</param>
        /// <returns>换单状态查询结果列表</returns>
        Task<List<LabelReplaceStatusResult>> GetLabelReplaceStatusAsync(string customerCode, List<string> waybillNumbers, List<string> trackingNumbers);

        /// <summary>
        /// 批量取消订单
        /// </summary>
        /// <param name="customerCode">客户代码</param>
        /// <param name="apiKey">API密钥</param>
        /// <param name="waybillNumbers">中性面单单号列表</param>
        /// <returns>批量取消结果</returns>
        Task<BatchCancelResult> BatchCancelOrdersAsync(string customerCode, string apiKey, List<string> waybillNumbers);

        /// <summary>
        /// 获取数据看板数据
        /// </summary>
        /// <param name="type">数据类型：billOfLading 或 masterPackage</param>
        /// <param name="billOfLadingNumber">提单号</param>
        /// <param name="masterPackageNumber">大箱号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="customerId">客户ID</param>
        /// <returns>数据看板数据列表</returns>
        Task<List<DashboardDataDto>> GetDashboardDataAsync(string arrivalNumber, string startDate, string endDate, int? customerId);
        
        /// <summary>
        /// 将指定中性面单号对应的Label从base64转换为URL格式，保持LabelRetrievedAt不变
        /// </summary>
        /// <param name="neutralWaybillNumber">中性面单号</param>
        /// <returns>转换结果</returns>
        Task<ConvertBase64ToUrlResult> ConvertBase64LabelToUrlAsync(string neutralWaybillNumber);

        /// <summary>
        /// 获取每日标签统计数据
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="customerId">客户ID</param>
        /// <returns>每日标签统计数据列表</returns>
        Task<List<DailyLabelStatsDto>> GetDailyLabelStatsAsync(string startDate, string endDate, int? customerId);

        /// <summary>
        /// 获取每日标签统计数据（中文版本，使用自定义SQL）
        /// </summary>
        /// <returns>每日标签统计数据列表</returns>
        Task<List<DailyLabelStatsChineseDto>> GetDailyLabelStatsChineseAsync();

        /// <summary>
        /// 获取订单表中有标签的所有订单
        /// </summary>
        /// <param name="limit">限制返回的最大数量</param>
        /// <returns>有标签的订单列表</returns>
        Task<List<LabelReplaceEntity>> GetAllOrdersWithLabelsAsync(int limit = 1000);

        /// <summary>
        /// 获取指定日期范围内有标签的订单
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="limit">限制返回的最大数量</param>
        /// <returns>有标签的订单列表</returns>
        Task<List<LabelReplaceEntity>> GetOrdersWithLabelsByDateRangeAsync(DateTime startDate, DateTime endDate, int limit = 1000);

        /// <summary>
        /// 获取指定客户有标签的订单
        /// </summary>
        /// <param name="customerId">客户ID</param>
        /// <param name="limit">限制返回的最大数量</param>
        /// <returns>有标签的订单列表</returns>
        Task<List<LabelReplaceEntity>> GetOrdersWithLabelsByCustomerAsync(int customerId, int limit = 1000);

        Task<List<OpsMonitorDto>> GetOpsMonitorDataAsync();
    }

    /// <summary>
    /// base64转URL结果类
    /// </summary>
    public class ConvertBase64ToUrlResult
    {
        /// <summary>
        /// 操作状态（ok或error）
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 操作时间戳
        /// </summary>
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// 中性面单号
        /// </summary>
        public string NeutralWaybillNumber { get; set; }

        /// <summary>
        /// 是否成功转换
        /// </summary>
        public bool Converted { get; set; }

        /// <summary>
        /// 转换后的URL（如果成功）
        /// </summary>
        public string? Url { get; set; }

        /// <summary>
        /// 操作消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 错误详情（仅状态为error时有效）
        /// </summary>
        public string? ErrorDetails { get; set; }
    }

    /// <summary>
    /// 批量取消结果
    /// </summary>
    public class BatchCancelResult
    {
        /// <summary>
        /// 操作状态（ok或error）
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 操作时间戳
        /// </summary>
        public System.DateTime Timestamp { get; set; }

        /// <summary>
        /// 成功取消的订单数量
        /// </summary>
        public int SuccessCount { get; set; }

        /// <summary>
        /// 失败的订单数量
        /// </summary>
        public int FailedCount { get; set; }

        /// <summary>
        /// 失败的订单详情
        /// </summary>
        public List<CancelFailedItem> FailedItems { get; set; }

        /// <summary>
        /// 操作消息
        /// </summary>
        public string Message { get; set; }
    }

    /// <summary>
    /// 取消失败的订单项
    /// </summary>
    public class CancelFailedItem
    {
        /// <summary>
        /// 中性面单单号
        /// </summary>
        public string WaybillNumber { get; set; }

        /// <summary>
        /// 失败原因
        /// </summary>
        public string Reason { get; set; }
    }

    /// <summary>
    /// 标签替换结果
    /// </summary>
    public class LabelReplaceResult
    {
        /// <summary>
        /// 操作状态（ok或error）
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 操作时间戳
        /// </summary>
        public System.DateTime Timestamp { get; set; }

        /// <summary>
        /// 记录ID
        /// </summary>
        public int? Id { get; set; }

        /// <summary>
        /// 中性面单单号
        /// </summary>
        public string NeutralWaybillNumber { get; set; }

        /// <summary>
        /// 换单状态
        /// </summary>
        public string ReplaceStatus { get; set; }

        /// <summary>
        /// 是否成功换单
        /// </summary>
        public bool LabelReplaced { get; set; }

        /// <summary>
        /// 操作消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 错误详情（仅状态为error时有效）
        /// </summary>
        public string ErrorDetails { get; set; }
    }

    /// <summary>
    /// 换单状态查询结果
    /// </summary>
    public class LabelReplaceStatusResult
    {
        /// <summary>
        /// 中性面单单号
        /// </summary>
        public string WaybillNumber { get; set; }

        /// <summary>
        /// 尾程跟踪单号
        /// </summary>
        public string TrackingNumber { get; set; }

        /// <summary>
        /// 是否换单成功
        /// </summary>
        public bool Replaced { get; set; }

        /// <summary>
        /// 换单成功时间
        /// </summary>
        public DateTime? ReplacedAt { get; set; }
    }
}