/* eslint-disable */
// Staffing Partner — shared shell + small helpers

(function () {
const { Icon, Badge, Avatar } = window;
const { useState } = React;

// --- Agency identity (the "SP" logged in) ------------------------------
const AGENCY = {
  name: "Meridian Talent Partners",
  short: "Meridian",
  initials: "MT",
};

// --- TopNav -----------------------------------------------------------
function SPTopNav({ active, onNav, agencyName = AGENCY.name }) {
  const tabs = [
    { key: "jobs",       label: "Jobs",       icon: "briefcase" },
    { key: "candidates", label: "Candidates", icon: "users"     },
    { key: "workers",    label: "Workers",    icon: "user-check"},
  ];
  return (
    <div style={{
      display: "flex", alignItems: "center", gap: 32,
      padding: "0 28px", height: 64,
      borderBottom: "2px solid var(--neutral-100)",
      background: "var(--neutral-10)", fontFamily: "var(--font-body)",
    }}>
      <img src="../../assets/logos/logo-primary.svg" alt="HireArt" style={{ height: 28 }} />
      <div style={{
        fontSize: 11, letterSpacing: "0.12em", fontWeight: 700,
        textTransform: "uppercase", color: "var(--neutral-60)",
        borderLeft: "2px solid var(--neutral-30)", paddingLeft: 12, marginLeft: -12,
      }}>
        Staffing Partner Portal
      </div>
      <div style={{ display: "flex", gap: 4, flex: 1, marginLeft: 8 }}>
        {tabs.map(t => {
          const on = active === t.key;
          return (
            <button key={t.key} onClick={() => onNav?.(t.key)} style={{
              display: "inline-flex", alignItems: "center", gap: 6,
              background: on ? "var(--green-60)" : "transparent",
              border: on ? "2px solid var(--neutral-100)" : "2px solid transparent",
              borderRadius: 6, padding: "6px 14px",
              fontWeight: 600, fontSize: 14, cursor: "pointer",
              color: "var(--neutral-100)",
              boxShadow: on ? "var(--shadow-btn-sm)" : "none",
              fontFamily: "var(--font-body)",
            }}>
              <Icon name={t.icon} size={16} />
              {t.label}
            </button>
          );
        })}
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
        <div style={{ textAlign: "right", lineHeight: 1.1 }}>
          <div style={{ fontSize: 11, color: "var(--neutral-60)", fontWeight: 700, letterSpacing: "0.08em", textTransform: "uppercase" }}>Agency</div>
          <div style={{ fontSize: 13, fontWeight: 600 }}>{agencyName}</div>
        </div>
        <Avatar name={agencyName} size={36} bg="var(--purple-20)" />
      </div>
    </div>
  );
}

// --- Urgency helpers ---------------------------------------------------
function urgencyOf(daysLeft) {
  if (daysLeft <= 3)  return "critical";
  if (daysLeft <= 7)  return "high";
  if (daysLeft <= 14) return "med";
  return "low";
}
function urgencyLabel(u) {
  return { critical: "Critical", high: "Urgent", med: "This month", low: "Later" }[u];
}
function urgencyColor(u) {
  return { critical: "red", high: "yellow", med: "blue", low: "neutral" }[u];
}

// --- Recurring HCO badge (the "don't stop submitting" signal) ---------
function RecurringBadge({ recurring, size = "md" }) {
  if (!recurring) return null;
  const small = size === "sm";
  return (
    <span style={{
      display: "inline-flex", alignItems: "center", gap: 4,
      padding: small ? "1px 6px" : "2px 8px",
      border: "2px solid var(--neutral-100)",
      background: "var(--purple-40)",
      borderRadius: 100,
      fontSize: small ? 10 : 11,
      fontWeight: 700, letterSpacing: "0.04em",
      color: "var(--neutral-100)",
      fontFamily: "var(--font-body)", whiteSpace: "nowrap",
    }} title={`Recurring: ${recurring.rate} every ${recurring.period} through ${recurring.through}`}>
      <Icon name="repeat" size={small ? 10 : 12} />
      RECURRING
      {!small && <span style={{ opacity: 0.75, fontWeight: 500, marginLeft: 2 }}>
        {recurring.rate}/{recurring.period}
      </span>}
    </span>
  );
}

// --- Progress bar (HCO filled / total) --------------------------------
function HcoBar({ filled, total, height = 6 }) {
  const pct = total === 0 ? 0 : Math.min(100, (filled / total) * 100);
  const full = filled >= total;
  return (
    <div style={{
      width: "100%", height, borderRadius: 100,
      background: "var(--neutral-20)",
      border: "1.5px solid var(--neutral-100)",
      overflow: "hidden", position: "relative",
    }}>
      <div style={{
        height: "100%", width: `${pct}%`,
        background: full ? "var(--green-60)" : "var(--yellow-40)",
        borderRight: pct > 0 && pct < 100 ? "1.5px solid var(--neutral-100)" : "none",
      }} />
    </div>
  );
}

// --- StatusDot (Accepting / Paused / Closed) --------------------------
function StatusPill({ status }) {
  const map = {
    "Accepting": { color: "green",   icon: "check-circle" },
    "Paused":    { color: "yellow",  icon: "pause-circle" },
    "Closed":    { color: "neutral", icon: "x-circle"     },
  };
  const m = map[status] || map["Closed"];
  return <Badge color={m.color} dot>{status}</Badge>;
}

// --- Days-left chip ----------------------------------------------------
function DaysLeft({ days }) {
  const u = urgencyOf(days);
  const bg = { critical: "var(--red-20)", high: "var(--yellow-20)", med: "var(--blue-20)", low: "var(--neutral-20)" }[u];
  const fg = { critical: "var(--red-100)", high: "var(--yellow-100)", med: "var(--blue-100)", low: "var(--neutral-90)" }[u];
  return (
    <span style={{
      display: "inline-flex", alignItems: "center", gap: 4,
      fontFamily: "var(--font-body)", fontSize: 12, fontWeight: 700,
      padding: "2px 8px", borderRadius: 4,
      border: "2px solid var(--neutral-100)",
      background: bg, color: fg,
      fontVariantNumeric: "tabular-nums lining-nums",
    }}>
      <Icon name="clock" size={12} />
      {days === 1 ? "1 day left" : days < 0 ? `${Math.abs(days)}d overdue` : `${days} days left`}
    </span>
  );
}

// --- AgentChip ---------------------------------------------------------
function AgentChip({ agent, size = "sm" }) {
  if (!agent) return null;
  const small = size === "sm";
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
      <Avatar name={agent.name} size={small ? 22 : 28} bg={`var(--${agent.color}-20)`} />
      <span style={{ fontSize: small ? 12 : 13, color: "var(--neutral-90)", fontWeight: 500 }}>
        {agent.name}
      </span>
    </span>
  );
}

// --- SectionHeader -----------------------------------------------------
function SectionHeader({ eyebrow, title, sub, actions }) {
  return (
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", marginBottom: 18 }}>
      <div>
        {eyebrow && <div style={{
          fontSize: 11, fontWeight: 700, letterSpacing: "0.12em", textTransform: "uppercase",
          color: "var(--neutral-60)", marginBottom: 4,
        }}>{eyebrow}</div>}
        <h1 style={{ fontFamily: "var(--font-display)", fontSize: 40, fontWeight: 700, color: "var(--neutral-100)", margin: 0, letterSpacing: "0.01em", lineHeight: 1.05 }}>{title}</h1>
        {sub && <div style={{ fontSize: 14, color: "var(--neutral-70)", marginTop: 4 }}>{sub}</div>}
      </div>
      {actions && <div style={{ display: "flex", gap: 8 }}>{actions}</div>}
    </div>
  );
}

Object.assign(window, {
  AGENCY, SPTopNav, RecurringBadge, HcoBar, StatusPill, DaysLeft, AgentChip, SectionHeader,
  urgencyOf, urgencyLabel, urgencyColor,
});
})();
