/**
 * [BATCH-OP-001] 批量操作类型定义
 * 本文件定义批量操作相关的所有类型
 * AI注意: 所有批量操作相关类型必须从此文件导入
 */

export enum BatchOperationType {
  BATCH_EDIT_PRICE = 'BATCH_EDIT_PRICE',
  BATCH_EDIT_INVENTORY = 'BATCH_EDIT_INVENTORY',
  BATCH_EDIT_TITLE = 'BATCH_EDIT_TITLE',
  BATCH_EDIT_CATEGORY = 'BATCH_EDIT_CATEGORY',
  BATCH_EDIT_PACKAGE_WEIGHT = 'BATCH_EDIT_PACKAGE_WEIGHT',
  BATCH_EDIT_PACKAGE_SIZE = 'BATCH_EDIT_PACKAGE_SIZE',
  BATCH_EDIT_COD = 'BATCH_EDIT_COD',
  BATCH_EDIT_SELLER_SKU = 'BATCH_EDIT_SELLER_SKU',
  BATCH_LISTING = 'BATCH_LISTING',
  BATCH_DELIST = 'BATCH_DELIST',
  BATCH_AD_CREATE = 'BATCH_AD_CREATE',
  BATCH_AD_RESUME = 'BATCH_AD_RESUME',
  BATCH_AD_PAUSE = 'BATCH_AD_PAUSE',
  BATCH_AD_ADJUST_BUDGET = 'BATCH_AD_ADJUST_BUDGET',
  BATCH_AD_CHANGE_CREATIVE = 'BATCH_AD_CHANGE_CREATIVE',
}

export enum BatchOperationStatus {
  PENDING = 'PENDING',
  AI_ANALYZING = 'AI_ANALYZING',
  AI_RECOMMENDED = 'AI_RECOMMENDED',
  AWAITING_CONFIRM = 'AWAITING_CONFIRM',
  EXECUTING = 'EXECUTING',
  COMPLETED = 'COMPLETED',
  PARTIAL_SUCCESS = 'PARTIAL_SUCCESS',
  FAILED = 'FAILED',
  CANCELLED = 'CANCELLED',
}

export enum BatchOperationResult {
  SUCCESS = 'SUCCESS',
  PARTIAL_SUCCESS = 'PARTIAL_SUCCESS',
  FAILED = 'FAILED',
  AI_AUTO_EXECUTED = 'AI_AUTO_EXECUTED',
  HUMAN_CONFIRMED = 'HUMAN_CONFIRMED',
  HUMAN_REJECTED = 'HUMAN_REJECTED',
}

