/* cst-login.jsx — Login sheet UI (Google, Apple, GitHub).
   Matches the app's dark glass aesthetic. Exports to window.CST_LOGIN. */

const _LC = window.CST;

function LoginSheet({ open, onClose, intent }) {
  if (!open) return null;

  const providers = [
    { id: 'google', label: 'Continue with Google', icon: GoogleIcon },
    { id: 'apple', label: 'Continue with Apple', icon: AppleIcon },
    { id: 'github', label: 'Continue with GitHub', icon: GithubIcon },
  ];

  const handleLogin = async (provider) => {
    const { error } = await window.CST_AUTH.signInWith(provider);
    if (error) console.error('Login error:', error);
    // OAuth redirects away — no close needed
  };

  return (
    <div style={{
      position: 'fixed', inset: 0, zIndex: 300,
      display: 'flex', alignItems: 'flex-end', justifyContent: 'center',
      background: 'rgba(11,13,17,0.7)',
      WebkitBackdropFilter: 'blur(6px)', backdropFilter: 'blur(6px)',
      animation: 'cstFadeIn .2s',
    }} onClick={onClose}>
      <div style={{
        width: '100%', maxWidth: 480, margin: '0 16px 24px',
        background: 'linear-gradient(165deg, rgba(25,28,35,0.96) 0%, rgba(15,17,21,0.98) 100%)',
        border: `1px solid ${_LC.HAIR}`,
        borderRadius: 18, padding: '28px 24px 24px',
        animation: 'cstSheetUp .3s cubic-bezier(.22,.9,.3,1)',
      }} onClick={(e) => e.stopPropagation()}>
        {/* Header */}
        <div style={{ textAlign: 'center', marginBottom: 22 }}>
          <div style={{
            fontSize: 10, fontWeight: 600, letterSpacing: 2, textTransform: 'uppercase',
            color: _LC.HAZE_DIM, marginBottom: 8,
          }}>
            {intent === 'subscribe' ? 'UNLOCK UNLIMITED' : intent === 'report' ? 'GET YOUR REPORT' : 'SIGN IN'}
          </div>
          <div style={{ fontSize: 18, fontWeight: 700, color: _LC.TEXT, fontFamily: _LC.FONT }}>
            {intent === 'subscribe'
              ? 'Sign in to go unlimited'
              : intent === 'report'
              ? 'Sign in to purchase'
              : 'Sign in to CS Tech'}
          </div>
          <div style={{ fontSize: 12.5, color: _LC.HAZE, marginTop: 6, lineHeight: 1.4 }}>
            We never post on your behalf. Just need an account to save your decisions.
          </div>
        </div>

        {/* Buttons */}
        <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
          {providers.map(({ id, label, icon: Icon }) => (
            <button key={id} onClick={() => handleLogin(id)} style={{
              display: 'flex', alignItems: 'center', gap: 12,
              width: '100%', padding: '13px 16px',
              background: _LC.GLASS_HI, border: `1px solid ${_LC.HAIR}`,
              borderRadius: 12, color: _LC.TEXT, fontSize: 14, fontWeight: 600,
              fontFamily: _LC.FONT, cursor: 'pointer',
              transition: 'background .15s, border-color .15s',
            }}
            onMouseEnter={(e) => { e.currentTarget.style.background = 'rgba(236,237,241,0.12)'; e.currentTarget.style.borderColor = 'rgba(236,237,241,0.20)'; }}
            onMouseLeave={(e) => { e.currentTarget.style.background = _LC.GLASS_HI; e.currentTarget.style.borderColor = _LC.HAIR; }}
            >
              <Icon />
              <span>{label}</span>
            </button>
          ))}
        </div>

        {/* Footer */}
        <div style={{ textAlign: 'center', marginTop: 16, fontSize: 11, color: _LC.HAZE_DIM }}>
          By signing in you agree to our terms of service.
        </div>

        {/* Close */}
        <button onClick={onClose} style={{
          position: 'absolute', top: 12, right: 12,
          background: 'transparent', border: 0, color: _LC.HAZE,
          fontSize: 18, cursor: 'pointer', padding: 4,
        }}>✕</button>
      </div>
    </div>
  );
}

// SVG icons
function GoogleIcon() {
  return (
    <svg width="20" height="20" viewBox="0 0 24 24">
      <path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" fill="#4285F4"/>
      <path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/>
      <path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/>
      <path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/>
    </svg>
  );
}

function AppleIcon() {
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="#ECEDF1">
      <path d="M17.05 20.28c-.98.95-2.05.88-3.08.4-1.09-.5-2.08-.48-3.24 0-1.44.62-2.2.44-3.06-.4C2.79 15.25 3.51 7.59 9.05 7.31c1.35.07 2.29.74 3.08.8 1.18-.24 2.31-.93 3.57-.84 1.51.12 2.65.72 3.4 1.8-3.12 1.87-2.38 5.98.48 7.13-.57 1.5-1.31 2.99-2.54 4.09zM12.03 7.25c-.15-2.23 1.66-4.07 3.74-4.25.29 2.58-2.34 4.5-3.74 4.25z"/>
    </svg>
  );
}

function GithubIcon() {
  return (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="#ECEDF1">
      <path d="M12 2C6.477 2 2 6.477 2 12c0 4.42 2.865 8.17 6.839 9.49.5.092.682-.217.682-.482 0-.237-.008-.866-.013-1.7-2.782.604-3.369-1.34-3.369-1.34-.454-1.156-1.11-1.464-1.11-1.464-.908-.62.069-.608.069-.608 1.003.07 1.531 1.03 1.531 1.03.892 1.529 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.11-4.555-4.943 0-1.091.39-1.984 1.029-2.683-.103-.253-.446-1.27.098-2.647 0 0 .84-.269 2.75 1.025A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.294 2.747-1.025 2.747-1.025.546 1.377.203 2.394.1 2.647.64.699 1.028 1.592 1.028 2.683 0 3.842-2.339 4.687-4.566 4.935.359.309.678.919.678 1.852 0 1.336-.012 2.415-.012 2.743 0 .267.18.578.688.48C19.138 20.167 22 16.418 22 12c0-5.523-4.477-10-10-10z"/>
    </svg>
  );
}

window.CST_LOGIN = { LoginSheet };
