/* Company Stock Price — About, Terms, Privacy, Disclaimer */
const B = () => window.CSP_BRAND || { name: 'Company Stock Price', domain: 'companystockprice.com', url: 'https://companystockprice.com', tagline: '' };

const LEGAL_PAGES = {
  about: {
    title: 'About Company Stock Price',
    updated: 'June 2026',
    body: () => [
      { h: 'Welcome', p: `${B().name} (${B().domain}) helps Indian investors check company stock prices, benchmark indices, and market tools in one place. ${B().tagline}` },
      { h: 'What we offer', p: 'Live NSE and BSE equity prices, Nifty 50 and Sensex snapshots, a stock screener, index breakdowns, mutual fund reference data, IPO trackers, market news, and investment calculators — designed for quick, readable research.' },
      { h: 'Who we serve', p: 'Retail investors, students, and anyone following Indian markets who wants clear stock prices and context without switching between multiple terminals or apps.' },
      { h: 'Data & sources', p: 'Indian quotes and indices are aggregated from market data providers via our backend. Optional Finnhub integration adds global news and US benchmark data when you connect your own API key. Fund and IPO listings include curated reference information.' },
      { h: 'Our promise', p: 'We focus on clarity and accessibility — presenting stock prices and market information in a straightforward way so you can stay informed and make your own decisions.' },
    ],
  },
  terms: {
    title: 'Terms of Use',
    updated: 'June 2026',
    body: () => [
      { h: 'Agreement', p: `By visiting ${B().domain} or using ${B().name}, you agree to these Terms of Use. If you do not agree, please discontinue use of the site.` },
      { h: 'Permitted use', p: `${B().name} is for personal, non-commercial information and research only. You may not scrape, resell, mirror, or automate access in a way that overloads our servers or third-party data providers.` },
      { h: 'API keys', p: 'If you supply a third-party API key (e.g. Finnhub), you are responsible for securing it and complying with that provider\'s terms. Keys are stored in your browser unless configured for local development.' },
      { h: 'Changes', p: 'We may update features, data sources, or these terms at any time. Continued use after changes means you accept the revised terms.' },
      { h: 'Termination', p: `We may suspend or discontinue ${B().domain} at any time for maintenance, legal, or operational reasons, with or without notice.` },
    ],
  },
  privacy: {
    title: 'Privacy Policy',
    updated: 'June 2026',
    body: () => [
      { h: 'Overview', p: `${B().name} respects your privacy. This policy describes what is stored when you use ${B().domain}.` },
      { h: 'Local storage', p: 'Theme preference, live-data settings, and optional API keys are saved in your browser\'s localStorage on your device. We do not operate user accounts or collect passwords on this site.' },
      { h: 'What we do not do', p: 'We do not sell your personal data. We do not track you across other websites. Standard server logs may record request metadata for security and reliability when the site is hosted.' },
      { h: 'Third parties', p: 'Live data requests may be sent to Finnhub and other market data providers under their own privacy policies. Review those services before adding API keys.' },
      { h: 'Your control', p: 'Clear site data from your browser settings or remove localStorage entries for this domain to reset stored preferences.' },
      { h: 'Contact', p: `Privacy questions about ${B().domain} can be directed to the site operator listed on our About page.` },
    ],
  },
  disclaimer: {
    title: 'Disclaimer',
    updated: 'June 2026',
    body: () => [
      { h: 'Not advice', p: `${B().name} does not provide investment, trading, tax, or legal advice. All stock prices, news, screener results, IPO details, and calculator outputs are for general information only.` },
      { h: 'Accuracy', p: 'Prices and data may be delayed, estimated, or incomplete. Always verify figures with official NSE, BSE, AMC, or exchange sources before acting on them.' },
      { h: 'No brokerage', p: `${B().name} is not a broker, investment adviser, or portfolio manager. Buttons such as "Apply" on IPO cards are illustrative only and do not place real orders.` },
      { h: 'Risk', p: 'Investing in stocks and mutual funds involves risk, including loss of capital. Past performance does not guarantee future results. You are solely responsible for your decisions.' },
      { h: 'Liability', p: `To the fullest extent permitted by law, ${B().name} and its operators are not liable for losses arising from use of or reliance on information on ${B().domain}.` },
    ],
  },
};

function LegalScreen({ page, onBack }) {
  const doc = LEGAL_PAGES[page] || LEGAL_PAGES.about;
  const sections = typeof doc.body === 'function' ? doc.body() : doc.body;
  const brand = B();

  return (
    <div className="legal-page">
      <button className="pill" style={{ height: 36, marginBottom: 16 }} onClick={() => onBack()}>← Back</button>
      <div className="legal-brand-row">
        <div className="brand-mark" style={{ width: 40, height: 40 }}><span className="brand-mark-text">{brand.shortName || 'CSP'}</span></div>
        <div>
          <h1 className="h1" style={{ marginBottom: 4 }}>{doc.title}</h1>
          <p className="muted" style={{ margin: 0 }}>{brand.url} · Last updated {doc.updated}</p>
        </div>
      </div>

      <div className="legal-prose" style={{ marginTop: 24 }}>
        {sections.map((sec, i) => (
          <section key={i} className="legal-section">
            <h2 className="h3">{sec.h}</h2>
            <p>{sec.p}</p>
          </section>
        ))}
      </div>

      <div className="legal-foot muted">
        © {brand.copyrightYear || 2026} {brand.legalName || brand.name} ·{' '}
        <a href={brand.url} target="_blank" rel="noopener noreferrer" style={{ color: 'var(--accent)', fontWeight: 600 }}>{brand.domain}</a>
        {' · '}
        <button type="button" className="legal-inline-link" onClick={() => onBack('terms')}>Terms</button>
        {' · '}
        <button type="button" className="legal-inline-link" onClick={() => onBack('privacy')}>Privacy</button>
        {' · '}
        <button type="button" className="legal-inline-link" onClick={() => onBack('disclaimer')}>Disclaimer</button>
      </div>
    </div>
  );
}

Object.assign(window, { LegalScreen, LEGAL_PAGES });
