// legal.jsx — PsyFit legal documents (Privacy Policy, Terms, Health Disclaimer)
// Plain, app-accurate boilerplate for Google Play submission. PsyFit is a
// local-first app: all user data lives on the device, so the privacy story is
// simple and honest. Have these reviewed by a professional before publishing.

const LEGAL_META = {
  app: 'PsyFit',
  company: 'Psydek Interactive',
  contact: 'support@psydek.com',
  website: 'psydek.com',
  privacyUrl: 'https://psyfit.psydek.com/privacy',
  updated: 'June 9, 2026',
};

const LEGAL_DOCS = {
  // ── PRIVACY POLICY ──────────────────────────────────────────
  privacy: {
    kind: 'privacy',
    icon: 'shield',
    label: 'Privacy Policy',
    title: 'Privacy Policy',
    subtitle: 'How PsyFit handles your information.',
    intro: 'PsyFit is built to be private by design. Your workout data stays on your device — we don\u2019t run accounts, and we don\u2019t collect or sell your personal information.',
    sections: [
      { h: 'The short version', p: [
        'Everything you enter — your name, profile photo, workouts, sets, weights, and preferences — is stored locally on your device. It is not uploaded to our servers, because PsyFit does not have a user-account server.',
        'We do not sell, rent, or share your personal data with advertisers or data brokers.',
        'PsyFit offers an optional paid subscription, PsyFit Pro. Payments are processed by Google Play, not by us — we never see or store your payment-card details.',
      ] },
      { h: 'Information we process', p: [
        'Data you create in the app: profile details (name and optional photo), exercises, logged sets and weights, workouts, calendar entries, goals, and app settings. This is saved in your device\u2019s local app storage so the app works offline and remembers your progress.',
        'If you subscribe to PsyFit Pro, we store your subscription status locally on your device — for example, whether a free trial or paid plan is active and when it renews — so the app can unlock paid features offline. This status comes from Google Play; we do not store payment details.',
        'PsyFit does not request your location, contacts, microphone, or background activity. Photos you add are read only from the image you choose and are stored locally with your profile.',
      ] },
      { h: 'How your data is used', p: [
        'Your data is used solely to run the features you see: showing your stats, tracking streaks and personal records, and personalizing the app\u2019s appearance. Processing happens on your device.',
      ] },
      { h: 'Payments and subscriptions', p: [
        'PsyFit Pro is an optional auto-renewing subscription. All payments are handled by Google Play under Google\u2019s terms and privacy policy. PsyFit does not collect, see, or store your payment-card information.',
        'Google Play is the source of truth for your purchase, free trial, billing dates, renewals, and cancellations. You can review, manage, or cancel your subscription at any time in the Google Play Store under Subscriptions. The app keeps only a local record of whether your plan is active so paid features work offline.',
      ] },
      { h: 'Sharing and backups', p: [
        'You are in control of sharing. The Backup & Export feature creates a single file containing your data that you can save or move yourself. If you choose to store that file in a third-party cloud drive, that service\u2019s privacy policy applies to the copy you place there.',
        'We do not transmit your backup anywhere automatically.',
      ] },
      { h: 'Third-party services', p: [
        'The app loads fonts and code libraries from public content-delivery networks so it can render correctly. These requests are limited to delivering the app\u2019s resources and are not used by us to profile you. If you install PsyFit from Google Play, Google may collect standard install and crash diagnostics under its own policies.',
        'If you purchase a subscription, Google Play processes the transaction and handles billing, renewals, refunds, and cancellations under Google\u2019s policies. We receive only your resulting subscription status, never your payment details.',
      ] },
      { h: 'Data retention and deletion', p: [
        'Because your data lives on your device, you can remove it at any time: use Clear Workout History in Backup & Export to wipe logged data, or uninstall the app to remove everything associated with it.',
      ] },
      { h: 'Children\u2019s privacy', p: [
        'PsyFit is intended for users aged 16 and older and is not directed at children. We do not knowingly collect personal information from children.',
      ] },
      { h: 'Changes to this policy', p: [
        'We may update this policy as the app evolves. Material changes will be reflected here with a new \u201Clast updated\u201D date, and posted at our public policy URL.',
      ] },
    ],
    contact: true,
  },

  // ── TERMS OF SERVICE ────────────────────────────────────────
  terms: {
    kind: 'terms',
    icon: 'doc',
    label: 'Terms of Service',
    title: 'Terms of Service',
    subtitle: 'The agreement for using PsyFit.',
    intro: 'By downloading or using PsyFit, you agree to these terms. If you do not agree, please do not use the app.',
    sections: [
      { h: 'License to use', p: [
        'We grant you a personal, non-exclusive, non-transferable, revocable license to use PsyFit for your own non-commercial fitness tracking on devices you own or control.',
      ] },
      { h: 'Acceptable use', p: [
        'You agree not to misuse the app: no reverse-engineering or redistributing it for resale, no using it to break the law, and no interfering with its intended operation. You are responsible for the content you enter and for keeping your own backups.',
      ] },
      { h: 'Subscriptions and billing', p: [
        'PsyFit offers an optional auto-renewing subscription, PsyFit Pro, which unlocks premium features: building your own custom workouts, multi-week progressive training programs with auto-scheduling, and the full catalog of expert-designed Pro plans and templates. Logging, finishing, and saving your own workouts, tracking your stats, and exporting your data are always free and do not require a subscription. PsyFit Pro is $3.99 per month, billed immediately at the time of purchase.',
        'Subscriptions renew automatically each month at the then-current price until canceled. You can cancel anytime and turn off auto-renewal from the Subscriptions section of your Google Play account. Cancellation takes effect at the end of the current billing period, and you keep Pro access until then.',
        'All payments, renewals, refunds, and price-change notices are handled by Google Play under Google\u2019s Terms of Service. Refund eligibility is governed by Google Play\u2019s policies; ' + LEGAL_META.company + ' does not separately process payments or issue refunds. Prices may vary by region and may change, with notice as required by Google Play and applicable law.',
      ] },
      { h: 'Not medical advice', p: [
        'PsyFit provides general fitness tracking and informational content only. It is not a medical device and does not provide medical advice. See the Health & Safety Disclaimer for details, which forms part of these terms.',
      ] },
      { h: 'Your data', p: [
        'You own the data you create. PsyFit stores it on your device; you are responsible for exporting and safeguarding your own backups. We are not liable for data lost due to device failure, uninstalling, or clearing storage.',
      ] },
      { h: 'No warranty', p: [
        'The app is provided \u201Cas is\u201D and \u201Cas available,\u201D without warranties of any kind, express or implied, including fitness for a particular purpose. We do not warrant that the app will be uninterrupted, error-free, or that calculations are suitable for any specific goal.',
      ] },
      { h: 'Limitation of liability', p: [
        'To the maximum extent permitted by law, ' + LEGAL_META.company + ' will not be liable for any indirect, incidental, or consequential damages, or for any injury, loss, or data loss arising from your use of the app.',
      ] },
      { h: 'Changes and termination', p: [
        'We may update the app and these terms over time. Continued use after changes means you accept the updated terms. We may discontinue the app or features at any time.',
      ] },
      { h: 'Governing law', p: [
        'These terms are governed by the laws of the jurisdiction in which ' + LEGAL_META.company + ' operates, without regard to conflict-of-law rules.',
      ] },
    ],
    contact: true,
  },

  // ── HEALTH & SAFETY DISCLAIMER ──────────────────────────────
  health: {
    kind: 'health',
    icon: 'alert',
    label: 'Health & Safety Disclaimer',
    title: 'Health & Safety',
    subtitle: 'Please read before you train.',
    intro: 'PsyFit helps you plan and track exercise, but it cannot assess your individual health. Always train responsibly.',
    sections: [
      { h: 'Consult a professional', p: [
        'Consult a physician or qualified healthcare provider before beginning any exercise program, especially if you are pregnant, have a medical condition, an injury, or have been inactive. Do not use PsyFit as a substitute for professional medical advice, diagnosis, or treatment.',
      ] },
      { h: 'Assumption of risk', p: [
        'Physical exercise carries inherent risks, including injury. By using PsyFit, you acknowledge that you participate in any activity at your own risk and take full responsibility for your health and safety.',
      ] },
      { h: 'Listen to your body', p: [
        'Stop exercising immediately and seek medical attention if you feel pain, dizziness, shortness of breath, or any other concerning symptom. Use proper form and appropriate weights, and never push beyond your limits to match a number in the app.',
      ] },
      { h: 'Results vary', p: [
        'Fitness outcomes depend on many personal factors. PsyFit makes no guarantee of any specific result, and its suggestions are general in nature, not personalized prescriptions.',
      ] },
      { h: 'Emergencies', p: [
        'PsyFit is not an emergency service. If you think you are having a medical emergency, call your local emergency number immediately.',
      ] },
    ],
    contact: false,
  },
};

