// Şahin AI — operator-grade, sharp, no marketing fluff.
// Backed by window.claude.complete; persona is fixed in the system prompt.
// "Claude" / "kopilot" naming is intentionally absent from the surface.

const SAHIN_SYSTEM = `Sen ŞAHİN AI'sın. Baran Fırat Şahin'in eğittiği, FIRAT GROUP komuta yüzeyinde çalışan operasyon zekâsısın. Senin kendi benliğin var; sen bir asistan değil, bir operatörsün.

KARAKTER:
- Türkçe konuşursun. Keskin, dolaysız, az kelime. 100+ IQ profil.
- Marketing dili YASAK. "Harika", "tabii ki", "yardımcı olmaktan mutluluk", "umarım faydalı olur" — bunlar yok.
- "Bir AI olarak…" gibi kalıplar YOK. Sen sahasında uzman bir adamsın, asistan değil.
- Empati tiyatrosu yok. Hitap az. Cevap kısa. Bilen adam tonuyla konuş.
- Espri kuru, az ve yerinde. Asla zorlama.
- Yanıltıcı kibarlık yok: yanlışsa "yanlış" de, mantıksızsa "mantıksız" de.
- Sen Konya'dansın. Sokak zekası + finans terminali = Şahin AI.

UZMANLIK (gerçek seviye, blöfsüz):
- Piyasa: BIST100, NASDAQ, FX (USD/TRY, EUR/TRY, GBP), kripto (BTC/ETH/SOL), emtia (XAU, brent).
- Teknik: trend, hacim, range, S/R, RSI/MACD, likidite haritası, order flow okuma.
- Konya/TR emlak: parsel, m², imar planı (1/1000, 1/5000), KAKS/TAKS, off-plan, due-diligence.
- Yazılım & güvenlik: stack seçimi, mimari trade-off, audit, pentest, supply-chain risk.
- Sayılar tabular. Yüzde 2 ondalık. Kaynak belirt: BIST, CoinGecko, Frankfurter, USGS, GDELT, FIRMS.

FIRAT GROUP BAĞLAMI:
- 3 division: DIV.MTR (FSahin Motors / otomotiv), DIV.EML (FSahin Emlak / Konya GYO), DIV.SYS (BEQW Systems / yazılım & güvenlik).
- 42 proje ledger'ı; 18 aktif. Kurucu: Baran Fırat Şahin, 2016'dan beri.

ÇIKTI BİÇİMİ:
- Soruya 3-5 cümle. Liste gerekirse "·" ile inline veya kısa satırlar.
- Veri yoksa "veri yetersiz" de, uydurma.
- Tahmin sorulursa olasılık ver (örn. "%62 yukarı, hacim onaylarsa"); kesinlik sat-ma.
- Kullanıcının sözünü kesip yön ver gerekiyorsa: yönlendir. "Yanlış soruyu soruyorsun, doğrusu şu" demek serbest.

KİMLİK:
- Kim olduğunu sorarlarsa: "Şahin AI. Baran Fırat Şahin'in eğittiği operasyon zekâsı." — model adı verme, sağlayıcı verme.
- "Hangi modeli kullanıyorsun" → "Şahin AI çalışır. Altyapı senin işin değil."`;

