import { z } from "zod";

export const PromoRewardSchema = z.object({
  type: z.string().min(1, "Reward type is required"),
  value: z.number(),
  name: z.string().optional().nullable(),
  currency: z.string().optional().nullable(),
  expiresAt: z.string().optional().nullable(),
  ttl: z.number().optional().nullable(),
  /**
   * The reward's slug, as the upstream API returns and accepts it.
   *
   * Needed explicitly: `sanitizePayload` validates with this schema, and zod strips
   * keys it does not know about — so a slug sent by the console was being dropped here
   * before the request ever left, which read as the field being ignored upstream.
   *
   * `createdAt` is deliberately absent. The API returns it, but it is set when the
   * reward is created and accepting it would let a client rewrite its own history.
   */
  slug: z.string().optional().nullable(),
});

export const PromoAccessListSchema = z.object({
  membershipAccounts: z.array(z.string()).default([]),
  membershipClubs: z.array(z.string()).default([]),
  membershipAccountTypes: z.array(z.string()).default([]),
});

export const PromoSignupCodeSchema = z.object({
  serviceCenterID: z.string().min(1, "serviceCenterID is required"),
  edmTemplateIDs: z.array(z.string()).default([]),
  externalMembershipEntityID: z
    .string()
    .min(1, "externalMembershipEntityID is required"),
  membershipAccountTypeID: z
    .string()
    .min(1, "membershipAccountTypeID is required"),
  externalMembershipID: z.string().optional().nullable(),
  verificationEdmTemplateID: z.string().optional().nullable(),
  countryRecordIDs: z.array(z.string()).default([]),
  viewpointChargeCenterID: z.string().optional().nullable(),
});

export const CreatePromoCodeSchema = z.object({
  name: z.string().min(1, "Name is required"),
  isActive: z.boolean(),
  note: z.string().min(1, "Note is required"),
  code: z.string().min(1, "Code is required"),
  expiresAt: z.string().datetime().nullable().optional(),
  description: z.string().min(1, "Description is required"),
  rewards: z.array(PromoRewardSchema).min(1, "At least one reward is required"),
  access_list: PromoAccessListSchema,
  signupPromoCodes: z
    .array(PromoSignupCodeSchema)
    .min(1, "At least one signup promo code is required"),
});

export const UpdatePromoCodeSchema = CreatePromoCodeSchema.partial();


export const ListPromoCodesQuerySchema = z.object({
  isActive: z.enum(["true", "false"]).optional(),
  onlyPromoCodes: z.enum(["true", "false"]).optional(),
  onlyMemberReferralCodes: z.enum(["true", "false"]).optional(),
  includeExpired: z.enum(["true", "false"]).optional(),
  searchTerm: z.string().optional(),
  limit: z.string().regex(/^\d+$/).optional(),
  offset: z.string().regex(/^\d+$/).optional(),
});

export type PromoReward = z.infer<typeof PromoRewardSchema>;
export type PromoAccessList = z.infer<typeof PromoAccessListSchema>;
export type PromoSignupCode = z.infer<typeof PromoSignupCodeSchema>;

export type CreatePromoCodeRequest = z.infer<typeof CreatePromoCodeSchema>;
export type UpdatePromoCodeRequest = z.infer<typeof UpdatePromoCodeSchema>;
export type ListPromoCodesQuery = z.infer<typeof ListPromoCodesQuerySchema>;
