import type { JsonObject } from '../../types/json.js'
import { logInfo } from '../../utils/logger.js'
import { settleOverdueWorkOrders } from '../worker-platform/worker-service.js'

export async function runWorkOrderTimeoutJob(job: JsonObject) {
  const config =
    job.config && typeof job.config === 'object' && !Array.isArray(job.config)
      ? (job.config as JsonObject)
      : {}
  const scanLimit = Math.max(1, Number(config.scanLimit || 50))

  const result = await settleOverdueWorkOrders({ limit: scanLimit })

  logInfo('[work-order-timeout]', '超时工单扫描完成', result)

  return {
    ok: result.processedCount === result.checkedCount,
    status: 'ok',
    message: `扫描 ${result.checkedCount} 个，处置 ${result.processedCount} 个，跳过 ${result.skippedCount} 个`,
    checkedCount: result.checkedCount,
    failedCount: result.skippedCount,
    asset: result.processedCount,
  }
}