export interface BatchOperationRecord {
  id: string;
  tenantId: string;
  shopId: string;
  operatorId: string;
  operatorName: string;
  operationType: BatchOperationType;
  status: BatchOperationStatus;
  targetType: 'PRODUCT' | 'AD_CAMPAIGN' | 'AD_CREATIVE';
  targetIds: string[];
  totalCount: number;
  successCount: number;
  failedCount: number;
  beforeValues: Record<string, any>[];
  afterValues: Record<string, any>[];
  params?: BatchEditPriceParams | BatchEditInventoryParams | BatchListingParams | BatchDelistParams;
  aiConfidence?: number;
  aiReason?: string;
  riskLevel?: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
  subscriptionLevel?: 'FREE' | 'BASIC' | 'PROFESSIONAL' | 'ENTERPRISE';
  aiAutoExecuted: boolean;
  confirmedAt?: Date;
  executedAt?: Date;
  completedAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export interface BatchOperationItem {
  id: string;
  batchId: string;
  targetId: string;
  targetType: 'PRODUCT' | 'AD_CAMPAIGN' | 'AD_CREATIVE';
  beforeValue: Record<string, any>;
  afterValue: Record<string, any>;
  result: 'SUCCESS' | 'FAILED' | 'PENDING';
  errorMessage?: string;
  executedAt?: Date;
}

export interface BatchEditPriceParams {
  productIds: string[];
  priceChange: {
    type: 'ABSOLUTE' | 'PERCENTAGE';
    value: number;
  };
  currency?: string;
}

export interface BatchEditInventoryParams {
  productIds: string[];
  inventoryChange: {
    type: 'ABSOLUTE' | 'PERCENTAGE';
    value: number;
  };
}

export interface BatchEditTitleParams {
  productIds: string[];
  titleTemplate?: string;
  aiGenerated?: boolean;
}

export interface BatchListingParams {
  productIds: string[];
  targetPlatforms: string[];
  targetShops: string[];
  listingData?: {
    price?: number;
    inventory?: number;
    category?: string;
  };
}

export interface BatchDelistParams {
  productIds: string[];
  reason?: string;
}

export interface BatchOperationQuery {
  tenantId: string;
  shopId?: string;
  operationType?: BatchOperationType;
  status?: BatchOperationStatus;
  startDate?: Date;
  endDate?: Date;
  operatorId?: string;
  page?: number;
  pageSize?: number;
}

export interface BatchOperationSummary {
  totalOperations: number;
  totalItems: number;
  successRate: number;
  aiAutoExecuteRate: number;
  averageConfidence: number;
  operationsByType: Record<BatchOperationType, number>;
  operationsByStatus: Record<BatchOperationStatus, number>;
}

export interface BatchOperationAIConfig {
  subscriptionLevel: 'FREE' | 'BASIC' | 'PROFESSIONAL' | 'ENTERPRISE';
  autoExecuteConfidence: number;
  pendingReviewConfidence: number;
  allowedRiskLevels: ('LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL')[];
  maxAutoBudget?: number;
  maxAutoQuantity?: number;
}

export const BATCH_OPERATION_AI_CONFIG: Record<string, BatchOperationAIConfig> = {
  FREE: {
    subscriptionLevel: 'FREE',
    autoExecuteConfidence: 1.0,
    pendingReviewConfidence: 1.0,
    allowedRiskLevels: [],
    maxAutoBudget: 0,
    maxAutoQuantity: 0,
  },
  BASIC: {
    subscriptionLevel: 'BASIC',
    autoExecuteConfidence: 0.95,
    pendingReviewConfidence: 0.80,
    allowedRiskLevels: ['LOW'],
    maxAutoBudget: 1000,
    maxAutoQuantity: 50,
  },
  PROFESSIONAL: {
    subscriptionLevel: 'PROFESSIONAL',
    autoExecuteConfidence: 0.80,
    pendingReviewConfidence: 0.60,
    allowedRiskLevels: ['LOW', 'MEDIUM'],
    maxAutoBudget: 10000,
    maxAutoQuantity: 200,
  },
  ENTERPRISE: {
    subscriptionLevel: 'ENTERPRISE',
    autoExecuteConfidence: 0.55,
    pendingReviewConfidence: 0.40,
    allowedRiskLevels: ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL'],
    maxAutoBudget: Infinity,
    maxAutoQuantity: Infinity,
  },
};

export const AI_CAN_AUTO_EXECUTE: Record<BatchOperationType, boolean> = {
  [BatchOperationType.BATCH_EDIT_PRICE]: true,
  [BatchOperationType.BATCH_EDIT_INVENTORY]: true,
  [BatchOperationType.BATCH_EDIT_TITLE]: true,
  [BatchOperationType.BATCH_EDIT_CATEGORY]: false,
  [BatchOperationType.BATCH_EDIT_PACKAGE_WEIGHT]: true,
  [BatchOperationType.BATCH_EDIT_PACKAGE_SIZE]: true,
  [BatchOperationType.BATCH_EDIT_COD]: false,
  [BatchOperationType.BATCH_EDIT_SELLER_SKU]: false,
  [BatchOperationType.BATCH_LISTING]: true,
  [BatchOperationType.BATCH_DELIST]: true,
  [BatchOperationType.BATCH_AD_CREATE]: true,
  [BatchOperationType.BATCH_AD_RESUME]: true,
  [BatchOperationType.BATCH_AD_PAUSE]: true,
  [BatchOperationType.BATCH_AD_ADJUST_BUDGET]: true,
  [BatchOperationType.BATCH_AD_CHANGE_CREATIVE]: true,
};

export const OPERATION_TYPE_LABELS: Record<BatchOperationType, string> = {
  [BatchOperationType.BATCH_EDIT_PRICE]: '批量修改价格',
  [BatchOperationType.BATCH_EDIT_INVENTORY]: '批量修改库存',
  [BatchOperationType.BATCH_EDIT_TITLE]: '批量修改标题',
  [BatchOperationType.BATCH_EDIT_CATEGORY]: '批量修改类目',
  [BatchOperationType.BATCH_EDIT_PACKAGE_WEIGHT]: '批量修改包装重量',
  [BatchOperationType.BATCH_EDIT_PACKAGE_SIZE]: '批量修改包装尺寸',
  [BatchOperationType.BATCH_EDIT_COD]: '批量修改到货付款',
  [BatchOperationType.BATCH_EDIT_SELLER_SKU]: '批量修改Seller SKU',
  [BatchOperationType.BATCH_LISTING]: '批量上架',
  [BatchOperationType.BATCH_DELIST]: '批量下架',
  [BatchOperationType.BATCH_AD_CREATE]: '批量创建广告',
  [BatchOperationType.BATCH_AD_RESUME]: '批量启动广告',
  [BatchOperationType.BATCH_AD_PAUSE]: '批量暂停广告',
  [BatchOperationType.BATCH_AD_ADJUST_BUDGET]: '批量调整预算',
  [BatchOperationType.BATCH_AD_CHANGE_CREATIVE]: '批量更换素材',
};

export const STATUS_LABELS: Record<BatchOperationStatus, string> = {
  [BatchOperationStatus.PENDING]: '待处理',
  [BatchOperationStatus.AI_ANALYZING]: 'AI分析中',
  [BatchOperationStatus.AI_RECOMMENDED]: 'AI已建议',
  [BatchOperationStatus.AWAITING_CONFIRM]: '待确认',
  [BatchOperationStatus.EXECUTING]: '执行中',
  [BatchOperationStatus.COMPLETED]: '已完成',
  [BatchOperationStatus.PARTIAL_SUCCESS]: '部分成功',
  [BatchOperationStatus.FAILED]: '失败',
  [BatchOperationStatus.CANCELLED]: '已取消',
};
