/* global React, AppIcon, APPS_BY_ID */
// ─────────────────────────────────────────────────────────────
// FocusSheet — app-as-focus-surface shell.
//
// THESIS: every Mahana app is a focus surface for the Mastermind.
// Opening an app doesn't "switch context" — it binds the Mastermind's
// attention to that app's data, so subsequent chat turns answer WITH
// that context.  The sheet header literally reads
// "Answering with {app} context" to make the binding visible.
//
// Lifted from mastermind-v2/components/app-icons.jsx (UnifierPanel).
// Source of truth for the app catalog stays in app-icons.jsx
// (MAHANA_APPS / APPS_BY_ID / AppIcon).  This file factors out the
// reusable sheet shell + per-app body atoms so they can be used
// independently in: sidebar rail unifier, mobile bottom sheet,
// floating focus pane, inline inspector, …
// ─────────────────────────────────────────────────────────────

// (no useState used here; composition-only)

// ─────────────────────────────────────────────────────────────
// FocusSheet — the shell.  Wraps any app body with the FOCUS · X
// kicker + "Answering with X context" title + × button.
// Consumers compose children for body content.
// ─────────────────────────────────────────────────────────────
function FocusSheet({
  appId,                    // required; looks up label + icon from APPS_BY_ID
  onClose,                  // × click
  width = 360,              // desktop pane width; mobile bottom-sheet ignores
  variant = 'pane',         // 'pane' | 'sheet' — pane = side rail, sheet = bottom modal
  children,
}) {
  const app = APPS_BY_ID[appId];
  if (!app) return null;

  const isSheet = variant === 'sheet';

  return (
    <aside
      data-component="FocusSheet"
      data-app={appId}
      data-variant={variant}
      style={{
        width: isSheet ? '100%' : width,
        maxWidth: isSheet ? 'none' : width,
        flexShrink: 0,
        background: 'rgba(255,255,255,0.92)',
        backdropFilter: 'blur(20px)',
        WebkitBackdropFilter: 'blur(20px)',
        borderLeft: isSheet ? 'none' : '1px solid rgba(0,0,0,0.08)',
        borderTopLeftRadius: isSheet ? 20 : 0,
        borderTopRightRadius: isSheet ? 20 : 0,
        display: 'flex',
        flexDirection: 'column',
        overflow: 'hidden',
        animation: isSheet
          ? 'focussheet-in-bottom 320ms cubic-bezier(0.2,0.9,0.2,1) both'
          : 'focussheet-in-right 280ms cubic-bezier(0.2,0.9,0.2,1) both',
        boxShadow: isSheet ? '0 -12px 40px rgba(0,0,0,0.08)' : 'none',
      }}
    >
      <style>{`
        @keyframes focussheet-in-right {
          from { opacity: 0; transform: translateX(12px); }
          to   { opacity: 1; transform: translateX(0); }
        }
        @keyframes focussheet-in-bottom {
          from { opacity: 0; transform: translateY(24px); }
          to   { opacity: 1; transform: translateY(0); }
        }
      `}</style>
      {/* Drag handle (sheet only) */}
      {isSheet && (
        <div style={{
          width: 40, height: 4, borderRadius: 2,
          background: 'rgba(0,0,0,0.15)',
          margin: '10px auto 0',
        }} />
      )}

      {/* Header */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 14,
        padding: '16px 18px',
        borderBottom: '1px solid rgba(0,0,0,0.06)',
      }}>
        <AppIcon app={app} size={36} />
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{
            fontFamily: window.MM_FONT, fontSize: 10.5, fontWeight: 600,
            letterSpacing: '0.08em', textTransform: 'uppercase',
            color: 'rgba(0,0,0,0.45)', marginBottom: 2,
          }}>Focus · {app.label}</div>
          <div style={{
            fontFamily: window.MM_SERIF, fontWeight: 500, fontSize: 16,
            color: '#1A1A1A', letterSpacing: -0.2,
          }}>
            Answering with {app.label.toLowerCase()} context
          </div>
        </div>
        {onClose && (
          <button onClick={onClose} aria-label="Unbind context" style={{
            padding: '6px 10px', borderRadius: 7,
            background: 'rgba(0,0,0,0.04)', border: '1px solid rgba(0,0,0,0.08)',
            fontFamily: window.MM_FONT, fontSize: 12, color: 'rgba(0,0,0,0.6)',
            cursor: 'pointer',
          }}>✕</button>
        )}
      </div>

      {/* Body */}
      <div style={{ flex: 1, overflow: 'auto', padding: '18px 20px' }}>
        {children}
      </div>
    </aside>
  );
}

