/**
 * 공정 작업기준 컴포넌트 타입 정의
 */

// 작업 단계 정의 (사용자가 추가/삭제/이름변경 가능)
export interface WorkPhaseDefinition {
  key: string;
  label: string;
  sortOrder: number;
}

// 상세 유형 정의 (사용자가 추가/삭제 가능)
export interface DetailTypeDefinition {
  value: string;
  label: string;
}

// 데이터 소스 설정
export interface DataSourceConfig {
  itemTable: string;
  itemNameColumn: string;
  itemCodeColumn: string;
  routingVersionTable: string;
  routingFkColumn: string;
  routingVersionNameColumn: string;
  routingDetailTable: string;
  processTable: string;
  processNameColumn: string;
  processCodeColumn: string;
}

// 전체 Config
export interface ProcessWorkStandardConfig {
  dataSource: DataSourceConfig;
  phases: WorkPhaseDefinition[];
  detailTypes: DetailTypeDefinition[];
  splitRatio?: number;
  leftPanelTitle?: string;
  readonly?: boolean;
  /** 품목 목록 모드: all=전체, registered=등록된 품목만 */
  itemListMode?: "all" | "registered";
  /** 등록 모드 시 화면 코드 (자동 설정됨) */
  screenCode?: string;
}

// ============================================================
// 데이터 모델
// ============================================================

export interface ItemData {
  id: string;
  item_name: string;
  item_code: string;
}

export interface RoutingVersion {
  id: string;
  version_name: string;
  description?: string;
  created_date?: string;
  processes: RoutingProcess[];
}

export interface RoutingProcess {
  routing_detail_id: string;
  seq_no: string;
  process_code: string;
  process_name: string;
  is_required?: string;
  work_type?: string;
}

export interface WorkItem {
  id: string;
  routing_detail_id: string;
  work_phase: string;
  title: string;
  is_required: string;
  sort_order: number;
  description?: string;
  detail_count: number;
  created_date?: string;
}

export interface WorkItemDetail {
  id: string;
  work_item_id: string;
  detail_type?: string;
  content: string;
  is_required: string;
  sort_order: number;
  remark?: string;
  created_date?: string;

  // 검사항목(inspection) 전용
  process_inspection_apply?: string; // "apply" | "none"
  inspection_code?: string;
  inspection_method?: string;
  unit?: string;
  base_value?: string;
  tolerance?: string;
  lower_limit?: string;
  upper_limit?: string;
  auto_collect?: string; // "Y" | "N"
  plc_data?: string;

  // 작업절차(procedure) 전용
  duration_minutes?: number;

  // 직접입력(input) 전용
  input_type?: string;

  // 문서참조(lookup) 전용
  lookup_target?: string;
  display_fields?: string;

  // 설비점검(equip_inspection) 전용
  equip_inspection_apply?: string; // "apply" | "none"

  // 설비조건(equip_condition) 전용
  condition_name?: string;
  condition_unit?: string;
  condition_base_value?: string;
  condition_tolerance?: string;
  condition_auto_collect?: string;
  condition_plc_data?: string;

  // 실적등록(production_result) 전용
  work_qty_auto_collect?: string;
  work_qty_plc_data?: string;
  defect_qty_auto_collect?: string;
  defect_qty_plc_data?: string;
  good_qty_auto_collect?: string;
  good_qty_plc_data?: string;

  // 자재투입(material_input) 전용 - BOM 자동연동
  material_code?: string;
  material_name?: string;
  quantity?: string;
  material_unit?: string;
}

export interface InspectionStandard {
  id: string;
  inspection_code: string;
  inspection_item: string;
  inspection_method: string;
  unit: string;
  lower_limit?: string;
  upper_limit?: string;
}

// ============================================================
// 컴포넌트 Props
// ============================================================

export interface ProcessWorkStandardComponentProps {
  config: ProcessWorkStandardConfig;
  formData?: Record<string, any>;
  isPreview?: boolean;
  tableName?: string;
  screenId?: number | string;
}

// 선택 상태
export interface SelectionState {
  itemCode: string | null;
  itemName: string | null;
  routingVersionId: string | null;
  routingVersionName: string | null;
  routingDetailId: string | null;
  processName: string | null;
}
