export interface DocumentTemplateRecord {
  id: string;
  organizationId: string;
  documentType: string;
  productType: string;
  fileType: string;
  templateName: string;
  description: string | null;
  isDefault: boolean;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
  updatedBy: string;
}

export interface DocumentTemplateVersionRecord {
  id: string;
  organizationId: string;
  templateId: string;
  version: string;
  filePath: string | null;
  schemaJson: unknown;
  previewImageUrl: string | null;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
}

export interface DocumentTemplateMappingRecord {
  id: string;
  organizationId: string;
  templateVersionId: string;
  placeholderKey: string;
  sourcePath: string;
  dataType: 'scalar' | 'multiline' | 'table' | 'image';
  sheetName: string | null;
  defaultValue: string | null;
  formatMask: string | null;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface DocumentTemplateTableColumnRecord {
  id: string;
  organizationId: string;
  mappingId: string;
  columnName: string;
  sourceField: string;
  columnLetter: string | null;
  sortOrder: number;
  formatMask: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface DocumentTemplateMappingColumnMutationPayload {
  id?: string;
  columnName: string;
  sourceField: string;
  columnLetter?: string | null;
  sortOrder?: number;
  formatMask?: string | null;
}

export interface DocumentTemplateMappingMutationPayload {
  placeholderKey: string;
  sourcePath: string;
  dataType: DocumentTemplateMappingRecord['dataType'];
  sheetName?: string | null;
  defaultValue?: string | null;
  formatMask?: string | null;
  sortOrder?: number;
  columns?: DocumentTemplateMappingColumnMutationPayload[];
}

export interface DocumentTemplateMappingWithColumns extends DocumentTemplateMappingRecord {
  columns: DocumentTemplateTableColumnRecord[];
}

export interface DocumentTemplateVersionDetail extends DocumentTemplateVersionRecord {
  mappings: DocumentTemplateMappingWithColumns[];
}

export interface DocumentTemplateDetail extends DocumentTemplateRecord {
  versions: DocumentTemplateVersionDetail[];
  mappingCount: number;
}

export interface DocumentTemplateListItem extends DocumentTemplateRecord {
  latestVersion: string | null;
  versionCount: number;
  mappingCount: number;
}

export interface DocumentTemplateFilters {
  documentType?: string;
  fileType?: string;
  isActive?: string;
}

export interface DocumentTemplateListResponse {
  success: boolean;
  time: string;
  message: string;
  items: DocumentTemplateListItem[];
}

export interface DocumentTemplateDetailResponse {
  success: boolean;
  time: string;
  message: string;
  template: DocumentTemplateDetail;
}

export interface DocumentTemplateVersionsResponse {
  success: boolean;
  time: string;
  message: string;
  items: DocumentTemplateVersionDetail[];
}

export interface DocumentTemplateMutationPayload {
  documentType: string;
  productType: string;
  fileType: string;
  templateName: string;
  description?: string | null;
  isDefault?: boolean;
  isActive?: boolean;
}

export interface DocumentTemplateVersionMutationPayload {
  version: string;
  filePath?: string | null;
  schemaJson: unknown;
  previewImageUrl?: string | null;
  isActive?: boolean;
}

export interface ResolveTemplateParams {
  documentType: string;
  productType?: string | null;
  fileType: string;
}

export interface ResolvedDocumentTemplate {
  template: DocumentTemplateRecord;
  version: DocumentTemplateVersionRecord;
}

export interface MutationSuccessResponse {
  success: boolean;
  message: string;
}