// ─────────────────────────────────────────────────────────────
// MastermindIntroLine — "Mastermind · I scanned 47 threads..."
// Appears at the top of any app body when Mastermind has already
// pre-processed the content.  Pink-accented "Mastermind" label + body.
// ─────────────────────────────────────────────────────────────
function MastermindIntroLine({ children }) {
  return (
    <div style={{
      fontFamily: window.MM_FONT, fontSize: 12, color: 'rgba(0,0,0,0.5)',
      marginBottom: 10,
    }}>
      <b style={{ color: 'rgba(255, 80, 180, 0.9)' }}>Mastermind</b>
      {' · '}{children}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// MastermindTag — "✦ 3 summarized by Mastermind" ribbon.
// Appears inline inside a row/card to attribute a small slice of
// content to Mastermind processing.
// ─────────────────────────────────────────────────────────────
function MastermindTag({ children, icon = '✦' }) {
  return (
    <div style={{
      fontFamily: window.MM_FONT, fontSize: 11,
      color: 'rgba(255, 80, 180, 0.85)', marginTop: 2,
    }}>
      {icon} {children}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// MailRow — sender avatar + subject + preview + time + optional tag
// ─────────────────────────────────────────────────────────────
function MailRow({ from, subj, preview, unread, time, tag, colorIndex = 0, isLast = false }) {
  const initials = from.split(' ').map(s => s[0]).join('').slice(0, 2);
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 12,
      padding: '10px 12px', borderRadius: 10,
      fontFamily: window.MM_FONT, fontSize: 13,
      borderBottom: isLast ? 'none' : '1px solid rgba(0,0,0,0.05)',
      background: unread ? 'rgba(255, 120, 200, 0.04)' : 'transparent',
    }}>
      <div style={{
        width: 30, height: 30, borderRadius: '50%',
        background: `hsl(${colorIndex * 80} 70% 65%)`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: '#fff', fontSize: 11.5, fontWeight: 600, flexShrink: 0,
      }}>{initials}</div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ display: 'flex', gap: 6, alignItems: 'baseline' }}>
          <span style={{ fontWeight: unread ? 600 : 500, color: 'rgba(0,0,0,0.85)' }}>{from}</span>
          <span style={{ flex: 1 }} />
          <span style={{ fontSize: 11, color: 'rgba(0,0,0,0.4)' }}>{time}</span>
        </div>
        <div style={{ color: 'rgba(0,0,0,0.75)', fontSize: 12.5, fontWeight: 500 }}>{subj}</div>
        <div style={{
          color: 'rgba(0,0,0,0.5)', fontSize: 12,
          textOverflow: 'ellipsis', whiteSpace: 'nowrap', overflow: 'hidden',
        }}>{preview}</div>
        {tag && <MastermindTag>{tag}</MastermindTag>}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// TaskRow — checkbox + label + optional project tag
// ─────────────────────────────────────────────────────────────
function TaskRow({ text, done, tag, onToggle }) {
  return (
    <div
      onClick={onToggle}
      style={{
        display: 'flex', alignItems: 'center', gap: 12,
        padding: '8px 8px', borderRadius: 10,
        fontFamily: window.MM_FONT, fontSize: 13,
        opacity: done ? 0.5 : 1,
        cursor: onToggle ? 'pointer' : 'default',
      }}
    >
      <div style={{
        width: 16, height: 16, borderRadius: 4,
        border: `1.5px solid ${done ? 'rgba(255, 80, 180, 0.6)' : 'rgba(0,0,0,0.2)'}`,
        background: done ? 'rgba(255, 80, 180, 0.8)' : 'transparent',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: '#fff', fontSize: 10, flexShrink: 0,
      }}>{done ? '✓' : ''}</div>
      <span style={{
        flex: 1,
        textDecoration: done ? 'line-through' : 'none',
        color: 'rgba(0,0,0,0.82)',
      }}>{text}</span>
      {tag && (
        <span style={{
          fontSize: 10.5, fontWeight: 500, padding: '2px 7px',
          borderRadius: 4, background: 'rgba(255, 80, 180, 0.1)',
          color: 'rgba(180, 40, 120, 0.9)',
        }}>{tag}</span>
      )}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// TaskSection — labelled group of TaskRows (TODAY, THIS WEEK)
// ─────────────────────────────────────────────────────────────
function TaskSection({ name, children }) {
  return (
    <div>
      <div style={{
        fontFamily: window.MM_FONT, fontSize: 11, fontWeight: 600,
        textTransform: 'uppercase', letterSpacing: 0.6,
        color: 'rgba(0,0,0,0.45)', marginBottom: 8,
      }}>{name}</div>
      {children}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// DocCard — title + source/duration + progress bar
// ─────────────────────────────────────────────────────────────
function DocCard({ title, source, pct = 0 }) {
  return (
    <div style={{
      padding: 14, borderRadius: 10,
      border: '1px solid rgba(0,0,0,0.08)',
      fontFamily: window.MM_FONT,
    }}>
      <div style={{
        fontWeight: 600, fontSize: 13, color: 'rgba(0,0,0,0.85)',
        marginBottom: 2,
      }}>{title}</div>
      <div style={{
        fontSize: 11.5, color: 'rgba(0,0,0,0.5)', marginBottom: 10,
      }}>{source}</div>
      <div style={{ height: 3, background: 'rgba(0,0,0,0.06)', borderRadius: 2 }}>
        <div style={{
          width: `${pct}%`, height: '100%',
          background: 'rgba(255, 80, 180, 0.7)', borderRadius: 2,
        }} />
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// IdeaCard — title + body + hashtag chips, soft-pink background
// ─────────────────────────────────────────────────────────────
function IdeaCard({ title, body, tags = [] }) {
  return (
    <div style={{
      padding: 14, borderRadius: 12,
      background: 'rgba(255, 240, 248, 0.7)',
      border: '1px solid rgba(255, 180, 220, 0.4)',
      fontFamily: window.MM_FONT,
    }}>
      <div style={{
        fontWeight: 600, fontSize: 13.5,
        color: 'rgba(0,0,0,0.85)', marginBottom: 4,
      }}>{title}</div>
      <div style={{
        fontSize: 12.5, color: 'rgba(0,0,0,0.6)', lineHeight: 1.45,
      }}>{body}</div>
      {tags.length > 0 && (
        <div style={{ display: 'flex', gap: 5, marginTop: 8, flexWrap: 'wrap' }}>
          {tags.map(tg => (
            <span key={tg} style={{
              fontSize: 10.5, padding: '2px 7px', borderRadius: 4,
              background: 'rgba(0,0,0,0.05)', color: 'rgba(0,0,0,0.55)',
            }}>#{tg}</span>
          ))}
        </div>
      )}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// SuggestionCard — the proactive Mastermind card on Apps home.
// "Fleet and Ledger are fighting over the same vendor row.
//  Ask the council to reconcile →"
// Not in v2 yet — design fresh here.
// ─────────────────────────────────────────────────────────────
function SuggestionCard({ kicker = 'Suggestion', body, cta, onAct }) {
  return (
    <button
      onClick={onAct}
      style={{
        display: 'block', width: '100%', textAlign: 'left',
        padding: '14px 16px', borderRadius: 12,
        background: 'rgba(255,255,255,0.6)',
        border: '1px solid rgba(0,0,0,0.06)',
        cursor: onAct ? 'pointer' : 'default',
        fontFamily: window.MM_FONT,
      }}
    >
      <div style={{
        fontSize: 10.5, fontWeight: 600,
        letterSpacing: '0.08em', textTransform: 'uppercase',
        color: 'rgba(0,0,0,0.4)', marginBottom: 4,
      }}>{kicker}</div>
      <div style={{
        fontSize: 13.5, lineHeight: 1.45,
        color: 'rgba(0,0,0,0.78)',
      }}>{body}{cta && (
        <span style={{ color: 'rgba(255, 80, 180, 0.9)', fontWeight: 500 }}>
          {' '}{cta} →
        </span>
      )}</div>
    </button>
  );
}

Object.assign(window, {
  FocusSheet,
  MastermindIntroLine,
  MastermindTag,
  MailRow,
  TaskRow,
  TaskSection,
  DocCard,
  IdeaCard,
  SuggestionCard,
});
