import { z } from "zod";
import { createApiResponseSchema } from "./Factory";

export const UserSchema = z.object({
  id: z.string().regex(/^[0-9a-fA-F]{24}$/, {
    message: "Invalid ObjectId",
  }),
  full_name: z.string().optional(),
  username: z.string().optional(),
  email: z.string().email().optional(),
  role: z.string().optional(),
  status: z.string().optional(),
  company_id: z.string().optional(),
  department: z.string().optional(),
  position: z.string().optional(),
  phone: z.string().optional(),
  profile_image: z.string().or(z.null()).optional(),
  is_verified: z.boolean().optional(),
  last_login_at: z.number().or(z.null()).optional(),
  created_at: z.number().optional(),
  updated_at: z.number().optional(),
  updated_by: z.string().optional(),
});

export const CreateAdminSchema = z.object({
  full_name: z.string().min(1, "Full name is required"),
  email: z.string().email("Invalid email address"),
  phone: z.string().regex(/^\+?[1-9]\d{1,14}$/, "Invalid phone number"),
  password: z
    .string()
    .min(8, "Password must be at least 8 characters long")
    .regex(/[A-Z]/, "Password must contain at least one uppercase letter")
    .regex(/[a-z]/, "Password must contain at least one lowercase letter")
    .regex(/\d/, "Password must contain at least one number")
    .regex(/[\W_]/, "Password must contain at least one special character"),

  profile_image: z.string().optional(),

  department: z.string().min(1, "Department is required"),
  position: z.string().min(1, "Position is required"),
  role: z.enum(["manager", "employee", "admin", "guest"]),
  username: z.string().min(3, "Username must be at least 3 characters long"),
});

export const LoginCredentials = z.object({
  username: z.string(),
  password: z.string(),
});
export const AdminListResponse = z.object({
  users: z.array(UserSchema),
});
export const LoginResponse = z.object({
  access_token: z.string(),
  refresh_token: z.string(),
  expires_at: z.number(),
  user: UserSchema,
});
export const LogoutResponse = z.object({
  message: z.string(),
});
export const changeAdminStatusCredentialsSchema = z.object({
  status: z.string(),
});
export type IUser = z.infer<typeof UserSchema>;
export type ILogoutResponse = z.infer<typeof LogoutResponse>;
export type ILoginCredentials = z.infer<typeof LoginCredentials>;
export type ILoginResponse = z.infer<typeof LoginResponse>;
export type IAdminListResponse = z.infer<typeof AdminListResponse>;
export type TChangeAdminStatusCredentials = z.infer<
  typeof changeAdminStatusCredentialsSchema
>;
export const LogoutResponseSchema = createApiResponseSchema(LogoutResponse);
export const LoginResponseSchema = createApiResponseSchema(LoginResponse);
export const AdminListResponseSchema =
  createApiResponseSchema(AdminListResponse);
export type ICreateAdminCredentials = z.infer<typeof CreateAdminSchema>;
