// pricing-table.jsx — Pricing table primitives + Packages replacement
// Overrides window.Packages (loads after sections.jsx).
// No lucide-react, no cva, no @radix-ui — all inline.

// ─── Helpers ──────────────────────────────────────────────────────────────────
const cn = (...args) => args.filter(Boolean).join(' ');

// ─── Inline SVG icons ─────────────────────────────────────────────────────────
const IconCheck = () => (
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"
    strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"
    style={{ color: 'var(--success, #22c55e)', flexShrink: 0 }}>
    <path d="M20 6 9 17l-5-5"/>
  </svg>
);
const IconMinus = () => (
  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"
    strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"
    style={{ color: 'var(--muted)', flexShrink: 0 }}>
    <path d="M5 12h14"/>
  </svg>
);
const IconShield = ({ size = 12 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor"
    strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
  </svg>
);
const IconUsers = ({ size = 12 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor"
    strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/>
    <path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>
  </svg>
);
const IconRocket = ({ size = 12 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke="currentColor"
    strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
    <path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09z"/>
    <path d="m12 15-3-3a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.35 22.35 0 0 1-4 2z"/>
    <path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 0 5 0"/><path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5"/>
  </svg>
);

// ─── Badge ────────────────────────────────────────────────────────────────────
const Badge = ({ children, className = '', variant = 'secondary' }) => (
  <span className={cn('pricing-badge', `pricing-badge-${variant}`, className)}>
    {children}
  </span>
);

// ─── Button ───────────────────────────────────────────────────────────────────
const PricingButton = ({ children, variant = 'default', className = '', href, onClick, size = 'lg' }) => {
  const base = cn('pricing-btn', `pricing-btn-${variant}`, `pricing-btn-${size}`, className);
  return href
    ? <a href={href} className={base} onClick={onClick}>{children}</a>
    : <button className={base} onClick={onClick}>{children}</button>;
};

// ─── Primitives ───────────────────────────────────────────────────────────────
const PricingTable = ({ className, children }) => (
  <div style={{ position: 'relative', width: '100%', overflowX: 'auto' }}>
    <table className={cn('pricing-tbl', className)}>{children}</table>
  </div>
);

const PricingTableHeader = ({ children }) => <thead data-slot="thead">{children}</thead>;

const PricingTableBody = ({ className, children }) => (
  <tbody className={cn('pricing-tbody', className)}>{children}</tbody>
);

const PricingTableRow = ({ children, className }) => (
  <tr className={cn('pricing-tr', className)}>{children}</tr>
);

const PricingTableHead = ({ children, className }) => (
  <th className={cn('pricing-th', className)}>{children}</th>
);

const PricingTableCell = ({ children, className }) => (
  <td className={cn('pricing-td', className)}>
    {children === true  ? <IconCheck />  :
     children === false ? <IconMinus /> :
     children}
  </td>
);

const PricingTablePlan = ({
  name, badge, price, compareAt,
  icon: Icon, children, className, featured,
}) => (
  <div className={cn('pricing-plan', featured && 'pricing-plan-featured', className)}>
    <div className="pricing-plan-top">
      <div className="pricing-plan-icon-wrap">
        {Icon && <Icon size={12} />}
      </div>
      <span className="pricing-plan-name">{name}</span>
      <Badge variant="secondary" className="ml-auto">{badge}</Badge>
    </div>
    <div className="pricing-plan-price">
      <span className="pricing-plan-amount">{price}</span>
      {compareAt && <span className="pricing-plan-compare">{compareAt}</span>}
    </div>
    <div style={{ marginTop: '1rem' }}>{children}</div>
  </div>
);

// ─── Feature data (Jawwad Automation) ────────────────────────────────────────
const FEATURES = [
  { label: 'Automation workflows',        values: ['1',             '3–5',          'Unlimited'] },
  { label: 'Timeline',                    values: ['5–7 days',      '2–3 weeks',    '4–8 weeks'] },
  { label: 'Tool connections',            values: ['Up to 3',       'Up to 10',     'Unlimited'] },
  { label: 'AI integration',              values: [false,           true,           true] },
  { label: 'CRM / email / sheet sync',    values: [false,           true,           true] },
  { label: 'Custom API / webhooks',       values: [false,           false,          true] },
  { label: 'Backend logic / database',    values: [false,           false,          true] },
  { label: 'Internal dashboard / tool',   values: [false,           false,          true] },
  { label: 'Error handling + monitoring', values: [true,            true,           true] },
  { label: 'Documentation',               values: [true,            true,           true] },
  { label: 'Handover walkthrough',        values: ['1 session',     '2 sessions',   'Unlimited'] },
  { label: 'Revisions',                   values: ['1 round',       '2 rounds',     'Unlimited'] },
  { label: 'Support after handover',      values: ['7 days',        '30 days',      '90 days'] },
  { label: 'Workflow audit + mapping',    values: [false,           true,           true] },
  { label: 'Priority delivery',           values: [false,           false,          true] },
];

// ─── Packages — overrides the card version from sections.jsx ─────────────────
const Packages = () => {
  const scrollToContact = (size) => {
    const el = document.getElementById('contact');
    if (el) el.scrollIntoView({ behavior: 'smooth', block: 'start' });
    if (size) {
      setTimeout(() => {
        const radio = document.querySelector(`input[name="size"][value="${size}"]`);
        if (radio && !radio.checked) radio.click();
      }, 600);
    }
  };

  return (
    <section className="section packages">
      <Reveal><SectionLabel num="04" label="Engagement" /></Reveal>
      <Reveal delay={80}><h2 className="sec-h">Three ways to work together.</h2></Reveal>
      <Reveal delay={140}><p className="sec-sub">Pick the size of the bite — every engagement ends with a working system you own.</p></Reveal>

      <Reveal delay={200} y={24}>
        <PricingTable className="pricing-tbl-main">
          {/* ── Plan headers ── */}
          <PricingTableHeader>
            <PricingTableRow>
              <th style={{ minWidth: '200px' }} />

              <th style={{ padding: '4px', verticalAlign: 'top' }}>
                <PricingTablePlan
                  name="Starter" badge="Solo founders"
                  price="From $750" compareAt="$1,200"
                  icon={IconShield}
                >
                  <PricingButton variant="outline" href="#contact"
                    onClick={(e) => { e.preventDefault(); scrollToContact('starter'); }}>
                    Get started →
                  </PricingButton>
                </PricingTablePlan>
              </th>

              <th style={{ padding: '4px', verticalAlign: 'top' }}>
                <PricingTablePlan
                  name="Business" badge="Growing teams"
                  price="From $2,400" compareAt="$3,200"
                  icon={IconUsers} featured
                >
                  <PricingButton variant="accent" href="#contact"
                    onClick={(e) => { e.preventDefault(); scrollToContact('workflow'); }}>
                    Get started →
                  </PricingButton>
                </PricingTablePlan>
              </th>

              <th style={{ padding: '4px', verticalAlign: 'top' }}>
                <PricingTablePlan
                  name="Custom" badge="Advanced systems"
                  price="From $6,000"
                  icon={IconRocket}
                >
                  <PricingButton variant="outline" href="#contact"
                    onClick={(e) => { e.preventDefault(); scrollToContact('custom'); }}>
                    Get started →
                  </PricingButton>
                </PricingTablePlan>
              </th>
            </PricingTableRow>
          </PricingTableHeader>

          {/* ── Feature rows ── */}
          <PricingTableBody>
            {FEATURES.map((f, i) => (
              <PricingTableRow key={i}>
                <PricingTableHead>{f.label}</PricingTableHead>
                {f.values.map((v, j) => (
                  <PricingTableCell key={j}>{v}</PricingTableCell>
                ))}
              </PricingTableRow>
            ))}
          </PricingTableBody>
        </PricingTable>
      </Reveal>

      <style>{`
        /* ── Table layout ── */
        .pricing-tbl { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
        .pricing-tbl-main { margin-top: var(--space-xl); }

        /* ── Body rows ── */
        .pricing-tbody tr { border-bottom: 1px solid color-mix(in oklab, var(--border) 60%, transparent); }
        .pricing-tbody tr td:not(:first-child),
        .pricing-tbody tr th:not(:first-child) {
          border-left: 1px solid color-mix(in oklab, var(--border) 60%, transparent);
        }

        .pricing-th {
          padding: 12px 16px;
          text-align: left;
          font-weight: 500;
          color: var(--muted);
          white-space: nowrap;
          min-width: 200px;
        }
        .pricing-td {
          padding: 12px 16px;
          text-align: left;
          color: var(--text);
          white-space: nowrap;
          min-width: 160px;
        }
        .pricing-tbody tr:hover { background: color-mix(in oklab, var(--accent), transparent 96%); }

        /* ── Plan card ── */
        .pricing-plan {
          background: color-mix(in oklab, var(--card) 80%, transparent);
          border: 1px solid var(--border);
          border-radius: 10px;
          padding: 14px;
          font-weight: 400;
          position: relative;
          overflow: hidden;
          height: 100%;
          backdrop-filter: blur(12px);
          text-align: left;
        }
        .pricing-plan-featured {
          border-color: color-mix(in oklab, var(--accent), transparent 50%);
          background: color-mix(in oklab, var(--accent), var(--card) 6%);
          box-shadow: 0 0 0 1px color-mix(in oklab, var(--accent), transparent 60%),
                      0 8px 32px color-mix(in oklab, var(--accent), transparent 82%);
        }
        .pricing-plan-featured::after {
          content: '';
          position: absolute; inset: -1px; border-radius: inherit;
          background: linear-gradient(to bottom, color-mix(in oklab, var(--accent), transparent 80%), transparent);
          pointer-events: none; z-index: 0;
        }
        .pricing-plan-top {
          display: flex; align-items: center; gap: 8px; position: relative; z-index: 1;
        }
        .pricing-plan-icon-wrap {
          width: 26px; height: 26px; border-radius: 50%;
          border: 1px solid var(--border);
          display: flex; align-items: center; justify-content: center;
          flex-shrink: 0;
        }
        .pricing-plan-name {
          font-family: var(--font-mono, monospace);
          font-size: 0.78rem; color: var(--muted);
        }
        .pricing-plan-price {
          margin-top: 14px;
          display: flex; align-items: baseline; gap: 8px;
          position: relative; z-index: 1;
        }
        .pricing-plan-amount { font-size: 1.6rem; font-weight: 700; color: var(--text); }
        .pricing-plan-compare {
          font-size: 0.82rem; color: var(--muted);
          text-decoration: line-through;
        }

        /* ── Badge ── */
        .pricing-badge {
          display: inline-flex; align-items: center;
          border-radius: 999px; border: 1px solid var(--border);
          padding: 1px 8px; font-size: 0.68rem; font-weight: 500;
          white-space: nowrap;
        }
        .pricing-badge-secondary {
          background: color-mix(in oklab, var(--card) 60%, transparent);
          color: var(--muted);
        }

        /* ── Button ── */
        .pricing-btn {
          display: inline-flex; align-items: center; justify-content: center;
          width: 100%; border-radius: 8px; font-size: 0.82rem; font-weight: 500;
          cursor: pointer; text-decoration: none; transition: all 0.22s ease;
          white-space: nowrap;
        }
        .pricing-btn-lg { height: 40px; padding: 0 16px; }
        .pricing-btn-default {
          background: var(--accent); color: #fff; border: none;
        }
        .pricing-btn-default:hover { filter: brightness(1.1); }
        .pricing-btn-accent {
          background: var(--accent); color: #fff; border: 1px solid var(--accent);
          box-shadow: 0 4px 16px color-mix(in oklab, var(--accent), transparent 70%);
        }
        .pricing-btn-accent:hover { filter: brightness(1.12); }
        .pricing-btn-outline {
          background: transparent; color: var(--text);
          border: 1px solid color-mix(in oklab, var(--border) 80%, transparent);
        }
        .pricing-btn-outline:hover {
          background: color-mix(in oklab, var(--accent), transparent 92%);
          border-color: color-mix(in oklab, var(--accent), transparent 60%);
        }

        /* ── Responsive scroll ── */
        @media (max-width: 700px) {
          .pricing-th, .pricing-td { min-width: 130px; padding: 10px 12px; }
          .pricing-plan { min-width: 150px; }
        }
      `}</style>
    </section>
  );
};

// Export primitives + override Packages
window.PricingTable      = PricingTable;
window.PricingTableHeader = PricingTableHeader;
window.PricingTableBody  = PricingTableBody;
window.PricingTableRow   = PricingTableRow;
window.PricingTableHead  = PricingTableHead;
window.PricingTableCell  = PricingTableCell;
window.PricingTablePlan  = PricingTablePlan;
window.Packages          = Packages;   // overrides sections.jsx export
