import { z } from "zod";
import { SafeFileCtor } from "@/utils/safeFile";

export const importLeadSchema = z
  .object({
    name: z.string().optional(),
    interestType: z.enum(["buy", "rent", "lease"]),
    source: z.string().min(1, "Source is required"),
    assignmentMode: z.enum(["equal", "roundRobin", "specificUser"]),
    roundRobin: z.boolean(),

    // Conditional fields for assignment
    team: z.string().optional(),
    assignedTo: z.array(z.string()).optional(),
    specificUser: z.string().optional(),

    // Buy preference fields
    buyingPreference: z
      .enum(["open_to_suggestions", "preferred_project"])
      .optional(),
    project: z.string().optional(),
    propertyTypeBuy: z.union([z.string(), z.array(z.string())]).optional(),
    preferredState: z.string().optional(),

    configuration: z.array(z.string()).or(z.string()).optional(),
    category: z.string().optional(),
    preferredCity: z.string().optional(),
    preferredLocalities: z.array(z.string()).optional(),

    file: z.instanceof(SafeFileCtor as any).optional(),
    // Optional integration platform (used only in Capture Leads UI)
    platform: z.string().optional(),
  })
  .superRefine((data, ctx) => {
    // Assignment mode validations
    if (data.assignmentMode === "equal" && !data.team) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "Team is required for equal assignment",
        path: ["team"],
      });
    }

    if (
      data.assignmentMode === "roundRobin" &&
      (!data.assignedTo || data.assignedTo.length === 0)
    ) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "At least one user is required for round robin",
        path: ["assignedTo"],
      });
    }

    if (data.assignmentMode === "specificUser" && !data.specificUser) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "Specific user is required",
        path: ["specificUser"],
      });
    }

    // Buy preference validations (only for buy interest type)
    // Skip these when called from specific Capture Leads platforms
    const skipBuyPrefValidation =
      (data as any).platform &&
      ["99acers", "housing", "magicbricks", "nobroker"].includes(
        String((data as any).platform).toLowerCase(),
      );

    if (data.interestType === "buy" && !skipBuyPrefValidation) {
      if (!data.buyingPreference) {
        ctx.addIssue({
          code: z.ZodIssueCode.custom,
          message: "Buying preference is required for buy leads",
          path: ["buyingPreference"],
        });
      }

      if (data.buyingPreference === "preferred_project" && !data.project) {
        ctx.addIssue({
          code: z.ZodIssueCode.custom,
          message: "Project is required when preferred project is selected",
          path: ["project"],
        });
      }

      if (data.buyingPreference === "open_to_suggestions") {
        if (!data.propertyTypeBuy || data.propertyTypeBuy.length === 0) {
          ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "At least one property type is required",
            path: ["propertyTypeBuy"],
          });
        }

        if (!data.preferredState) {
          ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: "Preferred state is required",
            path: ["preferredState"],
          });
        }
      }
    }

    // Property type validation for non-buy interest types
    if (
      ["rent", "lease"].includes(data.interestType) &&
      (!data.propertyTypeBuy || data.propertyTypeBuy.length === 0)
    ) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "Property type is required",
        path: ["propertyTypeBuy"],
      });
    }
  });