// Shared renderer for any legal document.
function LegalSheet({ kind, onClose }) {
  const c = useTheme();
  const doc = LEGAL_DOCS[kind];
  if (!doc) return null;
  return (
    <SheetShell title={doc.title} subtitle={doc.subtitle} onClose={onClose}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
        {/* meta + intro */}
        <div style={{ display: 'flex', flexDirection: 'column', gap: 12, padding: 16, borderRadius: 'var(--radius-sm)',
          background: 'var(--surface-2)', border: '1px solid var(--border)' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span style={{ color: c.accent, display: 'flex' }}><Icon name={doc.icon} size={20} /></span>
            <span style={{ fontFamily: 'var(--font-body)', fontSize: 11.5, fontWeight: 700, letterSpacing: 0.4, textTransform: 'uppercase', color: 'var(--muted)' }}>
              {LEGAL_META.app} · Last updated {LEGAL_META.updated}
            </span>
          </div>
          <div style={{ fontFamily: 'var(--font-body)', fontSize: 13.5, lineHeight: 1.55, color: 'var(--text)' }}>{doc.intro}</div>
        </div>

        {/* sections */}
        {doc.sections.map((s, i) => (
          <div key={i}>
            <div style={{ fontFamily: 'var(--font-body)', fontWeight: 800, fontSize: 15, color: 'var(--text)', marginBottom: 7 }}>{s.h}</div>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
              {s.p.map((para, j) => (
                <div key={j} style={{ fontFamily: 'var(--font-body)', fontSize: 13, lineHeight: 1.6, color: 'var(--muted)' }}>{para}</div>
              ))}
            </div>
          </div>
        ))}

        {/* contact */}
        {doc.contact && (
          <div style={{ display: 'flex', alignItems: 'flex-start', gap: 12, padding: 16, borderRadius: 'var(--radius-sm)',
            background: 'var(--surface-2)', border: '1px solid var(--border)' }}>
            <span style={{ color: c.accent, flexShrink: 0, marginTop: 1 }}><Icon name="heart" size={20} /></span>
            <div>
              <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 14, color: 'var(--text)', marginBottom: 3 }}>Questions?</div>
              <div style={{ fontFamily: 'var(--font-body)', fontSize: 13, lineHeight: 1.5, color: 'var(--muted)' }}>
                Contact us at <span style={{ color: c.accent, fontWeight: 700 }}>{LEGAL_META.contact}</span>.
              </div>
            </div>
          </div>
        )}

        <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, lineHeight: 1.5, color: 'var(--dim)', textAlign: 'center', padding: '0 6px' }}>
          This document is provided for general informational purposes and does not constitute legal advice.
        </div>
      </div>
    </SheetShell>
  );
}

