// ---------- Dashboard home ----------

const DashboardPage = ({ onNavigate, onOpenProject, onOpenMobileMenu }) => {
  return (
    <div>
      <Topbar
        eyebrow="GOOD MORNING, ALEX"
        title="Here's what's moving today."
        onOpenMobileMenu={onOpenMobileMenu}
        actions={
          <>
            <Button variant="ghost" size="md" icon={<window.Icon.Filter size={13} />}>This week</Button>
            <Button variant="primary" size="md" icon={<window.Icon.Plus size={13} />} onClick={() => window.dispatchEvent(new CustomEvent("hd-open-new-project"))}>New project</Button>
          </>
        }
      />

      <div style={{ padding: "24px 32px 60px", display: "flex", flexDirection: "column", gap: 24 }}>
        {/* Metric strip */}
        <div className="md-2col sm-1col" style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16 }}>
          {HD.metrics.map(m => (
            <StatCard key={m.label} {...m} iconName={m.icon} />
          ))}
        </div>

        {/* Main row */}
        <div className="md-stack" style={{ display: "grid", gridTemplateColumns: "1.6fr 1fr", gap: 24 }}>
          {/* Active projects */}
          <Card style={{ padding: 0 }}>
            <div style={{ padding: "18px 20px", borderBottom: "1px solid rgba(255,255,255,0.05)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <div>
                <div className="micro" style={{ color: "var(--warm-gray)", marginBottom: 4 }}>ACTIVE PROJECTS</div>
                <div style={{ fontSize: 15, fontWeight: 600, color: "var(--white)" }}>In motion</div>
              </div>
              <button onClick={() => onNavigate("projects")} style={{
                background: "transparent", border: "none", color: "var(--warm-gray)",
                fontSize: 12, cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 4,
                fontFamily: "var(--sans)",
              }}>View all <window.Icon.ChevronR size={11} /></button>
            </div>
            <div>
              {HD.projects.slice(0, 4).map((p, i) => (
                <ActiveProjectRow key={p.id} project={p} last={i === 3} onClick={() => onOpenProject(p.id)} />
              ))}
            </div>
          </Card>

          {/* Client review queue */}
          <Card style={{ padding: 0 }}>
            <div style={{ padding: "18px 20px", borderBottom: "1px solid rgba(255,255,255,0.05)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <div>
                <div className="micro" style={{ color: "var(--warm-gray)", marginBottom: 4 }}>CLIENT REVIEW QUEUE</div>
                <div style={{ fontSize: 15, fontWeight: 600, color: "var(--white)" }}>Waiting on Winterhill</div>
              </div>
              <StatusBadge tone="amber" label={`${HD.reviewQueue.filter(r => r.unread).length} unread`} size="sm" />
            </div>
            <div style={{ maxHeight: 460, overflow: "auto" }}>
              {HD.reviewQueue.map((r, i) => <ReviewQueueRow key={r.id} item={r} last={i === HD.reviewQueue.length - 1} />)}
            </div>
          </Card>
        </div>

        {/* Second row: pending approvals + upcoming deadlines */}
        <div className="md-stack-tight" style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 24 }}>
          <Card style={{ padding: 0 }}>
            <div style={{ padding: "18px 20px", borderBottom: "1px solid rgba(255,255,255,0.05)" }}>
              <div className="micro" style={{ color: "var(--warm-gray)", marginBottom: 4 }}>PENDING APPROVALS</div>
              <div style={{ fontSize: 15, fontWeight: 600, color: "var(--white)" }}>Awaiting client sign-off</div>
            </div>
            <div>
              {HD.pendingApprovals.map((a, i) => (
                <PendingApprovalRow key={a.id} item={a} last={i === HD.pendingApprovals.length - 1} />
              ))}
            </div>
          </Card>

          <Card style={{ padding: 0 }}>
            <div style={{ padding: "18px 20px", borderBottom: "1px solid rgba(255,255,255,0.05)" }}>
              <div className="micro" style={{ color: "var(--warm-gray)", marginBottom: 4 }}>UPCOMING DEADLINES</div>
              <div style={{ fontSize: 15, fontWeight: 600, color: "var(--white)" }}>Next 21 days</div>
            </div>
            <div>
              {HD.upcomingDeadlines.map((d, i) => (
                <DeadlineRow key={i} item={d} last={i === HD.upcomingDeadlines.length - 1} />
              ))}
            </div>
          </Card>
        </div>

        {/* Activity feed */}
        <Card style={{ padding: 0 }}>
          <div style={{ padding: "18px 20px", borderBottom: "1px solid rgba(255,255,255,0.05)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
            <div>
              <div className="micro" style={{ color: "var(--warm-gray)", marginBottom: 4 }}>RECENT ACTIVITY</div>
              <div style={{ fontSize: 15, fontWeight: 600, color: "var(--white)" }}>Across all projects</div>
            </div>
            <Button variant="ghost" size="sm">Open feed</Button>
          </div>
          <div style={{ padding: "8px 20px 16px" }}>
            <ActivityFeed items={HD.activity} />
          </div>
        </Card>
      </div>
    </div>
  );
};

const ActiveProjectRow = ({ project, last, onClick }) => {
  const tone = STATUS_TONES[project.accent];
  return (
    <div
      onClick={onClick}
      className="hd-row"
      style={{
        display: "grid", gridTemplateColumns: "44px minmax(0,1fr) auto",
        alignItems: "center", gap: 14,
        padding: "14px 20px",
        borderBottom: last ? "none" : "1px solid rgba(255,255,255,0.04)",
        cursor: "pointer", transition: "background 120ms ease",
      }}
    >
      <div style={{
        width: 44, height: 32, borderRadius: 6,
        background: `linear-gradient(135deg, var(--charcoal), ${tone.bg})`,
        border: `1px solid ${tone.bd}`,
        display: "flex", alignItems: "center", justifyContent: "center",
        color: tone.fg, flexShrink: 0,
      }}>
        <window.Icon.Film size={13} />
      </div>
      <div style={{ minWidth: 0 }}>
        <div style={{ fontSize: 13.5, fontWeight: 500, color: "var(--white)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", marginBottom: 4 }} title={project.name}>{project.name}</div>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <div style={{ fontSize: 11, color: "var(--warm-gray)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", flex: 1, minWidth: 0 }}>
            <span>{project.client}</span>
            <span style={{ margin: "0 6px", opacity: 0.5 }}>·</span>
            <span>Due {project.due}</span>
          </div>
          <div style={{ width: 80, display: "flex", alignItems: "center", gap: 6, flexShrink: 0 }}>
            <div style={{ flex: 1 }}>
              <Progress value={project.progress} tone={project.accent} height={3} />
            </div>
            <span className="mono" style={{ fontSize: 10, color: tone.fg, width: 26, textAlign: "right" }}>{project.progress}%</span>
          </div>
        </div>
      </div>
      <StatusBadge status={project.status} size="sm" />
    </div>
  );
};

const ReviewQueueRow = ({ item, last }) => (
  <div className="hd-row" style={{
    padding: "14px 20px 14px 24px",
    borderBottom: last ? "none" : "1px solid rgba(255,255,255,0.04)",
    cursor: "pointer", transition: "background 120ms ease",
    position: "relative",
  }}>
    {item.unread && <span style={{ position: "absolute", left: 10, top: 22, width: 5, height: 5, borderRadius: 99, background: "var(--harbor-2)" }} />}
    <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 8, marginBottom: 6 }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8, minWidth: 0, flex: 1 }}>
        <Avatar initials={item.from.split(" ").map(s => s[0]).slice(0,2).join("")} accent="harbor" size={20} />
        <span style={{ fontSize: 12.5, color: "var(--white)", fontWeight: 500, whiteSpace: "nowrap" }}>{item.from}</span>
        <span style={{ fontSize: 11, color: "var(--warm-gray)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", minWidth: 0 }}>· {item.project}</span>
      </div>
      <span style={{ fontSize: 11, color: "var(--warm-gray)", flexShrink: 0 }}>{item.time}</span>
    </div>
    <div style={{ fontSize: 12.5, color: "rgba(250,250,250,0.78)", lineHeight: 1.5 }}>{item.message}</div>
  </div>
);

