export interface ReportTemplate {
  template_id: string;
  template_name_kor: string;
  template_name_eng: string | null;
  template_type: string;
  is_system: string;
  thumbnail_url: string | null;
  description: string | null;
  layout_config: string | null;
  default_queries: string | null;
  use_yn: string;
  sort_order: number;
  created_at: Date;
  created_by: string | null;
  updated_at: Date | null;
  updated_by: string | null;
}

export interface ReportMaster {
  report_id: string;
  report_name_kor: string;
  report_name_eng: string | null;
  template_id: string | null;
  template_name: string | null;
  report_type: string;
  company_code: string | null;
  description: string | null;
  use_yn: string;
  created_at: Date;
  created_by: string | null;
  updated_at: Date | null;
  updated_by: string | null;
}

export interface ReportLayout {
  layout_id: string;
  report_id: string;
  canvas_width: number;
  canvas_height: number;
  page_orientation: string;
  margin_top: number;
  margin_bottom: number;
  margin_left: number;
  margin_right: number;
  components: string | null;
  created_at: Date;
  created_by: string | null;
  updated_at: Date | null;
  updated_by: string | null;
}

export interface ReportQuery {
  query_id: string;
  report_id: string;
  query_name: string;
  query_type: "MASTER" | "DETAIL";
  sql_query: string;
  parameters: string[] | null;
  external_connection_id: number | null;
  display_order: number;
  created_at: Date;
  created_by: string | null;
  updated_at: Date | null;
  updated_by: string | null;
}

export interface ReportDetail {
  report: ReportMaster;
  layout: ReportLayout | null;
  queries: ReportQuery[];
  menuObjids?: number[];
}

export interface GetReportsParams {
  page?: number;
  limit?: number;
  searchText?: string;
  searchField?: "report_name" | "created_by" | "report_type" | "updated_at" | "created_at";
  startDate?: string;
  endDate?: string;
  reportType?: string;
  useYn?: string;
  sortBy?: string;
  sortOrder?: "ASC" | "DESC";
}

export interface GetReportsResponse {
  items: ReportMaster[];
  total: number;
  page: number;
  limit: number;
  typeSummary: Array<{ type: string; count: number }>;
  allTypes: string[];
  recentActivity: Array<{ date: string; count: number }>;
  recentTotal: number;
}

export interface CreateReportRequest {
  reportNameKor: string;
  reportNameEng?: string;
  templateId?: string;
  reportType: string;
  description?: string;
  companyCode?: string;
}

export interface UpdateReportRequest {
  reportNameKor?: string;
  reportNameEng?: string;
  reportType?: string;
  description?: string;
  useYn?: string;
}

export interface WatermarkConfig {
  enabled: boolean;
  type: "text" | "image";
  text?: string;
  fontSize?: number;
  fontColor?: string;
  imageUrl?: string;
  opacity: number;
  style: "diagonal" | "center" | "tile";
  rotation?: number;
}

export interface PageConfig {
  page_id: string;
  page_name: string;
  page_order: number;
  width: number;
  height: number;
  background_color: string;
  margins: {
    top: number;
    bottom: number;
    left: number;
    right: number;
  };
  components: Record<string, unknown>[];
}

export interface ReportLayoutConfig {
  pages: PageConfig[];
  watermark?: WatermarkConfig;
}

export interface SaveLayoutQueryItem {
  id: string;
  name: string;
  type: "MASTER" | "DETAIL";
  sqlQuery: string;
  parameters: string[];
  externalConnectionId?: number | null;
}

export interface SaveLayoutRequest {
  layoutConfig: ReportLayoutConfig;
  queries?: SaveLayoutQueryItem[];
  menuObjids?: number[];
}

export interface ReportMenuMapping {
  mapping_id: number;
  report_id: string;
  menu_objid: number;
  company_code: string;
  created_at: Date;
  created_by: string | null;
}

export interface GetTemplatesResponse {
  system: ReportTemplate[];
  custom: ReportTemplate[];
}

export interface CreateTemplateRequest {
  templateNameKor: string;
  templateNameEng?: string;
  templateType: string;
  description?: string;
  layoutConfig?: Record<string, unknown>;
  defaultQueries?: Array<Record<string, unknown>>;
}

