import { describe, expect, it } from "vitest";
import * as schema from "~/database/schema";
import { SIMULATOR_MANIFEST } from "../manifest";
import { SIMULATOR_TYPES } from "../registry";
import { assertRegistrySchemaDriftFree } from "~/models/simulator";

describe("simulator manifest", () => {
  it("has a manifest profile for every registered simulator type", () => {
    expect(Object.keys(SIMULATOR_MANIFEST).toSorted()).toEqual([...SIMULATOR_TYPES].toSorted());
  });

  it("keeps the registry and database enum in sync", () => {
    expect([...schema.simulatorTypeEnum.enumValues].toSorted()).toEqual([...SIMULATOR_TYPES].toSorted());
  });

  it("assertRegistrySchemaDriftFree does not throw when registry and schema are aligned", async () => {
    await expect(assertRegistrySchemaDriftFree()).resolves.toBeUndefined();
  });

  it("documents admin setup requirements for all profiles", () => {
    for (const simulatorType of SIMULATOR_TYPES) {
      const profile = SIMULATOR_MANIFEST[simulatorType];
      expect(profile.displayName).toBeTruthy();
      expect(profile.description).toBeTruthy();
      expect(Array.isArray(profile.requiredInputs)).toBe(true);
      expect(Array.isArray(profile.optionalInputs)).toBe(true);
      expect(profile.setupSections.length).toBeGreaterThan(0);
      expect(profile.defaultConfig).toBeTypeOf("object");
    }
  });

  it("only derives inputs from declared optional inputs", () => {
    for (const simulatorType of SIMULATOR_TYPES) {
      const profile = SIMULATOR_MANIFEST[simulatorType];
      for (const alternatives of Object.values(profile.derivableInputs ?? {})) {
        for (const alternative of alternatives ?? []) {
          expect(profile.optionalInputs).toContain(alternative);
        }
      }
    }
  });

  it("keeps EPL projection and match parity as separate config knobs", () => {
    expect(SIMULATOR_MANIFEST.epl_standings.defaultConfig).toMatchObject({
      parityFactor: 400,
      matchParityFactor: 400,
      averageOpponentElo: 1500,
      baseDrawRate: 0.26,
      drawDecay: 0.002,
    });
  });
});
