// Tradutor view — wraps form, calls AI, shows result

function TranslatorView({ direction, onBack, initialResult, initialForm, initialTone, initialMode, onModeChange }) {
  const [mode, setMode] = React.useState(initialMode || 'career'); // 'career' | 'cultural' | tool id
  const [view, setView] = React.useState(initialResult ? 'result' : 'form');
  const [form, setForm] = React.useState(initialForm || null);
  const [result, setResult] = React.useState(initialResult || null);
  const [tone, setTone] = React.useState(initialTone || 'balanced');
  const [loading, setLoading] = React.useState(false);
  const [regen, setRegen] = React.useState(false);
  const [error, setError] = React.useState('');
  const [showToast, toastNode] = useToast();

  const dirA = direction === 'a';

  // Is current mode a generic AdM_TOOLS tool?
  const tool = window.AdM_TOOLS && window.AdM_TOOLS[mode];

  const translate = async (formData, opts = {}) => {
    setError('');
    if (window.AdM_TRACK) window.AdM_TRACK.track('tool_started', { tool: mode, regen: !!opts.regen });
    if (opts.regen) setRegen(true); else setLoading(true);
    try {
      const r = await window.runTool(mode, formData, tone);
      setResult(r);
      setForm(formData);
      setView('result');
    } catch (e) {
      if (e && e.code === 'no_credits') {
        if (window.AdM_openPaywall) window.AdM_openPaywall('out');
      } else {
        console.error(e);
        setError('Não foi possível gerar o texto. Suas respostas continuam aqui para você tentar de novo.');
      }
    } finally {
      setLoading(false); setRegen(false);
    }
  };

  const switchMode = (m) => {
    if (m === mode) return;
    setError('');
    setMode(m);
    setView('form');
    setResult(null);
    setForm(null);
    if (onModeChange) onModeChange(m);
  };

  React.useEffect(() => {
    if (initialMode && initialMode !== mode) {
      setMode(initialMode);
      setView(initialResult ? 'result' : 'form');
      setResult(initialResult || null);
      setForm(initialForm || null);
    }
  }, [initialMode]);

  const Header = window.SiteHeader;

  return (
    <>
      {Header && (
        <Header label="Ferramentas">
          <Brand direction="b" size="lg" />
          <div className="nav-links" style={{ marginLeft: 'auto' }}>
            {window.CreditsPill && <CreditsPill direction="a" onClick={() => window.AdM_openPaywall && window.AdM_openPaywall('add')} />}
            <a href="/" className="adm-nav-link" onClick={(e) => { e.preventDefault(); onBack(); }}>← Voltar ao início</a>
          </div>
        </Header>
      )}
    <div className="wrap" style={{ paddingBottom: 80 }}>

      {/* Tool picker */}
      <ToolPicker direction={direction} mode={mode} onChange={switchMode} disabled={loading || regen} />

      {view === 'form' && mode === 'career' && (
        <div style={{ paddingTop: 28, maxWidth: 760, margin: '0 auto' }}>
          <h1 className="career-form-title">Sua experiência no Brasil</h1>
          <p className="career-form-lead">Escolha um cargo que você já exerceu. Vamos usar esse relato para escrever os tópicos do currículo em inglês.</p>

          <div className="card">
            <TranslatorForm
              initialValues={form}
              direction={direction}
              isLoading={loading}
              onSubmit={(f) => translate(f)}
            />
          </div>

          <details className="career-options">
            <summary>Ajustar o tom da escrita</summary>
            <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', marginTop: 8 }}>
              {[
                ['confident', 'Confiante & ambicioso'],
                ['balanced', 'Equilibrado & profissional'],
                ['humble', 'Direto & humilde'],
              ].map(([k, label]) => (
                <button
                  key={k} type="button"
                  aria-pressed={tone === k}
                  onClick={() => setTone(k)}
                  style={{
                    padding: '8px 14px', borderRadius: 999, fontSize: 13, fontWeight: 500,
                    border: `1px solid ${tone === k ? (dirA ? 'var(--b-bg)' : 'var(--b-gold)') : (dirA ? 'var(--a-rule)' : 'var(--b-rule)')}`,
                    background: tone === k ? (dirA ? 'var(--b-bg)' : 'var(--b-gold)') : 'transparent',
                    color: tone === k ? (dirA ? 'var(--b-text)' : '#fff') : 'inherit',
                  }}
                >
                  {label}
                </button>
              ))}
            </div>
          </details>

          {error && (
            <div role="alert" style={{ marginTop: 16, padding: 14, background: 'rgba(220,38,38,0.08)', border: '1px solid rgba(220,38,38,0.2)', borderRadius: 8, color: '#dc2626', fontSize: 14 }}>
              {error}
            </div>
          )}
        </div>
      )}

      {view === 'form' && mode === 'cultural' && (
        <div style={{ paddingTop: 28, maxWidth: 760, margin: '0 auto' }}>
          <h1 style={{
            fontFamily: 'var(--font-display)', fontSize: 'clamp(36px, 5.5vw, 64px)',
            fontWeight: 550, letterSpacing: '-0.025em', margin: '20px 0 12px', lineHeight: 1.02,
          }}>
            Cultura de trabalho no exterior
          </h1>
          <p style={{ fontSize: 18, lineHeight: 1.5, color: dirA ? 'var(--a-text-2)' : 'var(--b-text-2)', margin: '0 0 36px', maxWidth: 600 }}>
            Diferenças culturais reais no trabalho, scripts pra reuniões, erros comuns no primeiro ano, e o que vai parecer estranho mas é normal.
          </p>

          <div className="card">
            <CulturalForm
              initialValues={form}
              direction={direction}
              isLoading={loading}
              onSubmit={(f) => translate(f)}
            />
          </div>

          {error && (
            <div role="alert" style={{ marginTop: 16, padding: 14, background: 'rgba(220,38,38,0.08)', border: '1px solid rgba(220,38,38,0.2)', borderRadius: 8, color: '#dc2626', fontSize: 14 }}>
              {error}
            </div>
          )}
        </div>
      )}

      {view === 'result' && result && mode === 'career' && (
        <div style={{ paddingTop: 32, maxWidth: 880, margin: '0 auto' }}>
          <ResultView
            direction={direction}
            result={result}
            form={form}
            tone={tone}
            onResultChange={setResult}
            onRegen={() => translate(form, { regen: true })}
            onBack={() => setView('form')}
            isRegen={regen}
            showToast={showToast}
          />
        </div>
      )}

      {view === 'result' && result && mode === 'cultural' && (
        <div style={{ paddingTop: 32, maxWidth: 880, margin: '0 auto' }}>
          <CulturalResult
            direction={direction}
            result={result}
            form={form}
            onBack={() => setView('form')}
            onRegen={() => translate(form, { regen: true })}
            isRegen={regen}
          />
        </div>
      )}

      {view === 'form' && tool && (
        <div style={{ paddingTop: 28, maxWidth: 760, margin: '0 auto' }}>
          <h1 style={{
            fontFamily: 'var(--font-display)', fontSize: 'clamp(32px, 4.8vw, 54px)',
            fontWeight: 550, letterSpacing: '-0.025em', margin: '20px 0 12px', lineHeight: 1.04,
          }}>
            {tool.title}
          </h1>
          <p style={{ fontSize: 18, lineHeight: 1.5, color: dirA ? 'var(--a-text-2)' : 'var(--b-text-2)', margin: '0 0 36px', maxWidth: 600 }}>
            {tool.subtitle}
          </p>
          <div className="card">
            <GenericToolForm
              key={mode}
              initialValues={form}
              direction={direction}
              tool={tool}
              isLoading={loading}
              onSubmit={(f) => translate(f)}
            />
          </div>
          {error && (
            <div role="alert" style={{ marginTop: 16, padding: 14, background: 'rgba(220,38,38,0.08)', border: '1px solid rgba(220,38,38,0.2)', borderRadius: 8, color: '#dc2626', fontSize: 14 }}>
              {error}
            </div>
          )}
        </div>
      )}

      {view === 'result' && result && tool && (
        <div style={{ paddingTop: 32, maxWidth: 880, margin: '0 auto' }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 24, gap: 16, flexWrap: 'wrap' }}>
            <div>
                  <h2 style={{ fontFamily: 'var(--font-display)', fontSize: 32, fontWeight: 500, margin: '10px 0 0', letterSpacing: '-0.02em' }}>
                {tool.resultTitle || tool.title}
              </h2>
            </div>
            <div style={{ display: 'flex', gap: 10 }}>
              <button onClick={() => setView('form')} style={{
                padding: '8px 14px', borderRadius: 8, fontSize: 13, fontWeight: 500,
                border: `1px solid ${dirA ? 'var(--a-rule)' : 'var(--b-rule)'}`, color: 'inherit',
              }}>← Editar</button>
              <button onClick={() => translate(form, { regen: true })} disabled={regen} style={{
                padding: '8px 14px', borderRadius: 8, fontSize: 13, fontWeight: 600,
                background: dirA ? 'var(--b-bg)' : 'var(--b-gold)',
                color: dirA ? 'var(--b-text)' : '#fff',
                opacity: regen ? 0.5 : 1,
              }}>{regen ? 'Gerando…' : 'Refazer · 1 crédito'}</button>
            </div>
          </div>
          <ToolResult direction={direction} render={tool.render} result={result} />
        </div>
      )}

      {view === 'result' && error && <p role="alert">{error}</p>}
      {toastNode}
    </div>
    </>
  );
}

window.TranslatorView = TranslatorView;
