/* global window React */
const { useState } = React;
const { I } = window;

// ─── Login page (reference-inspired) ────────────────────────────────────
function LoginPage({ onLogin }) {
  const [email, setEmail] = useState("registrant@demo.com");
  const [password, setPassword] = useState("••••••••••••");
  const [remember, setRemember] = useState(false);
  const [error, setError] = useState(null);
  const [loading, setLoading] = useState(false);

  const submit = (e) => {
    e?.preventDefault();
    setError(null);
    setLoading(true);
    setTimeout(() => {
      const e1 = email.trim().toLowerCase();
      if (e1 === "registrant@demo.com") {
        onLogin({ role: "registrant", name: "Maria Hartmann", company: "AgriDemo Holdings", initials: "MH", email: e1 });
      } else if (e1 === "regulator@demo.com") {
        onLogin({ role: "regulator", name: "James Carter", company: "USEPA · OPPTS", initials: "JC", email: e1 });
      } else {
        setError("Use registrant@demo.com or regulator@demo.com to sign in to the demo.");
      }
      setLoading(false);
    }, 350);
  };

  const fillRegistrant = () => { setEmail("registrant@demo.com"); setPassword("••••••••••••"); };
  const fillRegulator  = () => { setEmail("regulator@demo.com");  setPassword("••••••••••••"); };

  return (
    <div className="rac-login">
      <style>{`
        .rac-login {
          min-height: 100vh; width: 100%;
          position: relative; overflow: hidden;
          background: radial-gradient(ellipse at 30% 30%, #c2c6e8 0%, #9099d4 60%, #7a83c4 100%);
          display: flex; align-items: center;
          padding: 48px clamp(32px, 8vw, 120px);
        }
        .rac-login__hero-bg { position: absolute; inset: 0; z-index: 0; }
        .rac-login__hero-cap {
          position: absolute; bottom: 28px; right: 28px;
          background: rgba(11, 19, 48, 0.78); backdrop-filter: blur(8px);
          color: #fff; padding: 14px 18px; border-radius: 12px;
          max-width: 360px; font-size: 12.5px; line-height: 1.55;
          z-index: 2;
        }
        .rac-login__hero-cap b { font-family: var(--font-display); font-size: 13.5px; display: block; margin-bottom: 4px; }

        .rac-login__card {
          position: relative; z-index: 1;
          width: 100%; max-width: 440px;
          background: #fff;
          border-radius: 14px;
          box-shadow:
            0 1px 2px rgba(11, 19, 48, 0.08),
            0 24px 60px -12px rgba(11, 19, 48, 0.35),
            0 12px 32px -8px rgba(11, 19, 48, 0.18);
          overflow: hidden;
          display: flex; flex-direction: column;
        }
        .rac-login__body { padding: 36px 40px 28px; display: flex; flex-direction: column; }
        .rac-login__brand { display: flex; align-items: center; gap: 10px; justify-content: center; }
        .rac-login__brand-mark { height: 28px; width: 28px; border-radius: 7px;
          background: linear-gradient(135deg, var(--rac-primary) 0%, var(--rac-navy) 100%);
          display: grid; place-items: center; color: #fff; }
        .rac-login__brand-name { font-family: var(--font-display); font-weight: 700; font-size: 15px; letter-spacing: -0.01em; color: var(--rac-ink); }

        .rac-login__heading { margin-top: 22px; text-align: center; }
        .rac-login__heading h1 { font-size: 22px; font-weight: 600; line-height: 1.25; }
        .rac-login__heading p  { color: var(--rac-muted); font-size: 13px; margin-top: 6px; }

        .rac-login__form { margin-top: 22px; display: flex; flex-direction: column; gap: 12px; }
        .rac-login__demo-bar {
          margin-top: 8px;
          background: var(--rac-surface-alt);
          border: 1px dashed var(--rac-line-strong);
          border-radius: 10px;
          padding: 10px 12px;
          font-size: 12px; color: var(--rac-muted);
        }
        .rac-login__demo-bar b { color: var(--rac-ink-2); display: block; margin-bottom: 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; }
        .rac-login__demo-chip {
          font-family: var(--font-mono); font-size: 11.5px;
          background: #fff; border: 1px solid var(--rac-line); padding: 4px 8px; border-radius: 6px;
          margin-right: 6px; cursor: pointer; color: var(--rac-ink-2);
        }
        .rac-login__demo-chip:hover { border-color: var(--rac-primary); color: var(--rac-primary); }
        .rac-login__forgot { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--rac-muted); margin-top: -4px; }
        .rac-login__forgot a { font-weight: 500; }

        .rac-login__bottom-card {
          background: var(--rac-navy);
          color: #fff;
          padding: 16px 40px;
          display: flex; align-items: center; justify-content: space-between;
          gap: 16px;
        }
        .rac-login__bottom-card span { font-size: 13px; font-weight: 600; }
        .rac-login__bottom-card button {
          background: transparent; color: #fff; border: 1.5px solid rgba(255,255,255,0.7);
          padding: 7px 14px; border-radius: 6px; font-size: 11px; font-weight: 700; letter-spacing: 0.08em;
          cursor: pointer;
        }
        .rac-login__bottom-card button:hover { background: rgba(255,255,255,0.08); }

        .rac-login__error { font-size: 12.5px; color: var(--rac-fail); background: var(--rac-fail-bg); border: 1px solid #f1c1c8; padding: 10px 12px; border-radius: 8px; }

        @media (max-width: 560px) {
          .rac-login { padding: 24px 16px; justify-content: center; }
          .rac-login__body { padding: 28px 24px 20px; }
          .rac-login__bottom-card { padding: 14px 24px; }
        }
      `}</style>

      <div className="rac-login__hero-bg">
        <HeroIso />
      </div>

      <div className="rac-login__card">
        <div className="rac-login__body">
          <div className="rac-login__brand">
            <span className="rac-login__brand-mark">
              <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
                <path d="M4 18 L4 8 L12 4 L20 8 L20 18" />
                <path d="M9 14 L11.5 16.5 L16 12" />
              </svg>
            </span>
            <span className="rac-login__brand-name">USEPA RaC Platform</span>
          </div>

          <div className="rac-login__heading">
            <h1>Log in to your Rules-as-Code account</h1>
            <p>Submit field-trial data, run automated assessments against OPPTS 860.1500, and track regulatory review.</p>
          </div>

          <form className="rac-login__form" onSubmit={submit}>
            <div>
              <label className="rac-label">Email</label>
              <input className="rac-input" type="email" value={email} onChange={(e) => setEmail(e.target.value)} placeholder="you@example.com" />
            </div>
            <div>
              <label className="rac-label">Password</label>
              <input className="rac-input" type="password" value={password} onChange={(e) => setPassword(e.target.value)} placeholder="••••••••" />
            </div>
            {error && <div className="rac-login__error">{error}</div>}

            <button className="rac-btn rac-btn--primary rac-btn--lg" type="submit" disabled={loading} style={{width:"100%", marginTop:4}}>
              {loading ? "Signing in…" : "LOG IN"}
            </button>

            <div className="rac-login__forgot">
              <label className="rac-check">
                <input type="checkbox" checked={remember} onChange={(e) => setRemember(e.target.checked)} />
                Remember me
              </label>
              <a href="#">Forgot password?</a>
            </div>

            <div className="rac-login__demo-bar">
              <b>Demo accounts</b>
              <button type="button" className="rac-login__demo-chip" onClick={fillRegistrant}>registrant@demo.com</button>
              <button type="button" className="rac-login__demo-chip" onClick={fillRegulator}>regulator@demo.com</button>
              <span style={{display:"block", marginTop:6}}>Click an account to auto-fill, then LOG IN.</span>
            </div>
          </form>
        </div>

        <div className="rac-login__bottom-card">
          <span>New to the RaC Platform?</span>
          <button type="button">REQUEST ACCESS</button>
        </div>
      </div>

      <div className="rac-login__hero-cap">
        <b>Mock-up</b>
        A demonstration of the Rules-as-Code workflow for USEPA OPPTS 860.1500 (Crop Field Trials, Group 15). No data is persisted; outcomes are pre-computed for two demo scenarios.
      </div>
    </div>
  );
}

