// DocumentRow.tsx
// Generic row component for displaying any document type.
import { SecretToggleRow } from "@/components/documents/secret/SecretToggleRow";
import {
  isLocation,
  isMonster,
  isNpc,
  isScene,
  isSecret,
  isSession,
  isTreasure,
  type Document,
  type Session,
} from "@/lib/types";
import { BasicRow } from "./BasicRow";
import { TreasureToggleRow } from "./treasure/TreasureToggleRow";

/**
 * Renders a row for any document type. Prioritizes Session, then Secret, then falls back to ID and creation time.
 * If rendering a SecretRow, uses the provided session prop if available.
 */
export const DocumentRow = ({
  document,
  session,
}: {
  document: Document;
  session?: Session;
}) => {
  if (isLocation(document)) {
    return (
      <BasicRow
        doc={document}
        title={document.data.location.name}
        description={document.data.location.description}
      />
    );
  }

  if (isMonster(document)) {
    return <BasicRow doc={document} title={document.data.monster.name} />;
  }

  if (isNpc(document)) {
    return (
      <BasicRow
        doc={document}
        title={document.data.npc.name}
        description={document.data.npc.description}
      />
    );
  }

  if (isSession(document)) {
    return (
      <BasicRow
        doc={document}
        title={document.created}
        description={document.data.session.strongStart}
      />
    );
  }

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

  if (isScene(document)) {
    return <BasicRow doc={document} title={document.data.scene.text} />;
  }

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

  // Fallback: show ID and creation time
  return (
    <div>
      <div className="font-semibold text-lg text-slate-300">
        Unrecognized Document
      </div>
      <div className="text-slate-400 text-sm">ID: {document.id}</div>
      <div className="text-slate-400 text-sm">Created: {document.created}</div>
    </div>
  );
};
