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

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

import {
  createAuthoringContentExpectationsSchema,
  createAuthoringKeyedExpectationsSchema,
  createAuthoringValueExpectationSchema,
  createNormalizedContentExpectationsSchema,
  createNormalizedKeyedExpectationsSchema,
  createNormalizedValueExpectationSchema,
} from "../../schema/fragments";

export const dbCheckerSchemas: CheckerSchemas = {
  authoring: {
    config: createDbConfigSchema(),
    expect: createDbExpectSchema("authoring"),
  },
  normalized: {
    config: createDbConfigSchema(),
    expect: createDbExpectSchema("normalized"),
  },
};

function createDbConfigSchema() {
  return Type.Object(
    {
      query: Type.Optional(
        Type.String({
          minLength: 1,
        }),
      ),
      url: Type.String({ minLength: 1 }),
    },
    { additionalProperties: false },
  );
}

function createDbExpectSchema(kind: "authoring" | "normalized") {
  return Type.Object(
    {
      durationMs: Type.Optional(
        kind === "authoring" ? createAuthoringValueExpectationSchema() : createNormalizedValueExpectationSchema(),
      ),
      result: Type.Optional(
        kind === "authoring" ? createAuthoringContentExpectationsSchema() : createNormalizedContentExpectationsSchema(),
      ),
      rowCount: Type.Optional(
        kind === "authoring" ? createAuthoringValueExpectationSchema() : createNormalizedValueExpectationSchema(),
      ),
      rows: Type.Optional(
        Type.Array(
          kind === "authoring" ? createAuthoringKeyedExpectationsSchema() : createNormalizedKeyedExpectationsSchema(),
        ),
      ),
    },
    { additionalProperties: false },
  );
}
