import { z } from "zod";

const stringField = (requiredMessage: string) =>
  z.preprocess(
    (value) => value ?? "",
    z.string().trim().min(1, requiredMessage),
  );

export const paymentSettingsSchema = z.object({
  stripePublishableKey: stringField(
    "Stripe publishable key is required.",
  ).refine((value) => value.startsWith("pk_"), {
    message: "Stripe publishable key must start with 'pk_'.",
  }),

  stripeSecretKey: stringField("Stripe secret key is required.").refine(
    (value) => value.startsWith("sk_"),
    {
      message: "Stripe secret key must start with 'sk_'.",
    },
  ),

  stripeWebhookSecret: stringField("Stripe webhook secret is required.").refine(
    (value) => value.startsWith("whsec_"),
    {
      message: "Stripe webhook secret must start with 'whsec_'.",
    },
  ),
});

export type PaymentSettingsFormValues = z.infer<typeof paymentSettingsSchema>;
