import { z } from "zod";
import { UNITS } from "@app/shared-types";

export const uuidSchema = z.uuid();

export const idParamSchema = z.object({ id: uuidSchema });

export const dateStringSchema = z
  .string()
  .regex(/^\d{4}-\d{2}-\d{2}$/, "Datum måste vara YYYY-MM-DD");

export const paginationQuerySchema = z.object({
  limit: z.coerce.number().int().min(1).max(100).default(30),
  offset: z.coerce.number().int().min(0).default(0),
});
export type PaginationQuery = z.infer<typeof paginationQuerySchema>;

export const unitSchema = z.enum(UNITS);

export const quantitySchema = z.number().positive().max(1_000_000);

export const confidenceSchema = z.number().min(0).max(1);

/** Standardiserat felsvar från API:t. */
export const apiErrorSchema = z.object({
  error: z.object({
    code: z.string(),
    message: z.string(),
    details: z.unknown().optional(),
    correlationId: z.string().optional(),
  }),
});
export type ApiError = z.infer<typeof apiErrorSchema>;
