import { z } from 'zod';

export const problemDetailsSchema = z.object({
  type: z.string().url().or(z.literal('about:blank')),
  title: z.string(),
  status: z.number().int(),
  detail: z.string().optional(),
  instance: z.string().optional(),
  errors: z
    .array(
      z.object({
        field: z.string().optional(),
        message: z.string(),
      }),
    )
    .optional(),
});

export type ProblemDetails = z.infer<typeof problemDetailsSchema>;

export function createProblemDetails(
  input: Omit<ProblemDetails, 'type'> & { type?: string },
): ProblemDetails {
  return problemDetailsSchema.parse({
    type: input.type ?? 'about:blank',
    title: input.title,
    status: input.status,
    detail: input.detail,
    instance: input.instance,
    errors: input.errors,
  });
}