// ─── Isometric hero ─────────────────────────────────────────────────────
function HeroIso() {
  // Simple isometric grid of bars + spheres in primary palette
  return (
    <svg viewBox="0 0 600 700" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" style={{display:"block"}}>
      <defs>
        <linearGradient id="bar1a" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#1ed1a8"/><stop offset="1" stopColor="#0c8e6f"/></linearGradient>
        <linearGradient id="bar1b" x1="0" y1="0" x2="1" y2="0"><stop offset="0" stopColor="#0c8e6f"/><stop offset="1" stopColor="#066a52"/></linearGradient>
        <linearGradient id="bar2a" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor="#3a4dd6"/><stop offset="1" stopColor="#2233a8"/></linearGradient>
        <linearGradient id="bar2b" x1="0" y1="0" x2="1" y2="0"><stop offset="0" stopColor="#2233a8"/><stop offset="1" stopColor="#161e75"/></linearGradient>
        <radialGradient id="ball-mint" cx="0.35" cy="0.35"><stop offset="0" stopColor="#7eecd1"/><stop offset="1" stopColor="#1ed1a8"/></radialGradient>
        <radialGradient id="ball-blue" cx="0.35" cy="0.35"><stop offset="0" stopColor="#5e72e4"/><stop offset="1" stopColor="#1d2784"/></radialGradient>
      </defs>

      {/* Bars: faux-isometric - top diamond + two side parallelograms */}
      {/* helper: a bar starting at (x,y), width w, depth d, height h */}
      {makeBars().map((b, i) => (
        <Bar key={i} {...b} />
      ))}

      {/* Spheres */}
      <circle cx="200" cy="240" r="22" fill="url(#ball-blue)" />
      <circle cx="370" cy="170" r="16" fill="url(#ball-mint)" />
      <circle cx="500" cy="380" r="20" fill="url(#ball-mint)" />
      <circle cx="160" cy="500" r="18" fill="url(#ball-mint)" />
      <circle cx="430" cy="560" r="24" fill="url(#ball-blue)" />
    </svg>
  );
}
function Bar({ x, y, w, d, h, color }) {
  // top diamond (rhombus): (x,y) (x+w, y-w*0.5) (x+w+d, y+d*0.5-w*0.5) (x+d, y+d*0.5)
  const top = `${x},${y} ${x+w},${y - w*0.5} ${x+w+d},${y + d*0.5 - w*0.5} ${x+d},${y + d*0.5}`;
  const left = `${x},${y} ${x+d},${y + d*0.5} ${x+d},${y + d*0.5 + h} ${x},${y + h}`;
  const right = `${x+d},${y + d*0.5} ${x+w+d},${y + d*0.5 - w*0.5} ${x+w+d},${y + d*0.5 - w*0.5 + h} ${x+d},${y + d*0.5 + h}`;
  const a = color === "mint" ? "url(#bar1a)" : "url(#bar2a)";
  const b = color === "mint" ? "url(#bar1b)" : "url(#bar2b)";
  const top2 = color === "mint" ? "#3eddb5" : "#5263db";
  return (
    <g>
      <polygon points={left} fill={a} />
      <polygon points={right} fill={b} />
      <polygon points={top} fill={top2} />
    </g>
  );
}
function makeBars() {
  // Hand-placed scattered isometric bars
  return [
    { x: 120, y: 320, w: 200, d: 36, h: 36, color: "mint"  },
    { x:  80, y: 400, w: 240, d: 36, h: 36, color: "blue"  },
    { x: 280, y: 460, w: 220, d: 36, h: 36, color: "mint"  },
    { x: 360, y: 280, w: 180, d: 36, h: 36, color: "blue"  },
    { x: 220, y: 200, w: 220, d: 36, h: 36, color: "mint"  },
    { x: 420, y: 470, w: 180, d: 36, h: 36, color: "mint"  },
    { x:  60, y: 560, w: 200, d: 36, h: 36, color: "blue"  },
    { x: 320, y: 600, w: 200, d: 36, h: 36, color: "blue"  },
    { x: 440, y: 130, w: 140, d: 36, h: 36, color: "mint"  },
    { x: 100, y: 130, w: 160, d: 36, h: 36, color: "blue"  },
  ];
}

window.LoginPage = LoginPage;