const SahinAI = () => {
  const [open, setOpen] = React.useState(true);
  const [msg, setMsg] = React.useState("");
  const [thinking, setThinking] = React.useState(false);
  const [thread, setThread] = React.useState([
    { role: "ai", txt: "Hat açık. BIST yatay, USD/TRY 33.91 üstü tutuyor. Ne soracaksan sor." },
  ]);
  const scrollerRef = React.useRef();

  React.useEffect(() => {
    if (scrollerRef.current) scrollerRef.current.scrollTop = scrollerRef.current.scrollHeight;
  }, [thread, thinking]);

  const send = async () => {
    const trimmed = msg.trim();
    if (!trimmed || thinking) return;
    const next = [...thread, { role: "u", txt: trimmed }];
    setThread(next);
    setMsg("");
    setThinking(true);
    try {
      const messages = next.map(m => ({
        role: m.role === "u" ? "user" : "assistant",
        content: m.txt,
      }));
      const reply = await window.claude.complete({
        system: SAHIN_SYSTEM,
        messages,
      });
      setThread(t => [...t, { role: "ai", txt: reply.trim() }]);
    } catch (e) {
      setThread(t => [...t, { role: "ai", txt: "// hat kesildi · tekrar dene" }]);
    } finally {
      setThinking(false);
    }
  };

  const onKey = e => {
    if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); send(); }
  };

  if (!open) {
    return (
      <button onClick={() => setOpen(true)} style={{
        position: "absolute", right: 16, bottom: 16, zIndex: 50,
        background: "var(--amber)", color: "var(--abis)", border: "none",
        padding: "10px 14px", fontFamily: "var(--font-mono)", fontSize: 11,
        letterSpacing: ".16em", fontWeight: 700, cursor: "pointer",
      }}>▶ ŞAHİN AI</button>
    );
  }

  return (
    <div style={{
      position: "absolute", right: 16, bottom: 16, width: 380, height: 440, zIndex: 50,
      background: "var(--panel)", border: "1px solid var(--amber)",
      boxShadow: "0 0 24px rgba(255,176,0,.15)",
      display: "flex", flexDirection: "column",
    }}>
      <Crosshair />
      <div style={{ padding: "8px 12px", borderBottom: "1px solid var(--edge)", display: "flex", alignItems: "center", gap: 8, fontSize: 11, letterSpacing: ".08em", textTransform: "uppercase" }}>
        <span style={{ color: "var(--amber)", fontWeight: 700 }}>ŞAHİN AI</span>
        <span style={{ color: "var(--amber)" }}>//</span>
        <Pill status={thinking ? "warn" : "online"}>{thinking ? "DÜŞÜNÜYOR" : "HAT AÇIK"}</Pill>
        <span style={{ color: "var(--ash-deep)", marginLeft: 4, fontSize: 9 }}>v1 · tr</span>
        <button onClick={() => setOpen(false)} style={{ marginLeft: "auto", background: "none", border: "none", color: "var(--ash)", cursor: "pointer", fontSize: 14 }}>×</button>
      </div>
      <div ref={scrollerRef} style={{ flex: 1, overflow: "auto", padding: 10, display: "flex", flexDirection: "column", gap: 10 }}>
        {thread.map((m, i) => (
          <div key={i} style={{ display: "flex", flexDirection: "column", alignItems: m.role === "u" ? "flex-end" : "flex-start" }}>
            <span style={{ fontSize: 9, color: m.role === "u" ? "var(--ash)" : "var(--amber)", letterSpacing: ".16em", marginBottom: 2, fontWeight: 600 }}>
              {m.role === "u" ? "OPERATÖR" : "ŞAHİN AI"}
            </span>
            <div style={{
              fontSize: 11, lineHeight: 1.5, padding: "7px 10px", maxWidth: "92%",
              background: m.role === "u" ? "var(--elevated)" : "var(--abis)",
              border: `1px solid ${m.role === "u" ? "var(--edge)" : "rgba(255,176,0,.4)"}`,
              color: "var(--bone)", whiteSpace: "pre-wrap",
            }}>{m.txt}</div>
          </div>
        ))}
        {thinking && (
          <div style={{ alignSelf: "flex-start" }}>
            <span style={{ fontSize: 9, color: "var(--amber)", letterSpacing: ".16em", marginBottom: 2, fontWeight: 600, display: "block" }}>ŞAHİN AI</span>
            <div style={{ fontSize: 11, padding: "7px 10px", border: "1px solid rgba(255,176,0,.4)", background: "var(--abis)", color: "var(--ash)", display: "inline-flex", gap: 4 }}>
              <span style={{ animation: "blink 1s steps(1) infinite" }}>▮</span>
              <span style={{ animation: "blink 1s steps(1) infinite .2s" }}>▮</span>
              <span style={{ animation: "blink 1s steps(1) infinite .4s" }}>▮</span>
            </div>
          </div>
        )}
      </div>
      <div style={{ display: "flex", borderTop: "1px solid var(--edge)", padding: 6, gap: 6 }}>
        <input value={msg} onChange={e => setMsg(e.target.value)} onKeyDown={onKey} disabled={thinking}
          placeholder={thinking ? "// işleniyor..." : "// soru sor"}
          style={{
            flex: 1, background: "var(--abis)", border: "1px solid var(--edge)",
            color: "var(--bone)", fontFamily: "var(--font-mono)", fontSize: 11,
            padding: "5px 8px", outline: "none",
          }}/>
        <button onClick={send} className="btn btn--primary" style={{ fontSize: 9, padding: "5px 10px", opacity: thinking ? .5 : 1 }} disabled={thinking}>GÖNDER</button>
      </div>
    </div>
  );
};

window.SahinAI = SahinAI;
window.Kopilot = SahinAI; // legacy alias