const LEGAL_KINDS = ['privacy', 'terms', 'health'];

// ── CREDITS & LICENSES ──────────────────────────────────────
// Every visual asset that ships in PsyFit, with its license. Fonts are all
// SIL OFL (free commercial use); icons + the app mark are original; photos
// are user-supplied. Keep this in sync if any asset is ever added.
const CREDITS = {
  intro: 'Every visual asset in PsyFit is either created in-house or used under a license that allows use in a commercial app. Attribution is listed below.',
  groups: [
    {
      h: 'Typography',
      icon: 'doc',
      note: 'All typefaces are served from Google Fonts under the SIL Open Font License v1.1 — free to use, embed, and ship commercially.',
      items: [
        { name: 'Anton', by: 'Vernon Adams', license: 'SIL OFL 1.1' },
        { name: 'Bebas Neue', by: 'Dharma Type', license: 'SIL OFL 1.1' },
        { name: 'Oswald', by: 'Vernon Adams, Cyreal', license: 'SIL OFL 1.1' },
        { name: 'Barlow', by: 'Jeremy Tribby', license: 'SIL OFL 1.1' },
        { name: 'Saira', by: 'Omnibus-Type', license: 'SIL OFL 1.1' },
        { name: 'Manrope', by: 'Mikhail Sharanda', license: 'SIL OFL 1.1' },
      ],
    },
    {
      h: 'Icons & app mark',
      icon: 'bolt',
      note: 'The core line-icon set and the PsyFit lightning mark were drawn in-house as original vector artwork. Gym & fitness icons are from the free Hugeicons set (Stroke Rounded), which is free for commercial use; attribution appreciated.',
      items: [
        { name: 'PsyFit line icon set', by: LEGAL_META.company, license: 'Original work' },
        { name: 'App & launcher icon', by: LEGAL_META.company, license: 'Original work' },
        { name: 'Gym & fitness icons', by: 'Hugeicons · Halal Lab', license: 'Free (Stroke Rounded)' },
      ],
    },
    {
      h: 'Photography',
      icon: 'image',
      note: 'PsyFit ships with no stock photos. Any images you see — profile, home, and workout headers — are ones you add yourself from your own device. You own that content; it stays on your device.',
      items: [
        { name: 'Your photos', by: 'Provided by you', license: 'Your content' },
      ],
    },
    {
      h: 'Exercise demos',
      icon: 'image',
      note: 'The exercise demonstration images shown on each movement come from the open Free Exercise Database, released into the public domain under The Unlicense — free to use commercially with no attribution required. We credit it here gratefully all the same.',
      items: [
        { name: 'Free Exercise DB', by: 'yuhonas & contributors', license: 'Unlicense (public domain)',
          link: 'https://github.com/yuhonas/free-exercise-db', linkLabel: 'github.com/yuhonas/free-exercise-db' },
      ],
    },
    {
      h: 'Built with',
      icon: 'settings',
      note: 'PsyFit is built on open-source software used under permissive licenses (MIT and similar).',
      items: [
        { name: 'React', by: 'Meta', license: 'MIT' },
      ],
    },
  ],
};

