import { Banknote } from 'lucide-react';
import type { DetailConfig, FieldDefinition } from '@/components/templates/IntegratedDetailTemplate/types';
import type { DepositRecord } from './types';
import { DEPOSIT_TYPE_SELECTOR_OPTIONS } from './types';
import { getVendors, getBankAccounts } from './actions';

// ===== 필드 정의 =====
const fields: FieldDefinition[] = [
  // 입금일
  {
    key: 'depositDate',
    label: '입금일',
    type: 'date',
    placeholder: '입금일을 선택해주세요',
    disabled: (mode) => mode === 'view',
  },
  // 입금계좌
  {
    key: 'bankAccountId',
    label: '입금계좌',
    type: 'select',
    placeholder: '선택',
    fetchOptions: async () => {
      const result = await getBankAccounts();
      if (result.success) {
        return result.data.map((a) => ({
          value: a.id,
          label: a.name,
        }));
      }
      return [];
    },
    disabled: (mode) => mode === 'view',
  },
  // 입금자명
  {
    key: 'depositorName',
    label: '입금자명',
    type: 'text',
    placeholder: '입금자명을 입력해주세요',
    disabled: (mode) => mode === 'view',
  },
  // 입금금액
  {
    key: 'depositAmount',
    label: '입금금액',
    type: 'currency',
    placeholder: '입금금액을 입력해주세요',
    disabled: (mode) => mode === 'view',
  },
  // 적요 (editable)
  {
    key: 'note',
    label: '적요',
    type: 'text',
    placeholder: '적요를 입력해주세요',
    gridSpan: 2,
    disabled: (mode) => mode === 'view',
  },
  // 거래처 (editable, required)
  {
    key: 'vendorId',
    label: '거래처',
    type: 'select',
    required: true,
    placeholder: '선택',
    fetchOptions: async () => {
      const result = await getVendors();
      if (result.success) {
        return result.data.map((v) => ({
          value: v.id,
          label: v.name,
        }));
      }
      return [];
    },
    disabled: (mode) => mode === 'view',
  },
  // 입금 유형 (editable, required)
  {
    key: 'depositType',
    label: '입금 유형',
    type: 'select',
    required: true,
    defaultValue: 'unset',
    placeholder: '선택',
    options: DEPOSIT_TYPE_SELECTOR_OPTIONS.map((opt) => ({
      value: opt.value,
      label: opt.label,
    })),
    disabled: (mode) => mode === 'view',
  },
];

// ===== Config 정의 =====
export const depositDetailConfig: DetailConfig = {
  title: '입금',
  description: '입금 상세 내역을 등록합니다',
  icon: Banknote,
  basePath: '/accounting/deposits',
  fields,
  gridColumns: 2,
  actions: {
    showBack: true,
    showDelete: true,
    showEdit: true,
    backLabel: '목록',
    deleteLabel: '삭제',
    editLabel: '수정',
    deleteConfirmMessage: {
      title: '입금 삭제',
      description: '이 입금 내역을 삭제하시겠습니까? 삭제된 데이터는 복구할 수 없습니다.',
    },
  },
  transformInitialData: (data: Record<string, unknown>): Record<string, unknown> => {
    const record = data as unknown as DepositRecord;
    return {
      depositDate: record.depositDate || '',
      bankAccountId: record.bankAccountId || '',
      depositorName: record.depositorName || '',
      depositAmount: record.depositAmount || 0,
      note: record.note || '',
      vendorId: record.vendorId || '',
      depositType: record.depositType || 'unset',
    };
  },
  transformSubmitData: (formData: Record<string, unknown>): Partial<DepositRecord> => {
    return {
      depositDate: formData.depositDate as string,
      bankAccountId: formData.bankAccountId as string,
      depositorName: formData.depositorName as string,
      depositAmount: formData.depositAmount ? Number(formData.depositAmount) : 0,
      note: formData.note as string,
      vendorId: formData.vendorId as string,
      depositType: formData.depositType as DepositRecord['depositType'],
    };
  },
};
