export interface DocumentStatistics {
  planDocumentCount: number;
  mainPlanCount: number;
  subPlanCount: number;
  attachmentCount: number;
  todayViewerCount: number;
}

type RecordValue = Record<string, unknown>;

function isRecord(value: unknown): value is RecordValue {
  return !!value && typeof value === "object" && !Array.isArray(value);
}

function hasExactKeys(value: RecordValue, keys: readonly string[]): boolean {
  const actual = Object.keys(value).sort();
  const expected = [...keys].sort();
  return (
    actual.length === expected.length &&
    actual.every((key, index) => key === expected[index])
  );
}

function isCount(value: unknown): value is number {
  return typeof value === "number" && Number.isInteger(value) && value >= 0;
}

export function isDocumentStatistics(
  value: unknown,
): value is DocumentStatistics {
  if (
    !isRecord(value) ||
    !hasExactKeys(value, [
      "planDocumentCount",
      "mainPlanCount",
      "subPlanCount",
      "attachmentCount",
      "todayViewerCount",
    ])
  ) {
    return false;
  }
  return (
    isCount(value.planDocumentCount) &&
    isCount(value.mainPlanCount) &&
    isCount(value.subPlanCount) &&
    isCount(value.attachmentCount) &&
    isCount(value.todayViewerCount)
  );
}

