/* global React, TOKENS, FONTS, DarkCard, CircleCheck, Ring, Icon, hexA, IOSDevice, usePersona, personaStats */
// Eighty4 · iPhone product mockups, persona-driven (reads usePersona()).

function Phone({ children, scale = 1 }) {
  return (
    <div style={{ width: 402 * scale, height: 874 * scale, flexShrink: 0 }}>
      <div style={{ transform: `scale(${scale})`, transformOrigin: "top left", width: 402, height: 874 }}>
        <IOSDevice>{children}</IOSDevice>
      </div>
    </div>
  );
}

const monoLbl = (extra = {}) => ({ fontFamily: TOKENS.mono ? undefined : undefined, ...extra });
const ML = (extra = {}) => ({ fontFamily: FONTS.mono, fontSize: 11.5, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: TOKENS.textMuted, ...extra });

function ProfileBtn() {
  const T = TOKENS;
  return (
    <div style={{ width: 34, height: 34, borderRadius: 999, border: `1px solid ${T.hair}`, background: T.card, display: "flex", alignItems: "center", justifyContent: "center", boxShadow: T.shadowSm }}>
      <Icon name="person" size={16} color={T.textDim} />
    </div>
  );
}

// ───────────────────────── HOME ─────────────────────────
function PhoneHome() {
  const T = TOKENS;
  const p = usePersona();
  const st = personaStats(p);
  return (
    <div style={{ height: "100%", width: "100%", background: T.bg, color: T.ink, fontFamily: FONTS.sans, padding: "58px 22px 30px", boxSizing: "border-box", display: "flex", flexDirection: "column" }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
        <div style={{ display: "inline-flex", alignItems: "center", gap: 7 }}>
          <span style={ML()}>Fri, May 30</span>
          <span style={{ width: 3, height: 3, borderRadius: 2, background: T.textFaint }} />
          <span style={ML({ color: T.accent })}>Wk 3/12</span>
        </div>
        <ProfileBtn />
      </div>

      <div style={{ marginTop: 24 }}>
        <div style={{ fontSize: 31, fontWeight: 700, letterSpacing: -1.4, lineHeight: "35px" }}>Good morning.</div>
        <div style={{ fontSize: 14, color: T.textDim, letterSpacing: -0.2, marginTop: 4 }}>{p.cycle} · your focus today.</div>
      </div>

      <DarkCard pressable={false} style={{ marginTop: 22, padding: "22px 22px 20px" }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 16 }}>
          <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.7, textTransform: "uppercase", color: "rgba(255,255,255,0.85)" }}>Today's mission</span>
          <div style={{ flex: 1 }} />
          <Ring size={42} stroke={3.5} progress={st.taskDone / st.taskTotal} color="#fff"><span style={{ fontFamily: FONTS.mono, fontSize: 9.5, fontWeight: 600, color: "rgba(255,255,255,0.85)" }}>{st.taskDone}/{st.taskTotal}</span></Ring>
        </div>
        <div style={{ fontSize: 26, lineHeight: "32px", fontWeight: 600, color: "#fff", letterSpacing: -0.7 }}>{p.mission}</div>
        <div style={{ marginTop: 18, paddingTop: 16, borderTop: "1px solid rgba(255,255,255,0.1)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
          <div style={{ display: "inline-flex", alignItems: "center", gap: 9, background: T.accent, borderRadius: 999, padding: "8px 14px 8px 10px" }}>
            <CircleCheck checked={false} size={20} color="#fff" onClick={() => {}} />
            <span style={{ color: "#fff", fontSize: 13, fontWeight: 600 }}>Mark done</span>
          </div>
          <span style={{ fontFamily: FONTS.mono, fontSize: 11, color: "rgba(255,255,255,0.5)" }}>67d left</span>
        </div>
      </DarkCard>

      <div style={{ marginTop: 30 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 12, paddingBottom: 14, borderBottom: `1px solid ${T.hair}` }}>
          <span style={ML({ flexShrink: 0 })}>Next</span>
          <span style={{ fontFamily: FONTS.mono, fontSize: 13, color: T.ink, fontWeight: 600, flexShrink: 0 }}>{p.schedule[0].time}</span>
          <span style={{ width: 7, height: 7, borderRadius: 4, background: T.accent, flexShrink: 0 }} />
          <span style={{ flex: 1, fontSize: 14.5, fontWeight: 500, letterSpacing: -0.2, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{p.schedule[0].label}</span>
          <Icon name="chevR" size={14} color={T.textFaint} />
        </div>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", margin: "16px 0 2px" }}>
          <span style={ML()}>Habits</span>
          <span style={{ fontFamily: FONTS.mono, fontSize: 11, color: T.textMuted }}>{st.habitsTotal - st.habitsDone} left</span>
        </div>
        {p.habits.map((h, i) => (
          <div key={i} style={{ display: "flex", alignItems: "center", gap: 12, padding: "10px 0", borderBottom: i < p.habits.length - 1 ? `1px solid ${T.hair}` : "none" }}>
            <CircleCheck checked={h.done} size={20} color={T.accent} onClick={() => {}} />
            <span style={{ flex: 1, minWidth: 0, fontSize: 14.5, fontWeight: 500, letterSpacing: -0.2, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", textDecoration: h.done ? "line-through" : "none", textDecorationColor: T.textMuted, opacity: h.done ? 0.5 : 1 }}>{h.name}</span>
            <span style={{ display: "inline-flex", alignItems: "center", gap: 3, fontFamily: FONTS.mono, fontSize: 11, color: T.amber, fontWeight: 600 }}><Icon name="flame" size={11} color={T.amber} />{h.streak}</span>
          </div>
        ))}
      </div>

      <div style={{ flex: 1, minHeight: 14 }} />
      <div style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 3 }}>
        <span style={ML()}>Open the day</span>
        <Icon name="chevD" size={18} color={T.textMuted} />
      </div>
    </div>
  );
}

// task row
function TaskLine({ t, done, last }) {
  const T = TOKENS;
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 12, padding: "11px 0", borderBottom: last ? "none" : `1px solid ${T.hair}` }}>
      <CircleCheck checked={done} size={20} color={T.accent} onClick={() => {}} />
      <span style={{ flex: 1, minWidth: 0, fontSize: 14.5, fontWeight: 500, letterSpacing: -0.2, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", textDecoration: done ? "line-through" : "none", textDecorationColor: T.textMuted, opacity: done ? 0.5 : 1 }}>{t}</span>
    </div>
  );
}

// ───────────────────────── DAILY (the workbench — all three tiers) ─────────────────────────
function PhoneDaily() {
  const T = TOKENS;
  const p = usePersona();
  const st = personaStats(p);
  const scrub = (n) => (
    <div style={{ width: 32, height: 32, borderRadius: 999, border: `1px solid ${T.hair}`, background: T.card, display: "flex", alignItems: "center", justifyContent: "center", boxShadow: T.shadowSm, opacity: n === "chevR" ? 0.4 : 1 }}>
      <Icon name={n} size={14} color={T.textDim} />
    </div>
  );
  return (
    <div style={{ height: "100%", width: "100%", background: T.bg, color: T.ink, fontFamily: FONTS.sans, padding: "56px 22px 30px", boxSizing: "border-box", overflow: "hidden" }}>
      <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between" }}>
        <div>
          <span style={ML({ color: T.accent })}>Daily · Week 3</span>
          <div style={{ fontSize: 33, fontWeight: 700, letterSpacing: -1.6, lineHeight: "37px", marginTop: 5 }}>Today</div>
          <div style={{ fontSize: 13.5, color: T.textDim, marginTop: 2 }}>Fri, May 30</div>
        </div>
        <div style={{ display: "flex", gap: 8, paddingTop: 6 }}>{scrub("chevL")}{scrub("chevR")}</div>
      </div>

      {/* primary — the one thing */}
      <DarkCard pressable={false} style={{ marginTop: 18, padding: "20px 20px 18px" }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 12 }}>
          <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.7, textTransform: "uppercase", color: "rgba(255,255,255,0.85)" }}>The one thing</span>
          <div style={{ flex: 1 }} />
          <Ring size={36} stroke={3.5} progress={st.taskDone / st.taskTotal} color="#fff"><span style={{ fontFamily: FONTS.mono, fontSize: 9, fontWeight: 600, color: "rgba(255,255,255,0.85)" }}>{st.taskDone}/{st.taskTotal}</span></Ring>
        </div>
        <div style={{ fontSize: 23, lineHeight: "29px", fontWeight: 600, color: "#fff", letterSpacing: -0.6 }}>{p.mission}</div>
        <div style={{ marginTop: 16, display: "inline-flex", alignItems: "center", gap: 9, background: T.accent, borderRadius: 999, padding: "7px 14px 7px 9px" }}>
          <CircleCheck checked={false} size={18} color="#fff" onClick={() => {}} />
          <span style={{ color: "#fff", fontSize: 12.5, fontWeight: 600 }}>Mark done</span>
        </div>
      </DarkCard>

      {/* supporting (secondary) */}
      <div style={{ marginTop: 22 }}>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 2 }}>
          <span style={ML()}>Supporting</span>
          <span style={{ fontFamily: FONTS.mono, fontSize: 11, color: T.textMuted }}>{p.secondary.filter((x) => x.done).length}/{p.secondary.length}</span>
        </div>
        {p.secondary.map((x, i) => <TaskLine key={i} t={x.t} done={x.done} last={i === p.secondary.length - 1} />)}
      </div>

      {/* later (additional) */}
      <div style={{ marginTop: 18 }}>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 2 }}>
          <span style={ML()}>Later</span>
          <span style={{ fontFamily: FONTS.mono, fontSize: 11, color: T.textMuted }}>{p.additional.filter((x) => x.done).length}/{p.additional.length}</span>
        </div>
        {p.additional.map((x, i) => <TaskLine key={i} t={x.t} done={x.done} last={i === p.additional.length - 1} />)}
      </div>

      {/* schedule */}
      <div style={{ marginTop: 18 }}>
        <span style={ML()}>Schedule</span>
        <div style={{ marginTop: 8, display: "flex", flexDirection: "column", gap: 6 }}>
          {p.schedule.slice(0, 3).map((b, i) => (
            <div key={i} style={{ display: "flex", alignItems: "center", gap: 11, background: b.now ? T.accent : T.card, border: `1px solid ${b.now ? T.accent : T.hair}`, borderRadius: 11, padding: "9px 12px", boxShadow: b.now ? `0 4px 12px -3px ${hexA(T.accent, 0.5)}` : "none" }}>
              <span style={{ fontFamily: FONTS.mono, fontSize: 12, color: b.now ? "rgba(255,255,255,0.85)" : T.textMuted, width: 44, fontWeight: 600 }}>{b.time}</span>
              <span style={{ width: 3, height: 22, borderRadius: 99, background: b.now ? "#fff" : hexA(T.accent, 0.6) }} />
              <span style={{ flex: 1, fontSize: 13.5, fontWeight: 600, letterSpacing: -0.2, color: b.now ? "#fff" : T.ink, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{b.label}</span>
              {b.now && <span style={{ fontFamily: FONTS.mono, fontSize: 9.5, color: "#fff", fontWeight: 700, textTransform: "uppercase" }}>Now</span>}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { Phone, PhoneHome, PhoneDaily, ProfileBtn, TaskLine });
