/* Feed — the hospital's own record of itself: notes written on records, and what
   people did. Commit model: INSTANT. A note posts on its own and a reaction is a
   bare control; there is no Save on this screen.

   Notes are written ON a record, never here, so this screen has no composer:
   every item opens the record it belongs to, where the composer lives. */
(() => {
  const { Button, Avatar } = window.OriginDesignSystem_7c998b;

  function FeedScreen({ data, now, viewer, onNavigate, onReactToNote, onExport }) {
    const owed = mentionsOwed(data.comments, viewer.id);
    const written = data.comments.slice().sort((a, b) => b.at - a.at);
    const today = written.filter((c) => new Date(c.at).toDateString() === new Date(now).toDateString());
    const authorsToday = today.reduce((list, c) => (list.indexOf(c.authorId) > -1 ? list : list.concat([c.authorId])), []);
    const items = feedItems(data.activity);

    return (
      <>
        <PageHeader
          path="/feed"
          title="Feed"
          sub={"Everything written and everything done at " + HD_HOSPITAL.short + ", newest first."}
          actions={<ExportMenu scope={"All " + items.length + " activity entries"} onExport={(format) => onExport("Activity", "Every activity entry", items.length, format)} />}
        />
        <Figures three items={[
          { label: "Notes written today", value: today.length, qualifier: authorsToday.length + (authorsToday.length === 1 ? " person has written something" : " people have written something") },
          { label: "Waiting on you", value: owed.length, qualifier: owed.length ? "mentions you have not written back on" : "every mention of you has had a reply" },
          { label: "Notes on the record", value: written.length, qualifier: "across " + written.reduce((list, c) => (list.indexOf(c.record.type + c.record.id) > -1 ? list : list.concat([c.record.type + c.record.id])), []).length + " records" },
        ]} />

        <Band title={"Waiting on you (" + owed.length + ")"} note="Notes that mention you and that you have not written back on.">
          {owed.length ? (
            <div className="hd-notes">
              {owed.map((comment) => (
                <div className="hd-noteitem" key={comment.id}>
                  <Note comment={comment} staff={data.staff} viewer={viewer} now={now} onReact={onReactToNote}
                    context={recordTitle(data, comment.record.type, comment.record.id)} />
                  <Button variant="outline" size="xs" onClick={() => onNavigate(recordRoute(data, comment.record))}>Open the record to reply</Button>
                </div>
              ))}
            </div>
          ) : <p className="hd-footnote">Nothing is waiting on you. Every note that named you has had a reply from you since.</p>}
        </Band>

        <Band title={"Notes on records (" + written.length + ")"} note="Written on a patient, an admission, a procedure, an order, a stock line or an import.">
          <div className="hd-notes">
            {written.map((comment) => (
              <div className="hd-noteitem" key={comment.id}>
                <Note comment={comment} staff={data.staff} viewer={viewer} now={now} onReact={onReactToNote}
                  context={recordTitle(data, comment.record.type, comment.record.id)} />
                <Button variant="ghost" size="xs" onClick={() => onNavigate(recordRoute(data, comment.record))}>Open the record</Button>
              </div>
            ))}
          </div>
        </Band>

        <Band title={"What the hospital did (" + items.length + ")"} note="One entry per commit: a bed requested, a consent recorded, a course verified, a file imported.">
          <div className="hd-feed">
            {items.map((item) => {
              const actor = staffById(data.staff, item.actorId);
              return (
                <article className="hd-feed__item" key={item.id}>
                  <span className="hd-feed__avatar">
                    <Avatar size={32} src={actor ? actor.avatar : undefined} alt={actor ? "Photograph of " + actor.name : ""} fallback={initialsOf(actor ? actor.name : "?")} />
                  </span>
                  <span className="hd-feed__line">
                    <strong style={{ fontWeight: "var(--font-weight-medium)" }}>{actor ? actor.name : "Somebody"}</strong> {item.verb} ·{" "}
                    <button type="button" className="hd-linkbtn" onClick={() => onNavigate(recordRoute(data, item.record))}>{recordTitle(data, item.record.type, item.record.id)}</button>
                  </span>
                  <span className="hd-feed__time" title={formatAbsolute(item.at)}>{formatWhen(item.at, now)}</span>
                  {item.note ? <span className="hd-feed__note">{item.note}</span> : null}
                </article>
              );
            })}
          </div>
        </Band>
      </>
    );
  }

  Object.assign(window, { FeedScreen });
})();
