/**
 * 검증 유틸리티 함수 및 타입 추출
 */

import { z } from 'zod';
import type { ItemType } from '@/types/item';
import { bendingDetailSchema, bomLineSchema } from './common';
import {
  productSchema,
  partSchema,
  materialSchema,
  consumableSchema,
  itemMasterSchema,
  itemMasterBaseSchema,
} from './item-schemas';
import {
  createItemFormSchema,
  updateItemFormSchema,
  itemFilterSchema,
} from './form-schemas';

// ===== 타입 추출 =====

export type ItemMasterFormData = z.infer<typeof itemMasterSchema>;
export type CreateItemFormData = z.infer<typeof createItemFormSchema>;
export type UpdateItemFormData = z.infer<typeof updateItemFormSchema>;
export type ItemFilterFormData = z.infer<typeof itemFilterSchema>;
export type BOMLineFormData = z.infer<typeof bomLineSchema>;
export type BendingDetailFormData = z.infer<typeof bendingDetailSchema>;

// ===== 유틸리티 함수 =====

/**
 * 품목 유형에 따른 스키마 선택
 */
export function getSchemaByItemType(itemType: ItemType) {
  switch (itemType) {
    case 'FG':
      return productSchema;
    case 'PT':
      return partSchema;
    case 'SM':
    case 'RM':
      return materialSchema;
    case 'CS':
      return consumableSchema;
    default:
      return itemMasterBaseSchema;
  }
}

/**
 * 에러 메시지 한글화
 */
export function formatZodError(error: z.ZodError): Record<string, string> {
  const formatted: Record<string, string> = {};

  error.issues.forEach((err) => {
    const path = err.path.join('.');
    formatted[path] = err.message;
  });

  return formatted;
}
