import { z } from "zod";

export const AGE_BANDS = [
  "18-24",
  "25-34",
  "35-44",
  "45-54",
  "55-64",
  "65+",
  "prefer_not",
] as const;

export const AGE_BAND_LABELS: Record<(typeof AGE_BANDS)[number], string> = {
  "18-24": "18–24",
  "25-34": "25–34",
  "35-44": "35–44",
  "45-54": "45–54",
  "55-64": "55–64",
  "65+": "65+",
  prefer_not: "Rather not say",
};

export const PARTICIPANT_COUNTRIES = [
  { code: "US", name: "United States" },
  { code: "CA", name: "Canada" },
  { code: "GB", name: "United Kingdom" },
  { code: "IE", name: "Ireland" },
  { code: "AU", name: "Australia" },
  { code: "NZ", name: "New Zealand" },
  { code: "IN", name: "India" },
  { code: "PK", name: "Pakistan" },
  { code: "BD", name: "Bangladesh" },
  { code: "AE", name: "United Arab Emirates" },
  { code: "SA", name: "Saudi Arabia" },
  { code: "DE", name: "Germany" },
  { code: "FR", name: "France" },
  { code: "ES", name: "Spain" },
  { code: "IT", name: "Italy" },
  { code: "NL", name: "Netherlands" },
  { code: "SE", name: "Sweden" },
  { code: "ZA", name: "South Africa" },
  { code: "NG", name: "Nigeria" },
  { code: "KE", name: "Kenya" },
  { code: "BR", name: "Brazil" },
  { code: "MX", name: "Mexico" },
  { code: "PH", name: "Philippines" },
  { code: "SG", name: "Singapore" },
  { code: "JP", name: "Japan" },
] as const;

const COUNTRY_CODES = PARTICIPANT_COUNTRIES.map((c) => c.code) as [
  string,
  ...string[],
];

export const ONBOARDING_INTENTS = ["create", "participate", "both"] as const;
export type OnboardingIntentChoice = (typeof ONBOARDING_INTENTS)[number];

const profileFields = {
  countryCode: z.enum(COUNTRY_CODES, { message: "Select your country" }),
  regionCode: z
    .string()
    .trim()
    .max(64, "Region is too long")
    .optional()
    .transform((v) => (v ? v : null)),
  ageBand: z.enum(AGE_BANDS, { message: "Select an age range" }),
};

export const participantOnboardingSchema = z.object({
  ...profileFields,
  intent: z.enum(["participate", "both"]),
  acceptTerms: z.literal(true, { message: "Please accept the participant terms" }),
  domesticFeedConsent: z.boolean(),
});

export type ParticipantOnboardingInput = z.input<
  typeof participantOnboardingSchema
>;

export const updateParticipantProfileSchema = z.object(profileFields);

export type UpdateParticipantProfileInput = z.input<
  typeof updateParticipantProfileSchema
>;

export const setActiveModeSchema = z.object({
  mode: z.enum(["creator", "participant"]),
});

export const consentChangeSchema = z.object({
  policyKey: z.string().min(1).max(64),
  grant: z.boolean(),
});
