import { z } from "zod";

// ─── Step 1: Meeting ────────────────────────────────────────────────────────
export const meetingSchema = z.object({
  title: z.string().min(1, "Title / Objective is required"),
  projectName: z.string().min(1, "Project Name is required"),
  place: z.string().min(1, "Place is required"),
  date: z.string().min(1, "Date is required"),
  time: z.string().min(1, "Time is required"),
  duration: z
    .string()
    .min(1, "Duration is required")
    .refine((v) => Number(v) > 0, { message: "Duration must be greater than 0" }),
  durationUnit: z.string().min(1, "Duration unit is required"),
  calledBy: z.string().min(1, "Called By is required"),
});

// ─── Step 2: Attendees ──────────────────────────────────────────────────────
export const attendeeSchema = z.object({
  name: z.string().min(1, "Attendee name is required"),
  role: z.string().min(1, "Role is required"),
  contact: z.string().optional(),
});

export const attendeesSchema = z.object({
  attendees: z
    .array(attendeeSchema)
    .min(1, "At least one attendee is required"),
});

// ─── Step 3: Agenda ─────────────────────────────────────────────────────────
export const agendaItemSchema = z.object({
  item: z.string().min(1, "Agenda item is required"),
  owner: z.string().min(1, "Owner / Presenter is required"),
});

export const agendaSchema = z.object({
  agenda: z
    .array(agendaItemSchema)
    .min(1, "At least one agenda item is required"),
});

// ─── Step 4: Decisions ──────────────────────────────────────────────────────
export const decisionsSchema = z.object({
  decisions: z
    .array(z.string().min(1, "Decision cannot be empty"))
    .min(1, "At least one decision is required"),
});

// ─── Step 5: Actions ────────────────────────────────────────────────────────
export const actionItemSchema = z.object({
  item: z.string().min(1, "Action item is required"),
  responsible: z.string().min(1, "Responsible person is required"),
  dueDate: z.string().min(1, "Due date is required"),
});

export const actionsSchema = z.object({
  actions: z
    .array(actionItemSchema)
    .min(1, "At least one action item is required"),
});

// ─── Step 6: Others ─────────────────────────────────────────────────────────
// Adjust fields to match your OthersData shape
export const othersSchema = z.object({
  notes: z.string().optional(),
});

// ─── Step schema map (index = step number) ──────────────────────────────────
export const stepSchemas: Record<number, z.ZodTypeAny> = {
  1: meetingSchema,
  2: attendeesSchema,
  3: agendaSchema,
  4: decisionsSchema,
  5: actionsSchema,
  6: othersSchema,
};

// ─── Utility: validate a single step ────────────────────────────────────────
export type StepErrors = Record<string, string>;

export function validateStep(step: number, data: unknown): StepErrors {
  const schema = stepSchemas[step];
  if (!schema) return {};
  const result = schema.safeParse(data);
  if (result.success) return {};

  const errors: StepErrors = {};
  const zodError = result.error as z.ZodError;
  zodError.issues.forEach((issue) => {
    // path examples: ["title"], ["attendees", 0, "name"]
    const key = issue.path.join(".");
    if (!errors[key]) errors[key] = issue.message;
  });
  return errors;
}

// ─── Utility: does a step have errors? ──────────────────────────────────────
export function stepHasErrors(step: number, data: unknown): boolean {
  return Object.keys(validateStep(step, data)).length > 0;
}