import { z } from "zod";

export const PROFESSIONAL_ROLES = [
  "consultant", "registrar", "trainee", "researcher", "student", "other",
] as const;
export type ProfessionalRole = (typeof PROFESSIONAL_ROLES)[number];

export const applicationSchema = z.object({
  name: z.string().trim().min(2).max(120),
  email: z.string().trim().toLowerCase().email(),
  password: z.string().min(12).max(200),
  country: z.string().trim().min(2).max(80),
  institution: z.string().trim().min(2).max(160),
  professionalRole: z.enum(PROFESSIONAL_ROLES),
  motivationText: z.string().trim().min(20).max(200),
  orcidUrl: z
    .string()
    .trim()
    .url()
    .startsWith("https://orcid.org/")
    .or(z.literal(""))
    .optional(),
  linkedinUrl: z
    .string()
    .trim()
    .url()
    .includes("linkedin.com")
    .or(z.literal(""))
    .optional(),
  // Optional profile fields — captured on /join and editable later on
  // /account/profile. Safe-to-display on the internal member directory.
  bio: z.string().trim().max(600).optional().default(""),
  specialty: z.string().trim().max(120).optional().default(""),
  yearsExperience: z
    .string()
    .trim()
    .optional()
    .transform((v) => (v && v.length > 0 ? Number(v) : undefined))
    .refine(
      (v) => v === undefined || (Number.isFinite(v) && v >= 0 && v <= 80),
      "Enter a number between 0 and 80",
    ),
  websiteUrl: z
    .string()
    .trim()
    .url()
    .or(z.literal(""))
    .optional(),
  twitterUrl: z
    .string()
    .trim()
    .url()
    .or(z.literal(""))
    .optional(),
});

export type ApplicationInput = z.infer<typeof applicationSchema>;
