import type { Metadata } from 'next';
import Link from 'next/link';
import { isLocale, localePath, type Locale } from '@ai-radar/shared';
import { notFound } from 'next/navigation';
import { absoluteUrl, localeAlternates } from '@/lib/site';

type Params = Promise<{ lang: string }>;

export async function generateMetadata({ params }: { params: Params }): Promise<Metadata> {
  const { lang } = await params;
  if (!isLocale(lang)) return {};

  return {
    title: lang === 'ar' ? 'سياسة الخصوصية' : 'Privacy policy',
    description:
      lang === 'ar'
        ? 'كيف يجمع تطبيق On AI Radar بياناتك ويستخدمها ويحميها ويحذفها.'
        : 'How On AI Radar collects, uses, protects and deletes your data.',
    alternates: {
      canonical: absoluteUrl(`/${lang}/privacy`),
      languages: localeAlternates('/privacy'),
    },
  };
}

export default async function PrivacyPage({ params }: { params: Params }) {
  const { lang } = await params;
  if (!isLocale(lang)) notFound();

  const c = CONTENT[lang];

  return (
    <main id="main" className="page story" style={{ paddingBlock: 'var(--s10)' }}>
      <h1 className="t-h1">{c.title}</h1>
      <p className="story-summary">{c.intro}</p>
      <p className="t-meta" style={{ marginTop: 'var(--s3)' }}>{c.effective}</p>

      <div className="story-fields">
        {c.sections.map((section) => (
          <section key={section.heading} className="story-field">
            <h2 className="why-label">{section.heading}</h2>
            {section.body.map((paragraph) => (
              <p key={paragraph} className="t-body" style={{ marginTop: 'var(--s2)' }}>
                {paragraph}
              </p>
            ))}
            {section.list && (
              <ul className="about-list">
                {section.list.map((item) => <li key={item}>{item}</li>)}
              </ul>
            )}
          </section>
        ))}

        <section className="story-field" id="contact">
          <h2 className="why-label">{c.contactTitle}</h2>
          <p className="t-body" style={{ marginTop: 'var(--s2)' }}>
            {c.contactBody}{' '}
            <Link href={localePath('/delete-account', lang)}>{c.deleteLink}</Link>.
          </p>
        </section>
      </div>
    </main>
  );
}

interface PrivacyContent {
  title: string;
  intro: string;
  effective: string;
  sections: Array<{ heading: string; body: string[]; list?: string[] }>;
  contactTitle: string;
  contactBody: string;
  deleteLink: string;
}

