import { PlantingOrder } from '../aggregates/planting-order.aggregate';
import { PlantingOrderStatus } from '../value-objects/planting-order-status.enum';

export interface IPlantingOrderRepository {
  save(order: PlantingOrder): Promise<void>;
  findById(orderId: bigint): Promise<PlantingOrder | null>;
  findByOrderNo(orderNo: string): Promise<PlantingOrder | null>;
  findByUserId(
    userId: bigint,
    page?: number,
    pageSize?: number,
  ): Promise<PlantingOrder[]>;
  findByStatus(
    status: PlantingOrderStatus,
    limit?: number,
  ): Promise<PlantingOrder[]>;
  findPendingPoolScheduling(): Promise<PlantingOrder[]>;
  findByBatchId(batchId: bigint): Promise<PlantingOrder[]>;
  findReadyForMining(): Promise<PlantingOrder[]>;
  countTreesByUserId(userId: bigint): Promise<number>;
  countByUserId(userId: bigint): Promise<number>;
  /**
   * 查找用户已支付（PAID 及之后状态）但未创建合同的订单
   * 用于用户完成 KYC 后补创建合同
   */
  findPaidOrdersWithoutContract(userId: bigint): Promise<PlantingOrder[]>;

  /**
   * 获取全局统计数据（从订单表实时聚合）
   */
  getGlobalStats(): Promise<GlobalPlantingStats>;

  /**
   * 获取今日统计数据
   */
  getTodayStats(): Promise<DailyPlantingStats>;

  /**
   * 获取本月统计数据
   * [2026-01-06] 新增
   */
  getMonthStats(): Promise<DailyPlantingStats>;

  /**
   * 按状态获取订单分布统计
   */
  getStatusDistribution(): Promise<StatusDistribution>;

  /**
   * 获取趋势数据
   * [2026-01-06] 新增
   * @param period 时间维度: day(最近30天), week(最近12周), month(最近12月), quarter(最近8季度), year(最近5年)
   */
  getTrendData(period: TrendPeriod): Promise<TrendDataPoint[]>;
}

/** 全局认种统计 */
export interface GlobalPlantingStats {
  totalTreeCount: number;
  totalOrderCount: number;
  totalAmount: string;
}

/** 每日认种统计 */
export interface DailyPlantingStats {
  treeCount: number;
  orderCount: number;
  amount: string;
}

/** 订单状态分布 */
export interface StatusDistribution {
  paid: number;
  fundAllocated: number;
  poolScheduled: number;
  poolInjected: number;
  miningEnabled: number;
}

/** 趋势时间维度 [2026-01-06] 新增 */
export type TrendPeriod = 'day' | 'week' | 'month' | 'quarter' | 'year';

/** 趋势数据点 [2026-01-06] 新增 */
export interface TrendDataPoint {
  /** 时间标签 (如 "2026-01-06", "2026-W01", "2026-01", "2026-Q1", "2026") */
  label: string;
  /** 认种棵数 */
  treeCount: number;
  /** 订单数 */
  orderCount: number;
  /** 认种金额 */
  amount: string;
}

export const PLANTING_ORDER_REPOSITORY = Symbol('IPlantingOrderRepository');
