/* Mock data — stands in for the bydelaney_tasks + bydelaney_projects
   tables on Supabase. Lives in localStorage so claim/submit/approve flows
   survive a refresh. */

const STORAGE_KEY = "bydelaney.tasks.v1";

const SEED_TASKS = [
  {
    id: "T-014",
    title: "Instagram caption pack — Cypress Coffee, June",
    brief:
      "8 captions for their June feed. Tone: warm, a little funny, never aspirational. They sell drip + pastries + plants now. Focus on the plant pivot.",
    links: [
      { label: "Brand voice doc", url: "#" },
      { label: "Last month's feed", url: "#" },
    ],
    payout_usd: 15,
    status: "open",
    created_at: "2026-05-12T09:14:00Z",
    is_public: true,
  },
  {
    id: "T-013",
    title: "Rewrite About page — Reed & Co. Bookkeeping",
    brief:
      "Their current About reads like an LLC filing. Make it sound like Reed (a real person). 250 words max. He's a former English teacher who now does books for indie restaurants.",
    links: [{ label: "Current About", url: "#" }],
    payout_usd: 15,
    status: "claimed_delaney",
    created_at: "2026-05-11T16:32:00Z",
    claimed_at: "2026-05-12T08:02:00Z",
    is_public: true,
  },
  {
    id: "T-012",
    title: "Welcome email — Foxglove Florals subscribers",
    brief:
      "First email new subscribers get. They want it to feel like a handwritten note, not a coupon drop. No discount code on the first send.",
    links: [
      { label: "Sample emails they like", url: "#" },
      { label: "Mailerlite template", url: "#" },
    ],
    payout_usd: 15,
    status: "submitted",
    created_at: "2026-05-09T11:00:00Z",
    claimed_at: "2026-05-09T13:24:00Z",
    submitted_at: "2026-05-11T19:48:00Z",
    deliverable_url: "https://docs.google.com/document/d/foxglove-welcome",
    is_public: true,
  },
  {
    id: "T-011",
    title: "Landing page copy — Half Moon Bay surf rentals",
    brief:
      "One-page site. They rent boards + wetsuits to beginners. Audience is tourists who've never surfed. Confident but not cocky. Avoid 'stoke'.",
    links: [{ label: "Wireframe in Figma", url: "#" }],
    payout_usd: 15,
    status: "approved",
    created_at: "2026-05-05T10:00:00Z",
    claimed_at: "2026-05-05T14:11:00Z",
    submitted_at: "2026-05-07T17:30:00Z",
    approved_at: "2026-05-08T09:05:00Z",
    deliverable_url: "https://docs.google.com/document/d/hmb-surf-copy",
    is_public: true,
  },
  {
    id: "T-010",
    title: "Brand voice mini-guide — Tide & Pine candles",
    brief:
      "One page. 3 do's, 3 don'ts, 5 example sentences in the voice. They sell soy candles, women-owned, Mendocino.",
    links: [],
    payout_usd: 15,
    status: "paid",
    created_at: "2026-05-01T08:00:00Z",
    claimed_at: "2026-05-01T11:00:00Z",
    submitted_at: "2026-05-03T16:40:00Z",
    approved_at: "2026-05-04T07:55:00Z",
    paid_at: "2026-05-04T08:00:00Z",
    deliverable_url: "https://docs.google.com/document/d/tide-pine-voice",
    is_public: true,
  },
];

/* ── Portfolio items derived from completed (approved+paid) tasks
   Plus a couple older ones to give the grid texture. */
const PORTFOLIO_EXTRA = [
  {
    id: "P-09",
    title: "Website copy — Manzanita Mercantile",
    kind: "Website",
    summary:
      "Full-site rewrite for a general store on the Mendocino coast. 6 pages, voice guide, product blurbs.",
    when: "April 2026",
    tone: "warm, dry-witted, a little reverent",
  },
  {
    id: "P-08",
    title: "Email sequence — Salt Air Studio",
    kind: "Email",
    summary:
      "5-email welcome series for a yoga studio's new members. Reads like the teacher is texting you.",
    when: "March 2026",
    tone: "intimate, calm, never woo",
  },
];

/* ───────── store ───────── */
function loadTasks() {
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    if (!raw) return SEED_TASKS.slice();
    const parsed = JSON.parse(raw);
    if (!Array.isArray(parsed)) return SEED_TASKS.slice();
    return parsed;
  } catch {
    return SEED_TASKS.slice();
  }
}
function saveTasks(tasks) {
  try { localStorage.setItem(STORAGE_KEY, JSON.stringify(tasks)); } catch {}
}
function resetTasks() {
  try { localStorage.removeItem(STORAGE_KEY); } catch {}
}

/* ───────── helpers ───────── */
const STATUS_META = {
  open:            { label: "Open",          klass: "tag",                dot: "var(--ink-3)" },
  claimed_delaney: { label: "In progress",   klass: "tag tag--terracotta" },
  submitted:       { label: "Submitted",     klass: "tag tag--charcoal" },
  approved:        { label: "Approved",      klass: "tag tag--sage" },
  paid:            { label: "Paid",          klass: "tag tag--paid" },
};

function StatusPill({ status }) {
  const m = STATUS_META[status] || STATUS_META.open;
  return (
    <span className={m.klass}>
      <span className="dot" style={m.dot ? { background: m.dot } : undefined}></span>
      {m.label}
    </span>
  );
}

function fmtRelative(iso) {
  if (!iso) return "";
  const t = new Date(iso).getTime();
  const diff = Date.now() - t;
  const m = Math.floor(diff / 60000);
  if (m < 1) return "just now";
  if (m < 60) return m + " min ago";
  const h = Math.floor(m / 60);
  if (h < 24) return h + "h ago";
  const d = Math.floor(h / 24);
  if (d < 7) return d + "d ago";
  return new Date(iso).toLocaleDateString(undefined, { month: "short", day: "numeric" });
}

function fmtDate(iso) {
  if (!iso) return "";
  return new Date(iso).toLocaleDateString(undefined, { month: "short", day: "numeric", year: "numeric" });
}

Object.assign(window, {
  SEED_TASKS,
  PORTFOLIO_EXTRA,
  loadTasks,
  saveTasks,
  resetTasks,
  STATUS_META,
  StatusPill,
  fmtRelative,
  fmtDate,
});
