import { Type } from "@sinclair/typebox";

import type { CheckerSchemas } from "../types";

import {
  createRawContentExpectationsSchema,
  createRawKeyedExpectationsSchema,
  createRawValueExpectationSchema,
  httpMethodSchema,
  sizeSchema,
  statusCodePatternSchema,
  stringMapSchema,
} from "../../schema/fragments";

export const httpCheckerSchemas: CheckerSchemas = {
  config: Type.Object(
    {
      body: Type.Optional(Type.String()),
      headers: Type.Optional(stringMapSchema),
      ignoreSSL: Type.Optional(Type.Boolean()),
      maxBodyBytes: Type.Optional(sizeSchema),
      maxRedirects: Type.Optional(Type.Integer({ minimum: 0 })),
      method: Type.Optional(httpMethodSchema),
      url: Type.String({ minLength: 1 }),
    },
    { additionalProperties: false },
  ),
  defaults: Type.Object(
    {
      headers: Type.Optional(stringMapSchema),
      maxBodyBytes: Type.Optional(sizeSchema),
    },
    { additionalProperties: false },
  ),
  expect: Type.Object(
    {
      body: Type.Optional(createRawContentExpectationsSchema()),
      durationMs: Type.Optional(createRawValueExpectationSchema()),
      headers: Type.Optional(createRawKeyedExpectationsSchema()),
      status: Type.Optional(Type.Array(statusCodePatternSchema)),
    },
    { additionalProperties: false },
  ),
};
