import { z } from 'zod';

export const importSheetMetaSchema = z
  .object({
    name: z.string(),
    headers: z.array(z.string()),
    rowCount: z.number(),
    suggestedStepKey: z.string().nullable(),
  })
  .passthrough();

export const importStepSummarySchema = z
  .object({
    total: z.number(),
    valid: z.number(),
    error: z.number(),
    create: z.number(),
    update: z.number(),
    skip: z.number(),
  })
  .passthrough()
  .nullable();

export const importStepDetailSchema = z
  .object({
    id: z.number(),
    stepKey: z.string(),
    label: z.string(),
    sheets: z.array(z.string()),
    status: z.string(),
    mapping: z.record(z.string(), z.string()),
    summary: importStepSummarySchema,
    committedAt: z.string().nullable(),
  })
  .passthrough();

export const importRunSettingsSchema = z
  .object({
    mapping: z.record(z.string(), z.record(z.string(), z.string())).optional(),
    organization: z.string().nullable().optional(),
    updateExisting: z.boolean().optional(),
    duplicatePolicy: z.enum(['error', 'skip']).optional(),
    skipUnmatched: z.boolean().optional(),
  })
  .passthrough()
  .nullable()
  .optional();

export const importRunDetailSchema = z
  .object({
    id: z.string(),
    fileName: z.string(),
    source: z.enum(['ai', 'manual']),
    status: z.string(),
    currentStepKey: z.string().nullable(),
    createdAt: z.string(),
    sheets: z.array(importSheetMetaSchema),
    steps: z.array(importStepDetailSchema),
    settings: importRunSettingsSchema,
  })
  .passthrough();

export const importRunEnvelopeSchema = z
  .object({
    success: z.boolean(),
    data: importRunDetailSchema,
    message: z.string().optional(),
  })
  .passthrough();

/** 考勤记录 */
