/**
 * 노임관리 상세 페이지 설정
 * IntegratedDetailTemplate용 config
 */

import { Hammer } from 'lucide-react';
import type { DetailConfig, FieldDefinition } from '@/components/templates/IntegratedDetailTemplate';
import type { LaborFormData, LaborCategory, LaborStatus } from './types';
import { CATEGORY_OPTIONS, STATUS_OPTIONS } from './constants';

// Select 옵션 변환 ('all' 제외)
const categoryFieldOptions = CATEGORY_OPTIONS
  .filter((o) => o.value !== 'all')
  .map((o) => ({ value: o.value, label: o.label }));

const statusFieldOptions = STATUS_OPTIONS
  .filter((o) => o.value !== 'all')
  .map((o) => ({ value: o.value, label: o.label }));

// 필드 정의
const fields: FieldDefinition[] = [
  {
    key: 'laborNumber',
    label: '노임번호',
    type: 'text',
    required: true,
    placeholder: '노임번호를 입력하세요',
  },
  {
    key: 'category',
    label: '구분',
    type: 'select',
    required: true,
    options: categoryFieldOptions,
    placeholder: '구분 선택',
  },
  {
    key: 'minM',
    label: '최소 M',
    type: 'number',
    placeholder: '0.00',
    helpText: '소수점 둘째자리까지 입력 가능',
  },
  {
    key: 'maxM',
    label: '최대 M',
    type: 'number',
    placeholder: '0.00',
    helpText: '소수점 둘째자리까지 입력 가능',
  },
  {
    key: 'laborPrice',
    label: '노임단가',
    type: 'number',
    placeholder: '0',
    formatValue: (value) => {
      if (value === null || value === undefined || value === '') return '-';
      return Number(value).toLocaleString('ko-KR');
    },
  },
  {
    key: 'status',
    label: '상태',
    type: 'select',
    required: true,
    options: statusFieldOptions,
    placeholder: '상태 선택',
  },
];

// DetailConfig (Record<string, unknown> 제약 때문에 타입 캐스팅 필요)
export const laborDetailConfig: DetailConfig = {
  title: '노임',
  description: '노임 정보를 등록하고 관리합니다.',
  icon: Hammer,
  basePath: '/construction/order/base-info/labor',
  fields,
  gridColumns: 2,
  actions: {
    showBack: true,
    showDelete: true,
    showEdit: true,
    backLabel: '목록',
    deleteLabel: '삭제',
    editLabel: '수정',
    submitLabel: undefined, // 모드에 따라 자동 결정 (등록/저장)
    cancelLabel: '취소',
    deleteConfirmMessage: {
      title: '노임 삭제',
      description: '이 노임을 삭제하시겠습니까? 이 작업은 취소할 수 없습니다.',
    },
  },
  transformInitialData: (data: Record<string, unknown>): Record<string, unknown> => ({
    laborNumber: data.laborNumber,
    category: data.category,
    minM: data.minM,
    maxM: data.maxM,
    laborPrice: data.laborPrice,
    status: data.status,
  }),
  transformSubmitData: (formData: Record<string, unknown>): Partial<LaborFormData> => ({
    laborNumber: formData.laborNumber as string,
    category: formData.category as LaborCategory,
    minM: Number(formData.minM) || 0,
    maxM: Number(formData.maxM) || 0,
    laborPrice: formData.laborPrice === '' || formData.laborPrice === null
      ? null
      : Number(formData.laborPrice),
    status: formData.status as LaborStatus,
  }),
};

export default laborDetailConfig;
