import { z } from "zod";

/** A root-relative path or an http(s) URL. */
export const linkSchema = z
  .string()
  .trim()
  .max(500)
  .refine(
    (value) =>
      value === "" ||
      /^\/(?!\/)/.test(value) ||
      /^https?:\/\/[^\s]+$/i.test(value),
    "Use a full https:// URL or a path starting with /",
  );

/**
 * Values new articles start with. Editors can change them per article; these
 * only save typing.
 */
export const blogDefaultsSchema = z.object({
  ctaEnabled: z.boolean().default(true),
  ctaTitle: z
    .string()
    .trim()
    .max(120)
    .default("Get your second number today"),
  ctaDescription: z
    .string()
    .trim()
    .max(300)
    .default(
      "Call and text over Wi-Fi from a real US or Canadian number, on the phone you already own.",
    ),
  ctaButtonText: z.string().trim().max(40).default("Get the app"),
  ctaButtonUrl: linkSchema.default("/download"),
  ctaImage: z.string().trim().max(1000).nullable().default(null),
  faqSchemaEnabled: z.boolean().default(true),
  defaultAuthorId: z.string().max(64).nullable().default(null),
  defaultCategoryId: z.string().max(64).nullable().default(null),
});

export type BlogDefaults = z.infer<typeof blogDefaultsSchema>;

/** Every settings key and its schema. Add new site-wide settings here. */
export const settingSchemas = {
  blogDefaults: blogDefaultsSchema,
} as const;

export type SettingKey = keyof typeof settingSchemas;
