export type ImportStepKey = 'students' | 'rooms' | 'checkins' | 'transfers';

export interface ImportSheetMeta {
  name: string;
  headers: string[];
  rowCount: number;
  suggestedStepKey: ImportStepKey | null;
}

export interface ImportStepSummary {
  total: number;
  valid: number;
  error: number;
  create: number;
  update: number;
  skip: number;
}

export interface ImportStepDetail {
  id: number;
  stepKey: ImportStepKey;
  label: string;
  sheets: string[];
  status: 'pending' | 'ready' | 'committing' | 'committed' | 'failed' | 'skipped';
  mapping: Record<string, string>;
  summary: ImportStepSummary | null;
  committedAt: string | null;
}

export interface ImportRunDetail {
  id: string;
  fileName: string;
  source: 'ai' | 'manual';
  status: 'preparing' | 'ready' | 'committing' | 'committed' | 'failed' | 'expired';
  currentStepKey: ImportStepKey | null;
  createdAt: string;
  sheets: ImportSheetMeta[];
  steps: ImportStepDetail[];
  settings?: {
    mapping?: Partial<Record<ImportStepKey, Record<string, string>>>;
    organization?: string | null;
    updateExisting?: boolean;
    duplicatePolicy?: 'error' | 'skip';
    skipUnmatched?: boolean;
  };
}

export interface ImportStageRequest {
  stepKey: ImportStepKey;
  /** 兼容旧调用：单个工作表名。 */
  sheet?: string;
  /** 一个阶段可包含多张工作表；与 sheet 二选一（sheets 优先）。 */
  sheets?: string[];
  headerRow?: number;
}

export interface ImportPreviewRow {
  id: number;
  rowNumber: number;
  sheetName: string;
  raw: Record<string, string | number | boolean | null>;
  fields: Record<string, string | number | boolean | null>;
  action: 'create' | 'update' | 'skip' | null;
  status: 'pending' | 'valid' | 'error' | 'committed' | 'skipped';
  errors: string[];
}

export interface ImportPreviewResult {
  stepKey: ImportStepKey;
  sheetNames: string[];
  headers: string[];
  mapping: Record<string, string>;
  rows: ImportPreviewRow[];
  summary: ImportStepSummary;
}

export interface ImportReceipt {
  runId: string;
  stepKey: ImportStepKey;
  status: 'committed' | 'already_committed' | 'conflict';
  created: number;
  updated: number;
  skipped: number;
  failed: number;
  total: number;
  nextStepKey: ImportStepKey | null;
  runStatus: 'preparing' | 'ready' | 'committing' | 'committed' | 'failed' | 'expired';
  message: string;
}

export const STEP_FIELDS: Record<
  ImportStepKey,
  Array<{ key: string; label: string; required?: boolean; identity?: boolean }>
> = {
  students: [
    { key: 'name', label: '姓名', required: true },
    { key: 'studentNo', label: '学号', identity: true },
    { key: 'phone', label: '手机号', identity: true },
    { key: 'gender', label: '性别' },
    { key: 'idNumber', label: '身份证号' },
    { key: 'ethnicity', label: '民族' },
    { key: 'emergencyContact', label: '紧急联系人' },
    { key: 'emergencyPhone', label: '紧急联系电话' },
    { key: 'organization', label: '校区' },
    { key: 'status', label: '状态' },
  ],
  rooms: [
    { key: 'roomNumber', label: '宿舍号', required: true },
    { key: 'building', label: '楼栋' },
    { key: 'floor', label: '楼层' },
    { key: 'capacity', label: '容量', required: true },
    { key: 'roomType', label: '房型' },
    { key: 'rentalCategory', label: '租期类型' },
    { key: 'monthlyRate', label: '月租' },
  ],
  checkins: [
    { key: 'name', label: '姓名' },
    { key: 'studentNo', label: '学号', identity: true },
    { key: 'phone', label: '手机号', identity: true },
    { key: 'roomNumber', label: '宿舍号', required: true },
    { key: 'checkInDate', label: '入住日期', required: true },
    { key: 'stayType', label: '住宿类型' },
  ],
  transfers: [
    { key: 'studentNo', label: '学号', identity: true },
    { key: 'phone', label: '手机号', identity: true },
    { key: 'oldRoom', label: '原宿舍', required: true },
    { key: 'newRoom', label: '新宿舍', required: true },
    { key: 'transferDate', label: '换宿日期', required: true },
    { key: 'reason', label: '原因/备注' },
  ],
};
