import { z } from "zod";
import { ZUser } from "@formbricks/database/zod/users";
import { ZUserName } from "@formbricks/types/user";
import { ZGetFilter } from "@/modules/api/v2/types/api-filter";

export const ZGetUsersFilter = ZGetFilter.extend({
  id: z.string().optional(),
  email: z.string().optional(),
}).refine(
  (data) => {
    if (data.startDate && data.endDate && data.startDate > data.endDate) {
      return false;
    }
    return true;
  },
  {
    message: "startDate must be before endDate",
  }
);

export type TGetUsersFilter = z.infer<typeof ZGetUsersFilter>;

export const ZUserInput = ZUser.omit({
  id: true,
  createdAt: true,
  updatedAt: true,
  lastLoginAt: true,
}).extend({
  isActive: ZUser.shape.isActive.optional(),
});

export type TUserInput = z.infer<typeof ZUserInput>;

export const ZUserInputPatch = ZUserInput.extend({
  // Override specific fields to be optional
  name: ZUserName.optional(),
  role: ZUser.shape.role.optional(),
  teams: ZUser.shape.teams.optional(),
  isActive: ZUser.shape.isActive.optional(),
});

export type TUserInputPatch = z.infer<typeof ZUserInputPatch>;
