// MyBoost: welcome tour for new members + updated How-To page (loaded after setup.jsx)
const welcomeKey = (id) => `myboost_welcome_${id}`;
const welcomeSeen = (id) => { try { return localStorage.getItem(welcomeKey(id)) === "1"; } catch (e) { return false; } };
const welcomeMark = (id) => { try { localStorage.setItem(welcomeKey(id), "1"); } catch (e) {} };

const GStep = ({ n, t, d, c }) => (
  <div style={{ display: "flex", gap: 14, marginBottom: 14, alignItems: "flex-start" }}>
    <div style={{ minWidth: 30, height: 30, borderRadius: "50%", background: c, display: "flex", alignItems: "center", justifyContent: "center", fontWeight: 800, fontSize: 13, color: "#fff" }}>{n}</div>
    <div><div style={{ fontWeight: 700, fontSize: 14, marginBottom: 3 }}>{t}</div><div style={{ color: "#a0a0c0", fontSize: 13, lineHeight: 1.6, whiteSpace: "pre-line" }}>{d}</div></div>
  </div>
);

const PHONE_IPHONE = "1. Open this site in Safari (not inside another app).\n2. Tap the Share button.\n3. Tap Add to Home Screen, then Add.";
const PHONE_ANDROID = "1. Open this site in Chrome.\n2. Tap the three dots (top right).\n3. Tap Install app (or Add to Home screen).";

function Welcome({ profile }) {
  const [open, setOpen] = useState(() => !!profile && profile.role !== "admin" && !welcomeSeen(profile.id));
  if (!open) return null;
  const close = () => { welcomeMark(profile.id); setOpen(false); };
  return <div style={S.ov} onClick={(e) => e.target === e.currentTarget && close()}>
    <div style={{ ...S.modal, maxHeight: "88vh", overflowY: "auto" }}>
      <h2 style={{ fontSize: 20, fontWeight: 800, marginBottom: 4 }}>👋 Welcome to MyBoost</h2>
      <div style={{ ...S.muted, marginBottom: 16 }}>Grow your Instagram, Facebook, YouTube and TikTok. Here is how it works.</div>
      <GStep n="1" c="#7c3aed" t="Put MyBoost on your phone" d={"iPhone:\n" + PHONE_IPHONE + "\n\nAndroid:\n" + PHONE_ANDROID} />
      <GStep n="2" c="#7c3aed" t="Save your account links" d={"At the top of the Store, open 🔗 My accounts and save your profile links once. Follower orders will fill them in for you."} />
      <GStep n="3" c="#7c3aed" t="Add to your balance" d="Pay the admin (Cash App, Venmo, Zelle or PayPal). They add it to your balance, shown at the top right." />
      <GStep n="4" c="#059669" t="Pick a package and order" d={"Tap Buy Now. For followers, use your profile link. For likes or comments, paste the link to that one post or video. Your account must be public."} />
      <GStep n="5" c="#059669" t="Track it in Orders" d="New orders start as Pending. Once the admin sends it, it shows Processing, then Completed. If something is not delivered, the unused amount goes back to your balance." />
      <div style={{ ...S.muted, marginBottom: 14 }}>You can read all of this again anytime on the 📖 How-To tab.</div>
      <button style={S.btn} onClick={close}>✅ Got it, take me to the store</button>
    </div>
  </div>;
}

