/**
 * 출하 샘플 데이터 생성기
 */

import {
  randomPick,
  today,
  randomRemark,
} from './index';
import type {
  ShipmentCreateFormData,
  ShipmentPriority,
  DeliveryMethod,
  VehicleTonnageOption,
  LotOption,
  LogisticsOption,
} from '@/components/outbound/ShipmentManagement/types';

// 우선순위
const PRIORITIES: ShipmentPriority[] = ['urgent', 'normal', 'low'];

// 배송방식
const DELIVERY_METHODS: DeliveryMethod[] = ['pickup', 'direct', 'logistics'];

/**
 * 출하 폼 데이터 생성
 */
export interface GenerateShipmentDataOptions {
  lotOptions?: LotOption[];  // 로트 목록
  logisticsOptions?: LogisticsOption[];  // 물류사 목록
  tonnageOptions?: VehicleTonnageOption[]; // 차량 톤수 목록
  lotNo?: string;                       // 지정 로트번호 (플로우에서 전달)
}

export function generateShipmentData(
  options: GenerateShipmentDataOptions = {}
): ShipmentCreateFormData {
  const {
    lotOptions = [],
    logisticsOptions = [],
    tonnageOptions = [],
    lotNo,
  } = options;

  // 로트 선택
  let selectedLotNo = lotNo || '';
  if (!selectedLotNo && lotOptions.length > 0) {
    selectedLotNo = randomPick(lotOptions).value;
  }

  // 배송방식
  const deliveryMethod = randomPick(DELIVERY_METHODS);

  // 물류사 (물류사 배송일 때만)
  let logisticsCompany = '';
  if (deliveryMethod === 'logistics' && logisticsOptions.length > 0) {
    logisticsCompany = randomPick(logisticsOptions).label;
  }

  // 차량 톤수
  let vehicleTonnage = '';
  if (tonnageOptions.length > 0) {
    vehicleTonnage = randomPick(tonnageOptions).value;
  }

  return {
    lotNo: selectedLotNo,
    scheduledDate: today(),
    priority: randomPick(PRIORITIES),
    deliveryMethod,
    logisticsCompany,
    vehicleTonnage,
    loadingTime: '',
    loadingManager: '',
    remarks: randomRemark(),
    vehicleDispatches: [],
  };
}