/**
 * 부적합관리 타입 정의
 */

// ===== 코드 상수 =====

export type NcType = 'material' | 'process' | 'construction' | 'other';
export type NcStatus = 'RECEIVED' | 'ANALYZING' | 'RESOLVED' | 'CLOSED';

export const NC_TYPE_OPTIONS: { value: NcType; label: string }[] = [
  { value: 'material', label: '자재불량' },
  { value: 'process', label: '공정불량' },
  { value: 'construction', label: '시공불량' },
  { value: 'other', label: '기타' },
];

export const NC_STATUS_OPTIONS: { value: NcStatus; label: string }[] = [
  { value: 'RECEIVED', label: '접수' },
  { value: 'ANALYZING', label: '분석중' },
  { value: 'RESOLVED', label: '조치완료' },
  { value: 'CLOSED', label: '종결' },
];

export const NC_STATUS_CONFIG: Record<NcStatus, { label: string; className: string }> = {
  RECEIVED: { label: '접수', className: 'bg-gray-100 text-gray-700 border-gray-200' },
  ANALYZING: { label: '분석중', className: 'bg-yellow-100 text-yellow-700 border-yellow-200' },
  RESOLVED: { label: '조치완료', className: 'bg-blue-100 text-blue-700 border-blue-200' },
  CLOSED: { label: '종결', className: 'bg-green-100 text-green-700 border-green-200' },
};

export const NC_TYPE_LABELS: Record<NcType, string> = {
  material: '자재불량',
  process: '공정불량',
  construction: '시공불량',
  other: '기타',
};

// ===== 목록 아이템 =====

export interface NonconformingListItem {
  id: number;
  nc_number: string;
  status: NcStatus;
  nc_type: NcType;
  occurred_at: string;
  confirmed_at: string | null;
  site_name: string | null;
  total_cost: number;
  creator: { id: number; name: string };
  item: { id: number; name: string } | null;
  order: { id: number; order_number: string } | null;
}

// ===== 통계 =====

export interface NonconformingStats {
  by_status: Record<NcStatus, number>;
  total_count: number;
  total_cost: number;
}

// ===== 자재 상세 아이템 =====

export interface NonconformingReportItem {
  id?: number;
  item_id?: number | null;
  item_name: string;
  specification: string;
  quantity: number;
  unit_price: number;
  amount: number;
  sort_order: number;
  remarks: string;
}

// ===== 결재 정보 =====

export interface ApprovalStep {
  id: number;
  step_order: number;
  step_type: string;
  approver_id: number;
  status: string;
  comment: string | null;
  acted_at: string | null;
  approver: { id: number; name: string };
}

export interface ApprovalInfo {
  id: number;
  document_number: string;
  status: string;
  completed_at: string | null;
  steps: ApprovalStep[];
}

// ===== 단건 상세 =====

export interface NonconformingDetail {
  id: number;
  nc_number: string;
  status: NcStatus;
  nc_type: NcType;
  occurred_at: string;
  confirmed_at: string | null;
  site_name: string | null;
  department: { id: number; name: string } | null;
  order: { id: number; order_number: string; site_name: string } | null;
  item: { id: number; name: string } | null;
  defect_quantity: number | null;
  unit: string | null;
  defect_description: string | null;
  cause_analysis: string | null;
  corrective_action: string | null;
  action_completed_at: string | null;
  action_manager: { id: number; name: string } | null;
  related_employee: { id: number; name: string } | null;
  material_cost: number;
  shipping_cost: number;
  construction_cost: number;
  other_cost: number;
  total_cost: number;
  remarks: string | null;
  drawing_location: string | null;
  items: NonconformingReportItem[];
  files: unknown[];
  approval: ApprovalInfo | null;
  creator: { id: number; name: string };
  created_at: string;
  updated_at: string;
}

// ===== 폼 데이터 =====

export interface NonconformingFormData {
  nc_type: NcType;
  occurred_at: string;
  confirmed_at: string;
  site_name: string;
  department_id: number | null;
  order_id: number | null;
  item_id: number | null;
  defect_quantity: number | string;
  unit: string;
  defect_description: string;
  cause_analysis: string;
  corrective_action: string;
  action_manager_id: number | null;
  related_employee_id: number | null;
  shipping_cost: number | string;
  construction_cost: number | string;
  other_cost: number | string;
  drawing_location: string;
  remarks: string;
  items: NonconformingReportItem[];
}

export const INITIAL_FORM_DATA: NonconformingFormData = {
  nc_type: 'material',
  occurred_at: '',
  confirmed_at: '',
  site_name: '',
  department_id: null,
  order_id: null,
  item_id: null,
  defect_quantity: '',
  unit: 'EA',
  defect_description: '',
  cause_analysis: '',
  corrective_action: '',
  action_manager_id: null,
  related_employee_id: null,
  shipping_cost: 0,
  construction_cost: 0,
  other_cost: 0,
  drawing_location: '',
  remarks: '',
  items: [],
};

export const EMPTY_ITEM: NonconformingReportItem = {
  item_id: null,
  item_name: '',
  specification: '',
  quantity: 0,
  unit_price: 0,
  amount: 0,
  sort_order: 0,
  remarks: '',
};
