/* global React, Icon, Avatar */
const { useState, useMemo, useEffect, useRef } = React;

// ── Seed data (empty — real data comes from the API) ─────────────────────────
const ME = { id: "", name: "", role: "" };

const SQUAD = [];

const SCOPES = [
  { id: "squad", label: "My Squad", icon: "Users", count: [0, 0] },
  { id: "team", label: "My Team", icon: "Team", count: [0, 0] },
  { id: "group", label: "My Group", icon: "Layers", count: [0, 0] },
  { id: "function", label: "My Function", icon: "Building", count: [0, 0] },
  { id: "business", label: "Business", icon: "Globe", meta: "Centrica" },
];

const GROUPS = [];

const INITIAL_UPDATES = [];

const GOALS = [];

const INITIAL_TODOS = [];

const INITIAL_WIKI = {
  knowledge: { tech: [], design: [], waysOfWorking: [] },
  decisions: [],
  experiments: [],
};

const PERSON_HISTORY = [];

const MY_HISTORY = [];

window.SEED = {
  ME,
  SQUAD,
  SCOPES,
  GROUPS,
  INITIAL_UPDATES,
  GOALS,
  INITIAL_TODOS,
  INITIAL_WIKI,
  PERSON_HISTORY,
  MY_HISTORY,
};

// ── Period (time-range) helpers ──────────────────────────────────────────────
// A "period" is a {type, offset}: type ∈ day|week|month|quarter; offset 0 = current,
// -1 = previous, etc. computePeriod() turns that into {start, end (exclusive),
// label, key} — start/end are ISO strings sent to the API; key is the stored label.

const PERIOD_TYPES = ["day", "week", "month", "quarter"];

function _startOfDay(d) {
  const x = new Date(d);
  x.setHours(0, 0, 0, 0);
  return x;
}

function computePeriod(type, offset, now = new Date()) {
  let start, end, label, key;
  const Y = now.getFullYear();

  if (type === "day") {
    start = _startOfDay(now);
    start.setDate(start.getDate() + offset);
    end = new Date(start);
    end.setDate(end.getDate() + 1);
    label =
      offset === 0
        ? "Today"
        : offset === -1
          ? "Yesterday"
          : start.toLocaleDateString("en-GB", {
              weekday: "short",
              day: "numeric",
              month: "short",
            });
    key = start.toISOString().slice(0, 10);
  } else if (type === "week") {
    // ISO-ish week starting Monday
    const base = _startOfDay(now);
    const dow = (base.getDay() + 6) % 7; // Mon=0 … Sun=6
    start = new Date(base);
    start.setDate(start.getDate() - dow + offset * 7);
    end = new Date(start);
    end.setDate(end.getDate() + 7);
    const endDisp = new Date(end);
    endDisp.setDate(endDisp.getDate() - 1);
    const fmt = (d) =>
      d.toLocaleDateString("en-GB", { day: "numeric", month: "short" });
    label =
      offset === 0
        ? "This week"
        : offset === -1
          ? "Last week"
          : `${fmt(start)} – ${fmt(endDisp)}`;
    // Stable key = ISO date of the week's Monday (unique per week)
    const wk = new Date(start.getTime() - start.getTimezoneOffset() * 60000);
    key = `W${wk.toISOString().slice(0, 10)}`;
  } else if (type === "month") {
    start = new Date(Y, now.getMonth() + offset, 1);
    end = new Date(Y, now.getMonth() + offset + 1, 1);
    label =
      offset === 0
        ? "This month"
        : offset === -1
          ? "Last month"
          : start.toLocaleDateString("en-GB", {
              month: "long",
              year: "numeric",
            });
    key = `${start.getFullYear()}-${String(start.getMonth() + 1).padStart(2, "0")}`;
  } else {
    // quarter
    const curQ = Math.floor(now.getMonth() / 3);
    const totalQ = Y * 4 + curQ + offset;
    const qy = Math.floor(totalQ / 4);
    const qi = ((totalQ % 4) + 4) % 4;
    start = new Date(qy, qi * 3, 1);
    end = new Date(qy, qi * 3 + 3, 1);
    label = offset === 0 ? "This quarter" : `Q${qi + 1} ${qy}`;
    key = `${qy}-Q${qi + 1}`;
  }

  // Exact label always available (used as subtitle / tooltip)
  const exact =
    type === "day"
      ? start.toLocaleDateString("en-GB", {
          weekday: "short",
          day: "numeric",
          month: "short",
        })
      : `${start.toLocaleDateString("en-GB", { day: "numeric", month: "short" })} – ` +
        `${new Date(end - 1).toLocaleDateString("en-GB", { day: "numeric", month: "short" })}`;

  return {
    type,
    offset,
    start: start.toISOString(),
    end: end.toISOString(),
    label,
    exact,
    key,
  };
}

window.PERIOD_TYPES = PERIOD_TYPES;
window.computePeriod = computePeriod;