const CONTENT: Record<Locale, PrivacyContent> = {
  en: {
    title: 'Privacy policy',
    intro:
      'This policy explains how On AI Radar, published by CrazyBeans, handles information when you use the Android app and its account services.',
    effective: 'Effective September 12, 2026',
    sections: [
      {
        heading: 'Information we collect',
        body: ['You can read On AI Radar without an account. If you choose account or alert features, we collect only the information needed to provide them.'],
        list: [
          'Account information: email address, optional display name, a securely hashed password, email-verification status and linked sign-in provider identifiers.',
          'Preferences and content: language, theme, alert filters and the IDs of stories you save while signed in.',
          'App and device information: push-notification token, platform, notification preferences and locale when notifications are enabled.',
          'Security information: hashed session tokens, client type, limited user-agent information, a hashed IP address and session timestamps.',
        ],
      },
      {
        heading: 'Information kept on your device',
        body: [
          'The app stores your theme, language, sign-in session, notification choice and offline saved stories on your device. You can remove this local information by signing out where applicable, clearing the app data or uninstalling the app.',
        ],
      },
      {
        heading: 'How we use information',
        body: [],
        list: [
          'Authenticate your account and keep it secure.',
          'Synchronise preferences and saved stories across the app and website.',
          'Send alerts you explicitly enable and apply your chosen filters.',
          'Operate, troubleshoot and protect the service from abuse.',
        ],
      },
      {
        heading: 'Sharing and third parties',
        body: [
          'We do not sell personal information. Information may be processed by service providers that host the service, deliver email or push notifications, or provide a sign-in method, only to perform those services. Opening an original news source takes you to that third party, whose own privacy policy then applies.',
          'The Android app does not include advertising or third-party analytics SDKs.',
        ],
      },
      {
        heading: 'Retention and deletion',
        body: [
          'Account data is kept while your account is active. You can permanently delete your account from Settings in the app or from the web deletion page. The deletion removes your account, sessions, linked sign-ins, server-side saved stories, preferences, and linked email, Telegram and device alert records. Published editorial content is not tied to an account.',
        ],
      },
      {
        heading: 'Security and children',
        body: [
          'Passwords and session tokens are not stored in plain text. We use access controls and encrypted network connections appropriate to the service. No security method is perfect.',
          'On AI Radar is a general-audience technology news service and is not directed to children under 13.',
        ],
      },
      {
        heading: 'Changes',
        body: ['We may update this policy when the app or its data practices change. The effective date above will be updated when we do.'],
      },
    ],
    contactTitle: 'Your choices and contact',
    contactBody: 'You can disable notifications in the app at any time. For account and deletion requests, use the',
    deleteLink: 'account deletion page',
  },
  ar: {
    title: 'سياسة الخصوصية',
    intro:
      'توضح هذه السياسة كيف يتعامل تطبيق On AI Radar، المنشور بواسطة CrazyBeans، مع المعلومات عند استخدام تطبيق أندرويد وخدمات الحساب.',
    effective: 'سارية من 12 سبتمبر 2026',
    sections: [
      {
        heading: 'المعلومات التي نجمعها',
        body: ['يمكنك قراءة On AI Radar دون حساب. وإذا اخترت ميزات الحساب أو التنبيهات، نجمع فقط المعلومات اللازمة لتقديمها.'],
        list: [
          'بيانات الحساب: البريد الإلكتروني، والاسم المعروض الاختياري، وكلمة المرور بعد تحويلها إلى بصمة آمنة، وحالة تأكيد البريد، ومعرّفات مزودي تسجيل الدخول المرتبطين.',
          'التفضيلات والمحتوى: اللغة والمظهر وفلاتر التنبيهات ومعرّفات الأخبار التي تحفظها أثناء تسجيل الدخول.',
          'بيانات التطبيق والجهاز: رمز الإشعارات والمنصة وتفضيلات الإشعارات واللغة عند تفعيل الإشعارات.',
          'بيانات الأمان: بصمات رموز الجلسات ونوع العميل ومعلومات محدودة عن المتصفح أو الجهاز وبصمة عنوان IP وتوقيتات الجلسات.',
        ],
      },
      {
        heading: 'المعلومات المحفوظة على جهازك',
        body: [
          'يخزن التطبيق على جهازك المظهر واللغة وجلسة الدخول واختيار الإشعارات والأخبار المحفوظة للاستخدام دون اتصال. يمكنك إزالة هذه البيانات المحلية بتسجيل الخروج عند انطباق ذلك، أو مسح بيانات التطبيق، أو إلغاء تثبيته.',
        ],
      },
      {
        heading: 'كيف نستخدم المعلومات',
        body: [],
        list: [
          'تسجيل دخولك وحماية حسابك.',
          'مزامنة التفضيلات والأخبار المحفوظة بين التطبيق والموقع.',
          'إرسال التنبيهات التي تفعّلها صراحةً وتطبيق الفلاتر التي تختارها.',
          'تشغيل الخدمة ومعالجة الأعطال وحمايتها من إساءة الاستخدام.',
        ],
      },
      {
        heading: 'المشاركة والأطراف الأخرى',
        body: [
          'لا نبيع المعلومات الشخصية. قد يعالجها مزودو الخدمات الذين يستضيفون الخدمة أو يرسلون البريد أو الإشعارات أو يقدمون طريقة تسجيل دخول، وذلك فقط لأداء تلك الخدمات. عند فتح المصدر الأصلي للخبر تنتقل إلى طرف آخر وتسري حينها سياسة الخصوصية الخاصة به.',
          'لا يتضمن تطبيق أندرويد حزم إعلانات أو تحليلات تابعة لجهات خارجية.',
        ],
      },
      {
        heading: 'الاحتفاظ والحذف',
        body: [
          'نحتفظ ببيانات الحساب ما دام نشطاً. يمكنك حذف حسابك نهائياً من الإعدادات داخل التطبيق أو من صفحة الحذف على الويب. يزيل الحذف الحساب والجلسات وطرق الدخول المرتبطة والأخبار المحفوظة على الخادم والتفضيلات وسجلات تنبيهات البريد وتيليجرام والجهاز المرتبطة. المحتوى التحريري المنشور غير مرتبط بأي حساب.',
        ],
      },
      {
        heading: 'الأمان والأطفال',
        body: [
          'لا نخزن كلمات المرور أو رموز الجلسات بصورتها المقروءة. نستخدم ضوابط وصول واتصالات شبكة مشفرة مناسبة للخدمة، مع العلم أنه لا توجد وسيلة أمان كاملة.',
          'On AI Radar خدمة أخبار تقنية للجمهور العام وليست موجهة للأطفال دون 13 عاماً.',
        ],
      },
      {
        heading: 'التغييرات',
        body: ['قد نحدّث هذه السياسة عند تغيّر التطبيق أو ممارسات البيانات، وسنحدّث تاريخ السريان أعلاه عند ذلك.'],
      },
    ],
    contactTitle: 'اختياراتك والتواصل',
    contactBody: 'يمكنك إيقاف الإشعارات من التطبيق في أي وقت. لطلبات الحساب والحذف استخدم',
    deleteLink: 'صفحة حذف الحساب',
  },
};
