/**
 * SimpleRepeaterTable 타입 정의
 */

// 컬럼 데이터 소스 설정
export interface ColumnSourceConfig {
  type: "direct" | "join" | "manual";
  sourceTable?: string;
  sourceColumn?: string;
  joinTable?: string;
  joinColumn?: string;
  joinKey?: string;
  joinRefKey?: string;
}

// 컬럼 데이터 타겟 설정
export interface ColumnTargetConfig {
  targetTable?: string;
  targetColumn?: string;
  saveEnabled?: boolean;
}

// 컬럼 설정
export interface SimpleRepeaterColumnConfig {
  field: string;
  label: string;
  type?: "text" | "number" | "date" | "select";
  width?: string;
  editable?: boolean;
  required?: boolean;
  calculated?: boolean;
  defaultValue?: any;
  placeholder?: string;
  selectOptions?: { value: string; label: string }[];
  sourceConfig?: ColumnSourceConfig;
  targetConfig?: ColumnTargetConfig;
}

// 계산 규칙
export interface CalculationRule {
  result: string;
  formula: string;
  dependencies?: string[];
}

// 초기 데이터 필터 조건
export interface DataFilterCondition {
  field: string;
  operator: "=" | "!=" | ">" | "<" | ">=" | "<=" | "LIKE" | "IN";
  value?: any;
  valueFromField?: string;
}

// 소스 조인 조건
export interface SourceJoinCondition {
  sourceKey: string;
  referenceKey: string;
}

// 초기 데이터 설정
export interface InitialDataConfig {
  sourceTable: string;
  filterConditions?: DataFilterCondition[];
  joinConditions?: SourceJoinCondition[];
}

// 합계 필드 설정
export interface SummaryFieldConfig {
  field: string;
  label: string;
  type?: "sum" | "avg" | "count" | "min" | "max";
  formula?: string; // 다른 합계 필드를 참조하는 계산식 (예: "supply_amount + tax_amount")
  format?: "number" | "currency" | "percent";
  decimals?: number;
  highlight?: boolean; // 강조 표시 (합계 행)
}

// 합계 설정
export interface SummaryConfig {
  enabled: boolean;
  position?: "bottom" | "bottom-right";
  title?: string;
  fields: SummaryFieldConfig[];
}

// 메인 Props
export interface SimpleRepeaterTableProps {
  // 기본 설정
  columns?: SimpleRepeaterColumnConfig[];
  calculationRules?: CalculationRule[];
  initialDataConfig?: InitialDataConfig;
  
  // 표시 설정
  readOnly?: boolean;
  showRowNumber?: boolean;
  allowDelete?: boolean;
  maxHeight?: string;
  
  // 행 추가 설정
  allowAdd?: boolean;
  addButtonText?: string;
  addButtonPosition?: "top" | "bottom" | "both";
  minRows?: number;
  maxRows?: number;
  newRowDefaults?: Record<string, any>;
  
  // 합계 설정
  summaryConfig?: SummaryConfig;
  
  // 데이터
  value?: any[];
  onChange?: (newData: any[]) => void;
}