function Guide() {
  const faq = [
    ["How do I put MyBoost on my phone?", "iPhone: open the site in Safari, tap Share, then Add to Home Screen. Android: open it in Chrome, tap the three dots, then Install app."],
    ["How long does delivery take?", "Most orders start within a few hours. Each package card shows its expected delivery time. Large follower orders are spread over several days on purpose so they look natural."],
    ["How do I add money to my balance?", "Pay the admin via Cash App, Venmo, Zelle or PayPal. Once received, they add it to your balance and you will see it update."],
    ["What is a refill?", "Some packages include a refill guarantee. If your count drops after delivery, message the admin with your order number and we will request a refill when the service covers it."],
    ["What if my order is cancelled or only partly delivered?", "The unused amount is refunded to your MyBoost balance automatically when the admin refreshes order statuses."],
    ["Will my account get banned?", "Instagram, Facebook, YouTube and TikTok rules don't allow bought engagement, so there is always some risk. Order small amounts, spread orders over time, and keep posting real content."],
    ["Can I order for someone else's account?", "Only for accounts you own or have permission to boost."],
    ["Why did my order get cancelled?", "Usually the account or post was private, or the link was wrong. Make it public, check the link, and order again."],
  ];
  return <>
    <h1 style={S.h1}>📖 How-To Guide</h1>
    <div style={{ ...S.card, borderLeft: "4px solid #a78bfa" }}>
      <div style={{ fontSize: 16, fontWeight: 800, color: "#a78bfa", marginBottom: 12 }}>📲 Put MyBoost on your phone</div>
      <GStep n="1" c="#7c3aed" t="iPhone" d={PHONE_IPHONE} />
      <GStep n="2" c="#7c3aed" t="Android" d={PHONE_ANDROID} />
      <div style={S.muted}>After this, MyBoost opens like an app from your home screen.</div>
    </div>
    <div style={{ ...S.card, borderLeft: "4px solid #7c3aed" }}>
      <div style={{ fontSize: 16, fontWeight: 800, color: "#a78bfa", marginBottom: 12 }}>🚀 Getting Started</div>
      <GStep n="1" c="#7c3aed" t="Create your account" d="Use 'Create account' on the sign-in screen with your email and a password. Confirm your email if asked." />
      <GStep n="2" c="#7c3aed" t="Save your account links" d="In the Store, open 🔗 My accounts and save your Instagram, Facebook, YouTube and TikTok links. Follower orders then fill them in for you." />
      <GStep n="3" c="#7c3aed" t="Get a balance" d="Pay the admin (Cash App, Venmo, Zelle, or PayPal). They add it to your balance (top right)." />
      <GStep n="4" c="#7c3aed" t="Browse the Store" d="Filter by platform or type, then tap Buy Now." />
    </div>
    <div style={{ ...S.card, borderLeft: "4px solid #10b981" }}>
      <div style={{ fontSize: 16, fontWeight: 800, color: "#10b981", marginBottom: 12 }}>🛒 Placing an Order</div>
      <GStep n="1" c="#059669" t="Pick a package" d="Tap Buy Now on the package you want." />
      <GStep n="2" c="#059669" t="Use the right link" d={"Followers / subscribers: your profile or channel link\n• instagram.com/yourhandle\n• facebook.com/yourpage\n• youtube.com/@yourchannel\n• tiktok.com/@yourhandle\n\nLikes / comments / shares: the link to that one post or video"} />
      <GStep n="3" c="#059669" t="Make sure the account is public" d="Private accounts can't receive orders and will be cancelled (and refunded)." />
      <GStep n="4" c="#059669" t="Confirm" d="The price comes off your balance and the order shows in 📦 Orders." />
    </div>
    <div style={{ ...S.card, borderLeft: "4px solid #3b82f6" }}>
      <div style={{ fontSize: 16, fontWeight: 800, color: "#3b82f6", marginBottom: 12 }}>📦 Order Statuses</div>
      {[["pending", "Received. Waiting for the admin to send it"], ["processing", "Sent. Delivery in progress"], ["completed", "Done. Check your account"], ["partial", "Only part delivered. The rest is refunded to your balance"], ["cancelled", "Not delivered. Refunded to your balance"]].map(([s, d]) => (
        <div key={s} style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 10 }}><span style={{ minWidth: 90 }}><Status s={s} /></span><span style={{ color: "#a0a0c0", fontSize: 13 }}>{d}</span></div>))}
    </div>
    <div style={{ ...S.card, borderLeft: "4px solid #f59e0b" }}>
      <div style={{ fontSize: 16, fontWeight: 800, color: "#f59e0b", marginBottom: 14 }}>❓ FAQ</div>
      {faq.map(([q, a]) => <div key={q} style={{ marginBottom: 14 }}><div style={{ fontWeight: 700, fontSize: 14, marginBottom: 4 }}>Q: {q}</div><div style={{ color: "#a0a0c0", fontSize: 13, lineHeight: 1.6 }}>A: {a}</div></div>)}
    </div>
  </>;
}
