import {
  isLocation,
  isMonster,
  isNpc,
  isScene,
  isSecret,
  isSession,
  isTreasure,
  type AnyDocument,
} from "@/lib/types";
import { LocationEditForm } from "./location/LocationEditForm";
import { MonsterEditForm } from "./monsters/MonsterEditForm";
import { NpcEditForm } from "./npc/NpcEditForm";
import { SceneEditForm } from "./scene/SceneEditForm";
import { SecretEditForm } from "./secret/SecretEditForm";
import { SessionEditForm } from "./session/SessionEditForm";
import { TreasureEditForm } from "./treasure/TreasureEditForm";

function assertUnreachable(_x: never): never {
  throw new Error("DocumentForm switch is not exhaustive");
}

/**
 * Renders a form for any document type depending on the relationship.
 */
export const DocumentEditForm = ({ document }: { document: AnyDocument }) => {
  if (isLocation(document)) {
    return <LocationEditForm location={document} />;
  }

  if (isMonster(document)) {
    return <MonsterEditForm monster={document} />;
  }

  if (isNpc(document)) {
    return <NpcEditForm npc={document} />;
  }

  if (isScene(document)) {
    return <SceneEditForm scene={document} />;
  }

  if (isSecret(document)) {
    return <SecretEditForm secret={document} />;
  }

  if (isSession(document)) {
    return <SessionEditForm session={document} />;
  }

  if (isTreasure(document)) {
    return <TreasureEditForm treasure={document} />;
  }

  return assertUnreachable(document);
};