const PendingApprovalRow = ({ item, last }) => {
  const tone = STATUS_TONES[item.accent];
  return (
    <div className="hd-row" style={{
      display: "flex", alignItems: "center", gap: 12,
      padding: "14px 20px",
      borderBottom: last ? "none" : "1px solid rgba(255,255,255,0.04)",
      cursor: "pointer",
    }}>
      <div style={{
        width: 36, height: 36, borderRadius: 8,
        background: tone.bg, border: `1px solid ${tone.bd}`,
        color: tone.fg,
        display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
      }}><window.Icon.Clock size={15} /></div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 13, fontWeight: 500, color: "var(--white)" }}>{item.item}</div>
        <div style={{ fontSize: 11.5, color: "var(--warm-gray)", marginTop: 2 }}>{item.project} · {item.waiting}</div>
      </div>
      <div style={{ textAlign: "right", flexShrink: 0 }}>
        <div className="mono" style={{ fontSize: 11.5, color: tone.fg }}>{item.elapsed}</div>
        <div style={{ fontSize: 10.5, color: "var(--warm-gray)", marginTop: 2 }}>elapsed</div>
      </div>
    </div>
  );
};

const DeadlineRow = ({ item, last }) => {
  const tone = STATUS_TONES[item.accent];
  return (
    <div className="hd-row" style={{
      display: "flex", alignItems: "center", gap: 14,
      padding: "14px 20px",
      borderBottom: last ? "none" : "1px solid rgba(255,255,255,0.04)",
      cursor: "pointer",
    }}>
      <div style={{
        width: 44, padding: "6px 0", borderRadius: 8,
        background: "rgba(255,255,255,0.025)",
        border: "1px solid rgba(255,255,255,0.06)",
        textAlign: "center", flexShrink: 0,
      }}>
        <div className="micro" style={{ color: "var(--warm-gray)", fontSize: 9, marginBottom: 2 }}>{item.date.split(" ")[0]}</div>
        <div className="serif" style={{ fontSize: 18, color: "var(--white)", letterSpacing: "-0.01em", lineHeight: 1 }}>{item.date.split(" ")[1]}</div>
      </div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 13, fontWeight: 500, color: "var(--white)" }}>{item.item}</div>
        <div style={{ fontSize: 11.5, color: "var(--warm-gray)", marginTop: 2 }}>{item.project}</div>
      </div>
      <div style={{ textAlign: "right", flexShrink: 0 }}>
        <span style={{
          fontSize: 11.5, color: tone.fg,
          background: tone.bg, padding: "3px 8px", borderRadius: 99,
          border: `1px solid ${tone.bd}`, fontWeight: 500,
          whiteSpace: "nowrap",
        }}>in {item.days}d</span>
      </div>
    </div>
  );
};

