/* global React, ReactDOM */
const { useState } = React;

function NavBar({ page }) {
  return (
    <nav className="nav">
      <a href="index.html" className="nav-brand">
        <div className="nav-brand-icon" />
        <span>Postai</span>
      </a>
      <div className="nav-links">
        <a href="index.html#effects" className="nav-link">Effects</a>
        <a href="index.html#features" className="nav-link">Features</a>
        <a href="index.html#pricing" className="nav-link">Pricing</a>
        <a href="help.html" className={"nav-link" + (page === 'help' ? ' active' : '')}>Help</a>
      </div>
      <a href="index.html#download" className="nav-cta">Get the App</a>
    </nav>
  );
}

function Footer() {
  return (
    <footer className="footer">
      <div className="container">
        <div className="footer-bottom">
          <span>© {new Date().getFullYear()} Postai · Kadir Aktaş</span>
          <span>
            <a href="terms.html" style={{marginRight: 24}}>Terms</a>
            <a href="privacy.html" style={{marginRight: 24}}>Privacy</a>
            <a href="subscription-policy.html">Subscriptions</a>
          </span>
        </div>
      </div>
    </footer>
  );
}

const CATS = [
  { icon: '◆', title: 'Getting started', desc: 'Install, sign up, your first creation', count: '8 articles' },
  { icon: '✦', title: 'Effects & generation', desc: 'How effects work, what to expect', count: '14 articles' },
  { icon: '◇', title: 'Coins & billing', desc: 'Pricing, refunds, payment issues', count: '6 articles' },
  { icon: '⬡', title: 'Account & privacy', desc: 'Manage your data and account', count: '5 articles' },
  { icon: '◈', title: 'Troubleshooting', desc: 'Crashes, errors, slow generations', count: '9 articles' },
  { icon: '✶', title: 'Sharing & export', desc: 'Save, post, and share creations', count: '4 articles' },
];

const POPULAR = [
  { q: 'My generation failed — do I get my coins back?', a: 'Yes. Failed generations refund the coins automatically within a few minutes. If you don\'t see them, contact us with your order ID.' },
  { q: 'Why does my creation look different from the preview?', a: 'AI generation has natural variation. The preview shows a representative result — yours will look similar in style but with your features.' },
  { q: 'How do I delete my account?', a: 'Open Profile → Settings → Delete Account. This permanently removes your photos, generations, and account data within 30 days.' },
  { q: 'Can I use Postai creations commercially?', a: 'Yes — you own the creations you generate. Check our Terms of Use for full details on what\'s permitted.' },
  { q: 'How long does a video take to generate?', a: 'Most videos are ready in 30–90 seconds. Complex effects with many frames may take up to 3 minutes during peak hours.' },
];

const SUPPORT_EMAIL = 'kadiraktastr@gmail.com';

const SEND_ENDPOINT = 'https://us-central1-pixora-3080a.cloudfunctions.net/sendContactEmail';

function ContactForm() {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');
  const [message, setMessage] = useState('');
  const [status, setStatus] = useState('idle');
  const [errorMsg, setErrorMsg] = useState('');
  const send = async (e) => {
    e.preventDefault();
    if (status === 'sending') return;
    setStatus('sending');
    setErrorMsg('');
    try {
      const r = await fetch(SEND_ENDPOINT, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ name, email, message }),
      });
      if (!r.ok) {
        const data = await r.json().catch(() => ({}));
        throw new Error(data.error || `Server error (${r.status})`);
      }
      setStatus('sent');
      setName(''); setEmail(''); setMessage('');
    } catch (err) {
      setStatus('error');
      setErrorMsg(err.message || 'Failed to send');
    }
  };
  const disabled = status === 'sending';
  return (
    <form className="contact-card" onSubmit={send}>
      <div className="contact-head">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
        <h3>Contact Us</h3>
      </div>
      <label className="contact-field">
        <span>Name</span>
        <input type="text" placeholder="Your name" value={name} onChange={(e) => setName(e.target.value)} required disabled={disabled} />
      </label>
      <label className="contact-field">
        <span>Email</span>
        <input type="email" placeholder="your@email.com" value={email} onChange={(e) => setEmail(e.target.value)} required disabled={disabled} />
      </label>
      <label className="contact-field">
        <span>Message</span>
        <textarea placeholder="Describe your issue or question..." rows={5} value={message} onChange={(e) => setMessage(e.target.value)} required disabled={disabled} />
      </label>
      <button type="submit" className="btn btn-primary contact-submit" disabled={disabled}>
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4"><line x1="22" y1="2" x2="11" y2="13"/><polygon points="22 2 15 22 11 13 2 9 22 2"/></svg>
        {status === 'sending' ? 'Sending…' : status === 'sent' ? 'Message sent ✓' : 'Send Message'}
      </button>
      {status === 'error' && (
        <p className="contact-direct" style={{ color: '#ff6b6b' }}>
          {errorMsg}
        </p>
      )}
      <p className="contact-direct">
        Or email us directly at <a href={`mailto:${SUPPORT_EMAIL}`}>{SUPPORT_EMAIL}</a>
      </p>
    </form>
  );
}

function QuickLinks() {
  return (
    <div className="contact-card">
      <div className="contact-head">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
        <h3>Quick Links</h3>
      </div>
      <a className="quick-link" href="privacy.html">
        <div className="quick-link-icon">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
        </div>
        <div className="quick-link-text">
          <strong>Postai Privacy Policy</strong>
          <span>How we handle your data</span>
        </div>
        <span className="quick-link-arrow">›</span>
      </a>
      <a className="quick-link" href="terms.html">
        <div className="quick-link-icon">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
        </div>
        <div className="quick-link-text">
          <strong>Postai Terms of Service</strong>
          <span>Terms and conditions</span>
        </div>
        <span className="quick-link-arrow">›</span>
      </a>
      <a className="quick-link" href="subscription-policy.html">
        <div className="quick-link-icon">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></svg>
        </div>
        <div className="quick-link-text">
          <strong>Subscription Policy</strong>
          <span>Billing and renewals</span>
        </div>
        <span className="quick-link-arrow">›</span>
      </a>
    </div>
  );
}

function App() {
  const [open, setOpen] = useState(0);
  return (
    <>
      <NavBar page="help" />
      <section className="help-hero">
        <div className="container">
          <span className="eyebrow">Support</span>
          <h1 className="h-section" style={{marginTop: 18}}>
            Help &amp; <span className="gold">Support</span>
          </h1>
          <p style={{color: 'var(--text-2)', fontSize: 18, marginTop: 18, maxWidth: 600, marginInline: 'auto'}}>
            Need help? Send us a message or check the common questions below.
          </p>
        </div>
      </section>

      <section className="section">
        <div className="container">
          <div className="help-grid">
            <ContactForm />
            <div>
              <QuickLinks />
              <div className="contact-card" style={{marginTop: 24}}>
                <div className="contact-head">
                  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
                  <h3>Common Questions</h3>
                </div>
                <div className="faq-list" style={{marginTop: 8}}>
                  {POPULAR.map((f, i) => (
                    <div key={i} className={"faq-item" + (open === i ? ' open' : '')}>
                      <button className="faq-q" onClick={() => setOpen(open === i ? -1 : i)}>
                        <span>{f.q}</span>
                        <span className="faq-q-icon">+</span>
                      </button>
                      <div className="faq-a"><div className="faq-a-inner">{f.a}</div></div>
                    </div>
                  ))}
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
      <Footer />
    </>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