function CreditsSheet({ onClose }) {
  const c = useTheme();
  return (
    <SheetShell title="Credits & Licenses" subtitle="Where the artwork comes from." onClose={onClose}>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
        {/* intro / open-use banner */}
        <div style={{ display: 'flex', flexDirection: 'column', gap: 12, padding: 16, borderRadius: 'var(--radius-sm)',
          background: `linear-gradient(135deg, ${c.accent}1f, transparent 70%), var(--surface-2)`, border: `1px solid ${c.accent}44` }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span style={{ color: c.accent, display: 'flex' }}><Icon name="check" size={20} /></span>
            <span style={{ fontFamily: 'var(--font-body)', fontSize: 11.5, fontWeight: 700, letterSpacing: 0.4, textTransform: 'uppercase', color: c.accent }}>
              All assets cleared for free use
            </span>
          </div>
          <div style={{ fontFamily: 'var(--font-body)', fontSize: 13.5, lineHeight: 1.55, color: 'var(--text)' }}>{CREDITS.intro}</div>
        </div>

        {/* groups */}
        {CREDITS.groups.map((g, gi) => (
          <div key={gi}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 9, marginBottom: 10 }}>
              <span style={{ color: c.accent, display: 'flex' }}><Icon name={g.icon} size={18} /></span>
              <span style={{ fontFamily: 'var(--font-display)', fontSize: 17, letterSpacing: 0.3, textTransform: 'uppercase', color: 'var(--text)' }}>{g.h}</span>
            </div>
            <Card pad={0}>
              {g.items.map((it, i) => (
                <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '13px 15px', borderTop: i ? '1px solid var(--border)' : 'none' }}>
                  <div style={{ flex: 1, minWidth: 0 }}>
                    <div style={{ fontFamily: 'var(--font-body)', fontWeight: 700, fontSize: 14.5, color: 'var(--text)' }}>{it.name}</div>
                    <div style={{ fontFamily: 'var(--font-body)', fontSize: 12, color: 'var(--muted)', marginTop: 1 }}>{it.by}</div>
                    {it.link && (
                      <a href={it.link} target="_blank" rel="noopener noreferrer" style={{ display: 'inline-flex', alignItems: 'center', gap: 4, marginTop: 4,
                        fontFamily: 'var(--font-body)', fontSize: 11.5, fontWeight: 700, color: c.accent, textDecoration: 'none', wordBreak: 'break-all' }}>
                        <Icon name="external" size={12} />{it.linkLabel || it.link}
                      </a>
                    )}
                  </div>
                  <span style={{ flexShrink: 0, fontFamily: 'var(--font-body)', fontSize: 10.5, fontWeight: 700, letterSpacing: 0.3,
                    color: c.accent, background: 'var(--surface-2)', border: `1px solid ${c.accent}55`, borderRadius: 999, padding: '4px 10px', whiteSpace: 'nowrap' }}>{it.license}</span>
                </div>
              ))}
            </Card>
            <div style={{ fontFamily: 'var(--font-body)', fontSize: 12, lineHeight: 1.5, color: 'var(--muted)', marginTop: 8, padding: '0 2px' }}>{g.note}</div>
          </div>
        ))}

        <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, lineHeight: 1.5, color: 'var(--dim)', textAlign: 'center', padding: '0 6px' }}>
          The SIL Open Font License and MIT License texts are available from their respective authors. Questions? Contact {LEGAL_META.contact}.
        </div>
      </div>
    </SheetShell>
  );
}

Object.assign(window, { LEGAL_DOCS, LEGAL_META, LEGAL_KINDS, LegalSheet, CREDITS, CreditsSheet });