const ActivityFeed = ({ items }) => (
  <div style={{ position: "relative" }}>
    <div style={{
      position: "absolute", left: 19, top: 18, bottom: 18,
      width: 1, background: "rgba(255,255,255,0.05)",
    }} />
    {items.map((item, i) => {
      const tone = STATUS_TONES[item.accent];
      const IconComp = window.Icon[item.icon];
      return (
        <div key={item.id} style={{
          display: "flex", alignItems: "flex-start", gap: 14,
          padding: "12px 0", position: "relative",
        }}>
          <div style={{
            width: 28, height: 28, borderRadius: 99,
            background: tone.bg, border: `1px solid ${tone.bd}`,
            color: tone.fg,
            display: "flex", alignItems: "center", justifyContent: "center",
            flexShrink: 0, position: "relative", zIndex: 1,
            boxShadow: "0 0 0 4px var(--charcoal)",
          }}><IconComp size={12} /></div>
          <div style={{ flex: 1, paddingTop: 4 }}>
            <div style={{ fontSize: 13, color: "rgba(250,250,250,0.85)" }}>
              <span style={{ fontWeight: 500, color: "var(--white)" }}>{item.who}</span>
              {" "}
              <span style={{ color: "var(--warm-gray)" }}>{item.action}</span>
              {" "}
              <span style={{ color: "var(--white)" }}>{item.target}</span>
            </div>
            <div style={{ fontSize: 11, color: "var(--warm-gray)", marginTop: 2 }}>{item.at} · {item.role}</div>
          </div>
        </div>
      );
    })}
  </div>
);

window.DashboardPage = DashboardPage;
window.ActivityFeed = ActivityFeed;