export interface ComponentConfig {
  id: string;
  type: string;
  x: number;
  y: number;
  width: number;
  height: number;
  zIndex: number;
  fontSize?: number;
  fontFamily?: string;
  fontWeight?: string;
  fontColor?: string;
  backgroundColor?: string;
  borderWidth?: number;
  borderColor?: string;
  borderRadius?: number;
  textAlign?: string;
  padding?: number;
  queryId?: string;
  fieldName?: string;
  defaultValue?: string;
  format?: string;
  visible?: boolean;
  printable?: boolean;
  conditional?: string;
  locked?: boolean;
  groupId?: string;
  imageUrl?: string;
  objectFit?: "contain" | "cover" | "fill" | "none";
  orientation?: "horizontal" | "vertical";
  lineStyle?: "solid" | "dashed" | "dotted" | "double";
  lineWidth?: number;
  lineColor?: string;
  showLabel?: boolean;
  labelText?: string;
  labelPosition?: "top" | "left" | "bottom" | "right";
  personName?: string;
  tableColumns?: Array<{
    field: string;
    header: string;
    width?: number;
    align?: "left" | "center" | "right";
  }>;
  headerBackgroundColor?: string;
  headerTextColor?: string;
  showBorder?: boolean;
  rowHeight?: number;
  pageNumberFormat?: "number" | "numberTotal" | "koreanNumber";
  cardTitle?: string;
  cardItems?: Array<{
    label: string;
    value: string;
    fieldName?: string;
  }>;
  labelWidth?: number;
  showCardBorder?: boolean;
  showCardTitle?: boolean;
  titleFontSize?: number;
  labelFontSize?: number;
  valueFontSize?: number;
  titleColor?: string;
  labelColor?: string;
  valueColor?: string;
  calcItems?: Array<{
    label: string;
    value: number | string;
    operator: "+" | "-" | "x" | "÷";
    fieldName?: string;
  }>;
  resultLabel?: string;
  resultColor?: string;
  resultFontSize?: number;
  showCalcBorder?: boolean;
  numberFormat?: "none" | "comma" | "currency";
  currencySuffix?: string;
  barcodeType?: "CODE128" | "CODE39" | "EAN13" | "EAN8" | "UPC" | "QR";
  barcodeValue?: string;
  barcodeFieldName?: string;
  showBarcodeText?: boolean;
  barcodeColor?: string;
  barcodeBackground?: string;
  barcodeMargin?: number;
  qrErrorCorrectionLevel?: "L" | "M" | "Q" | "H";
  qrDataFields?: Array<{
    fieldName: string;
    label: string;
  }>;
  qrUseMultiField?: boolean;
  qrIncludeAllRows?: boolean;
  checkboxChecked?: boolean;
  checkboxFieldName?: string;
  checkboxLabel?: string;
  checkboxSize?: number;
  checkboxColor?: string;
  checkboxBorderColor?: string;
  checkboxLabelPosition?: "left" | "right";
  visualQuery?: VisualQuery;
  // 카드 레이아웃 설정 (card 컴포넌트 전용 - v3)
  cardLayoutConfig?: CardLayoutConfig;
}

export interface VisualQueryFormulaColumn {
  alias: string;
  header: string;
  expression: string;
}

export interface VisualQuery {
  tableName: string;
  limit?: number;
  columns: string[];
  formulaColumns: VisualQueryFormulaColumn[];
}

// ─────────────────────────────────────────────────────────────────────────────
// 카드 레이아웃 v3 타입 정의
// ─────────────────────────────────────────────────────────────────────────────

export type CardElementType = "header" | "dataCell" | "divider" | "badge";
export type CellDirection = "vertical" | "horizontal";

export interface CardElementBase {
  id: string;
  type: CardElementType;
  colspan?: number;
  rowspan?: number;
}

export interface CardHeaderElement extends CardElementBase {
  type: "header";
  icon?: string;
  iconColor?: string;
  title: string;
  titleColor?: string;
  titleFontSize?: number;
}

export interface CardDataCellElement extends CardElementBase {
  type: "dataCell";
  direction: CellDirection;
  label: string;
  columnName?: string;
  inputType?: "text" | "date" | "number" | "select" | "readonly";
  required?: boolean;
  placeholder?: string;
  selectOptions?: string[];
  labelWidth?: number;
  labelFontSize?: number;
  labelColor?: string;
  valueFontSize?: number;
  valueColor?: string;
}

export interface CardDividerElement extends CardElementBase {
  type: "divider";
  style?: "solid" | "dashed" | "dotted";
  color?: string;
  thickness?: number;
}

export interface CardBadgeElement extends CardElementBase {
  type: "badge";
  label?: string;
  columnName?: string;
  colorMap?: Record<string, string>;
}

export type CardElement =
  | CardHeaderElement
  | CardDataCellElement
  | CardDividerElement
  | CardBadgeElement;

export interface CardLayoutRow {
  id: string;
  gridColumns: number;
  elements: CardElement[];
  height?: string;
}

export interface CardLayoutConfig {
  tableName?: string;
  primaryKey?: string;
  rows: CardLayoutRow[];
  padding?: string;
  gap?: string;
  borderStyle?: string;
  borderColor?: string;
  backgroundColor?: string;
  headerTitleFontSize?: number;
  headerTitleColor?: string;
  labelFontSize?: number;
  labelColor?: string;
  valueFontSize?: number;
  valueColor?: string;
  dividerThickness?: number;
  dividerColor?: string;
}
