import request from '@/config/axios'

/**
 * 生产计划 VO
 */
export interface ProductionPlanVO {
  id?: number
  planCode: string
  planName: string
  planType: number // 1-年度 2-月度 3-周度
  planPeriod: string
  startDate: string
  endDate: string
  status?: number
  parentPlanId?: number
  hasChildPlans?: boolean // 是否有子计划
  totalPlanQuantity?: number
  totalCompletedQuantity?: number
  totalInProgressQuantity?: number
  overallCompletionRate?: number
  remark?: string
  createTime?: string
  items?: ProductionPlanItemVO[]
}

/**
 * 生产计划明细 VO
 */
export interface ProductionPlanItemVO {
  id?: number
  planId?: number
  productId: number
  productCode?: string
  productName?: string
  planQuantity: number
  completedQuantity?: number
  inProgressQuantity?: number
  completionRate?: number
  routeId?: number
  routeName?: string
  priority?: number
  plannedStartDate?: string
  plannedEndDate?: string
  actualStartDate?: string
  actualEndDate?: string
  remark?: string
}

/**
 * 生产计划分页查询参数
 */
export interface ProductionPlanPageReqVO {
  pageNo: number
  pageSize: number
  planCode?: string
  planName?: string
  planType?: number
  planPeriod?: string
  status?: number[]
  startDate?: string[]
  createTime?: string[]
}

/**
 * 生成工单请求参数
 */
export interface GenerateOrderReqVO {
  planId: number
  itemIds: number[]
  splitStrategy?: string // daily weekly single
  batchSize?: number
  autoApprove?: boolean
}

/**
 * 生成月度计划请求参数
 */
export interface GenerateMonthlyPlanReqVO {
  annualPlanId: number
  months: number[] // 要生成的月份，如 [1,2,3] 表示1月、2月、3月
  splitStrategy?: string // average proportional
}

/**
 * 获取生产计划分页
 */
export function getProductionPlanPage(params: ProductionPlanPageReqVO) {
  return request.get({ url: '/mes/production-plan/page', params })
}

/**
 * 获取生产计划详情
 */
export function getProductionPlan(id: number) {
  return request.get({ url: '/mes/production-plan/get', params: { id } })
}

/**
 * 创建生产计划
 */
export function createProductionPlan(data: ProductionPlanVO) {
  return request.post({ url: '/mes/production-plan/create', data })
}

/**
 * 更新生产计划
 */
export function updateProductionPlan(data: ProductionPlanVO) {
  return request.put({ url: '/mes/production-plan/update', data })
}

/**
 * 删除生产计划
 */
export function deleteProductionPlan(id: number) {
  return request.delete({ url: '/mes/production-plan/delete', params: { id } })
}

/**
 * 发布生产计划
 */
export function publishProductionPlan(id: number) {
  return request.post({ url: '/mes/production-plan/publish', params: { id } })
}

/**
 * 关闭生产计划
 */
export function closeProductionPlan(id: number) {
  return request.post({ url: '/mes/production-plan/close', params: { id } })
}

/**
 * 从生产计划生成工单
 */
export function generateWorkOrders(data: GenerateOrderReqVO) {
  return request.post({ url: '/mes/production-plan/generate-orders', data })
}

/**
 * 更新计划进度
 */
export function updatePlanProgress(id: number) {
  return request.post({ url: '/mes/production-plan/update-progress', params: { id } })
}

/**
 * 从年度计划生成月度计划
 */
export function generateMonthlyPlans(data: GenerateMonthlyPlanReqVO) {
  return request.post({ url: '/mes/production-plan/generate-monthly-plans', data })
}

/**
 * 生产计划分析结果 VO
 */
export interface ProductionPlanAnalysisVO {
  planId: number
  planCode: string
  planName: string
  totalPlanQuantity: number
  totalCompletedQuantity: number
  totalRemainingQuantity: number
  estimatedProductionHours: number
  estimatedDeliveryDate: string
  suggestedPurchaseLeadDays: number
  suggestedPurchaseDate: string
  productAnalysisList: ProductAnalysisItem[]
  materialRequirements: MaterialRequirementItem[]
  analysisDescription: string
  riskWarnings: string[]
}

/**
 * 产品分析项
 */
export interface ProductAnalysisItem {
  productId: number
  productCode: string
  productName: string
  planQuantity: number
  completedQuantity: number
  remainingQuantity: number
  unit: string
  routeName: string
  estimatedHours: number
  plannedStartDate: string
  plannedEndDate: string
  materialRequirements: MaterialRequirementItem[]
}

/**
 * 辅料/物料需求项
 */
export interface MaterialRequirementItem {
  materialId: number
  materialCode: string
  materialName: string
  materialType: string
  unit: string
  requiredQuantity: number
  currentStock: number
  stockGap: number
  stockStatus: 'sufficient' | 'warning' | 'shortage'
  suggestedPurchaseQuantity: number
  purchaseLeadTime: number
  unitPrice: number
  estimatedPurchaseAmount: number
}

/**
 * 分析生产计划 - 计算辅料需求、生产周期、库存、交货期
 */
export function analyzeProductionPlan(id: number) {
  return request.get<ProductionPlanAnalysisVO>({ url: '/mes/production-plan/analyze', params: { id } })
}
