import { z } from 'zod'
import {
  kbxDecimalSchema,
  kbxNullableEntityIdSchema,
  kbxRequiredTextSchema,
  kbxTextSchema,
} from '../../../../validation/kbxFieldSchema'

export const orderHeaderSchema = z.object({
  orderDate: kbxRequiredTextSchema('orderDate', '주문일을 입력하세요.'),
  customerId: kbxNullableEntityIdSchema('customerId', '거래처를 선택하세요.'),
  warehouseId: kbxNullableEntityIdSchema('warehouseId', '출고창고를 선택하세요.'),
  receiverName: kbxRequiredTextSchema('receiverName'),
  phone: kbxRequiredTextSchema('phone'),
  postalCode: kbxTextSchema('postalCode').optional(),
  address1: kbxRequiredTextSchema('address1'),
  address2: kbxTextSchema('address2').optional(),
})

export const orderLineSchema = z.object({
  clientId: z.string(),
  itemId: kbxNullableEntityIdSchema('itemId', '품목을 선택하세요.'),
  // Positive quantity is an order rule, so the field dictionary only supplies numeric structure.
  orderQty: kbxDecimalSchema('orderQty').positive('수량은 0보다 커야 합니다.'),
  unitPrice: kbxDecimalSchema('unitPrice').nonnegative('단가는 0 이상이어야 합니다.'),
})

export const orderRegisterSchema = z.object({
  header: orderHeaderSchema,
  lines: z.array(orderLineSchema).min(1, '주문 품목을 한 건 이상 입력하세요.'),
})
