/* legal — cancellation/refund, privacy, terms (tabbed, deep-linkable, bilingual) */

const LEGAL_DOCS = {
  cancellation: {
    en: [
      ["Automated handling", "Modifications, cancellations and refunds are handled automatically via Mr. Wing or by a human agent. Every request is processed strictly in line with the specific airline and hotel supplier policies attached to your booking."],
      ["Supplier-governed terms", "Refund eligibility, change fees and timelines are determined by the operating carrier or property — not by Thirdwing. Before any change is finalized, Mr. Wing presents a visual summary card showing the exact refund or fee so you can explicitly confirm."],
      ["Flights", "Refundable fares are returned to the original payment method, less any carrier-imposed fee. Non-refundable fares may retain residual value as airline credit where the carrier allows. Typical card refunds settle within 7–14 business days."],
      ["Hotels", "Free cancellation applies until the property's stated deadline (commonly 24–72 hours before check-in). Inside the deadline, one or more nights may be charged per the property's policy."],
      ["VVIP & government", "Complex VVIP and government itineraries are escalated to our Egypt Support Hub for white-glove handling and supplier negotiation."],
    ],
    ar: [
      ["المعالجة الآلية", "تُعالَج التعديلات والإلغاءات والاستردادات تلقائيًا عبر مستر وينغ أو بواسطة موظف بشري. يُعالَج كل طلب بدقة وفقًا لسياسات شركة الطيران ومزوّد الفندق المرتبطة بحجزك."],
      ["شروط يحكمها المورّد", "تُحدَّد أهلية الاسترداد ورسوم التغيير والمواعيد من قِبل الناقل المشغّل أو الفندق — وليس من ثيردوينغ. وقبل إتمام أي تغيير، يعرض مستر وينغ بطاقة ملخّص مرئية توضّح مبلغ الاسترداد أو الرسوم بدقة لتؤكّدها صراحةً."],
      ["الطيران", "تُعاد الأسعار القابلة للاسترداد إلى وسيلة الدفع الأصلية بعد خصم أي رسوم يفرضها الناقل. وقد تحتفظ الأسعار غير القابلة للاسترداد بقيمة متبقية كرصيد طيران حيثما يسمح الناقل. وتُسوّى استردادات البطاقات عادةً خلال 7–14 يوم عمل."],
      ["الفنادق", "يُتاح الإلغاء المجاني حتى الموعد النهائي المعلن من الفندق (غالبًا 24–72 ساعة قبل الوصول). وداخل هذا الموعد قد تُحتسب ليلة أو أكثر وفق سياسة الفندق."],
      ["كبار الشخصيات والجهات الحكومية", "تُحوَّل برامج كبار الشخصيات والجهات الحكومية المعقّدة إلى مركز الدعم في مصر لمعاملة استثنائية والتفاوض مع الموردين."],
    ],
  },
  privacy: {
    en: [
      ["Our commitment", "Thirdwing places the protection of corporate and personal data at the center of its operations, with particular care for the confidentiality required by our VVIP and government clients."],
      ["What we collect", "We collect only what is needed to deliver travel: traveler identity details, itinerary data, and tokenized payment references. We never store raw card numbers — payment credentials are tokenized by Tap Payments."],
      ["How we use it", "Data is used solely to search, book, modify and support your travel, to enforce your organization's travel policy, and to produce the reporting you request."],
      ["Confidentiality", "Government and VVIP itineraries are access-restricted within our systems and handled by designated personnel only. We do not sell data, ever."],
      ["Data retention", "We retain traveler and itinerary data only as long as needed to deliver and account for your travel and to meet legal, tax and audit obligations (typically up to 5 years for financial records), after which it is securely deleted or anonymized."],
      ["Your rights (PDPL & GDPR)", "Under Saudi Arabia's Personal Data Protection Law (PDPL) and, where applicable, the GDPR, you may request access, correction, portability, deletion or restriction of your personal data, and you may withdraw consent at any time. Email info@thirdwing.sa and we respond within 30 days."],
      ["Corporate clients & sub-processors", "For corporate accounts we act as a data processor under a Data Processing Agreement (available on request). Personal data is shared only with the suppliers needed to fulfil a booking — airlines, hotels and the Tap Payments gateway — and is never sold."],
    ],
    ar: [
      ["التزامنا", "تضع ثيردوينغ حماية بيانات الشركات والبيانات الشخصية في صميم عملياتها، مع عناية خاصة بالسرّية التي يتطلبها عملاؤنا من كبار الشخصيات والجهات الحكومية."],
      ["ما نجمعه", "نجمع فقط ما يلزم لتقديم خدمة السفر: بيانات هوية المسافر، وبيانات البرنامج، ومراجع الدفع المرمّزة. ولا نخزّن أرقام البطاقات الخام إطلاقًا — فبيانات الدفع تُرمَّز عبر Tap Payments."],
      ["كيف نستخدمها", "تُستخدَم البيانات حصريًا للبحث والحجز والتعديل ودعم سفرك، ولتطبيق سياسة سفر مؤسستك، ولإصدار التقارير التي تطلبها."],
      ["السرّية", "تُقيَّد صلاحية الوصول إلى برامج الجهات الحكومية وكبار الشخصيات داخل أنظمتنا ويتولّاها موظفون مخصّصون فقط. ولا نبيع البيانات أبدًا."],
      ["الاحتفاظ بالبيانات", "نحتفظ ببيانات المسافر والبرنامج فقط للمدة اللازمة لتقديم خدمة سفرك وإثباتها وللوفاء بالالتزامات القانونية والضريبية والتدقيقية (عادةً حتى 5 سنوات للسجلات المالية)، ثم تُحذف بأمان أو تُجعل مجهولة الهوية."],
      ["حقوقك (نظام حماية البيانات الشخصية وGDPR)", "بموجب نظام حماية البيانات الشخصية السعودي (PDPL) واللائحة الأوروبية (GDPR) حيثما تنطبق، يمكنك طلب الاطلاع على بياناتك الشخصية أو تصحيحها أو نقلها أو حذفها أو تقييد معالجتها، ويمكنك سحب موافقتك في أي وقت. راسلنا على info@thirdwing.sa ونردّ خلال 30 يومًا."],
      ["عملاء الشركات والمعالِجون الفرعيون", "لحسابات الشركات نعمل بصفة معالِج بيانات وفق اتفاقية معالجة بيانات (متاحة عند الطلب). ولا تُشارك البيانات الشخصية إلا مع الموردين اللازمين لإتمام الحجز — شركات الطيران والفنادق وبوابة Tap Payments — ولا تُباع أبدًا."],
    ],
  },
  terms: {
    en: [
      ["Agreement", "By using the Thirdwing platform, booking engine and payment gateway, you agree to these terms on behalf of yourself and the organization you represent."],
      ["The booking engine", "Thirdwing acts as an IATA-certified agent (numeric code 71240116) facilitating bookings with third-party airlines and hotels. The operating supplier's conditions of carriage and house rules apply to each booking."],
      ["Payments", "Payments are processed securely through Tap Payments. By saving a card you authorize tokenized storage to enable rapid, 1-click re-booking — including by Mr. Wing acting on your explicit confirmation."],
      ["Mr. Wing", "Mr. Wing acts strictly within flights and hotels and will always require your explicit confirmation before finalizing any booking or cancellation. Complex VVIP requests are escalated to human agents."],
      ["Liability", "Thirdwing is not liable for supplier-caused disruptions such as schedule changes or cancellations, but will coordinate resolution end to end under our HRC-F commitment."],
    ],
    ar: [
      ["الاتفاقية", "باستخدامك منصة ثيردوينغ ومحرّك الحجز وبوابة الدفع، فإنك توافق على هذه الشروط نيابةً عن نفسك وعن المؤسسة التي تمثّلها."],
      ["محرّك الحجز", "تعمل ثيردوينغ بصفتها وكيلًا معتمدًا من الإياتا (الرمز 71240116) لتسهيل الحجوزات مع شركات طيران وفنادق خارجية. وتسري شروط النقل وقواعد المنشأة الخاصة بالمورّد المشغّل على كل حجز."],
      ["المدفوعات", "تُعالَج المدفوعات بأمان عبر Tap Payments. وبحفظك للبطاقة فإنك تأذن بالتخزين المرمّز لتمكين إعادة الحجز السريعة بنقرة واحدة — بما في ذلك عبر مستر وينغ بناءً على تأكيدك الصريح."],
      ["مستر وينغ", "يعمل مستر وينغ ضمن الطيران والفنادق حصرًا، وسيطلب دائمًا تأكيدك الصريح قبل إتمام أي حجز أو إلغاء. وتُحوَّل طلبات كبار الشخصيات المعقّدة إلى موظفين بشريين."],
      ["المسؤولية", "لا تتحمّل ثيردوينغ مسؤولية الاضطرابات الناتجة عن الموردين كتغييرات الجداول أو الإلغاءات، لكنها تنسّق الحل من البداية إلى النهاية ضمن التزامنا بإطار HRC-F."],
    ],
  },
  cookies: {
    en: [
      ["Local storage, not tracking cookies", "Thirdwing does not use advertising or third-party tracking cookies. The experience runs entirely in your browser and stores data locally on your device so the site works and remembers you between visits."],
      ["What we store on your device", "Your language choice, current search, saved cards (tokenized — never the raw card number), bookings, support messages and Mr. Wing conversations are kept in your browser's localStorage. An admin session token is held in sessionStorage for the management panel only, and is cleared when the tab closes."],
      ["Why", "This keeps your trips and preferences available across visits and powers Mr. Wing's 1-click re-booking — without sending personal data to advertising networks or analytics brokers."],
      ["Your control", "Clear this data at any time from your browser settings, or by signing out and deleting your account, which removes your stored profile from this device. No consent banner is required because no non-essential tracking is performed."],
    ],
    ar: [
      ["تخزين محلي، لا ملفات تتبّع", "لا تستخدم ثيردوينغ ملفات تعريف ارتباط إعلانية أو تتبّعًا من أطراف ثالثة. تعمل التجربة بالكامل في متصفحك وتخزّن البيانات محليًا على جهازك ليعمل الموقع ويتذكّرك بين الزيارات."],
      ["ما نخزّنه على جهازك", "يُحفظ اختيارك للغة والبحث الحالي والبطاقات المحفوظة (مرمّزة — لا رقم البطاقة الخام أبدًا) والحجوزات ورسائل الدعم ومحادثات مستر وينغ في ذاكرة متصفحك (localStorage). ويُحفظ رمز جلسة الإدارة في sessionStorage للوحة الإدارة فقط، ويُمسح عند إغلاق التبويب."],
      ["لماذا", "يبقي ذلك رحلاتك وتفضيلاتك متاحة بين الزيارات ويشغّل إعادة الحجز بنقرة واحدة عبر مستر وينغ — دون إرسال بيانات شخصية إلى شبكات الإعلانات أو وسطاء التحليلات."],
      ["تحكّمك", "امسح هذه البيانات في أي وقت من إعدادات متصفحك، أو بتسجيل الخروج وحذف حسابك، ما يزيل ملفك المخزّن من هذا الجهاز. ولا حاجة إلى شريط موافقة لأنه لا يجري أي تتبّع غير ضروري."],
    ],
  },
  accessibility: {
    en: [
      ["Our commitment", "We aim to meet WCAG 2.2 Level AA so that everyone — including travelers using assistive technology — can search, book and manage trips with Thirdwing."],
      ["What we've built in", "Full keyboard navigation, a skip-to-content link, focus management and Escape handling in dialogs (including Mr. Wing), ARIA roles on menus and listboxes, visible focus states, respect for reduced-motion preferences, and complete right-to-left support for Arabic."],
      ["Known limitations", "Some procedurally generated imagery is decorative and text-labelled rather than photographic. We continuously test against the 320–1920px breakpoints and the latest Chrome, Firefox and Safari."],
      ["Contact us", "Email info@thirdwing.sa or call +966 59 702 3357 with any accessibility feedback, and our team will prioritize a fix."],
    ],
    ar: [
      ["التزامنا", "نسعى لاستيفاء معايير WCAG 2.2 المستوى AA حتى يتمكّن الجميع — بمن فيهم المسافرون الذين يستخدمون التقنيات المساعِدة — من البحث والحجز وإدارة الرحلات مع ثيردوينغ."],
      ["ما هيّأناه", "تنقّل كامل بلوحة المفاتيح، ورابط تخطٍّ إلى المحتوى، وإدارة التركيز ومعالجة مفتاح Escape في النوافذ (بما فيها مستر وينغ)، وأدوار ARIA على القوائم وصناديق الاختيار، وحالات تركيز ظاهرة، ومراعاة تفضيل تقليل الحركة، ودعم كامل للكتابة من اليمين إلى اليسار للعربية."],
      ["قيود معلومة", "بعض الصور المولّدة إجرائيًا تزيينية وموسومة بنص بدلًا من كونها فوتوغرافية. ونختبر باستمرار عبر مقاسات 320–1920 بكسل وأحدث إصدارات Chrome وFirefox وSafari."],
      ["تواصل معنا", "راسلنا على info@thirdwing.sa أو اتصل على 3357 702 59 966+ بأي ملاحظات حول إمكانية الوصول، وسيعطي فريقنا إصلاحها الأولوية."],
    ],
  },
};

function LegalPage() {
  const { t, lang } = useLang();
  const { params } = useStore();
  const [tab, setTab] = React.useState(params.tab || "cancellation");
  React.useEffect(() => { if (params.tab) setTab(params.tab); }, [params.tab]);

  const tabs = [
    { id: "cancellation", label: t("legal.tab.cancellation"), icon: "doc" },
    { id: "privacy", label: t("legal.tab.privacy"), icon: "lock" },
    { id: "terms", label: t("legal.tab.terms"), icon: "shield" },
    { id: "cookies", label: t("legal.tab.cookies"), icon: "globe" },
    { id: "accessibility", label: t("legal.tab.accessibility"), icon: "user" },
  ];
  const sections = (LEGAL_DOCS[tab] || LEGAL_DOCS.cancellation)[lang === "ar" ? "ar" : "en"];

  return (
    <main style={{ paddingTop: 96, background: "var(--paper)", minHeight: "100vh" }}>
      <div className="wrap" style={{ paddingTop: 36, paddingBottom: 80, maxWidth: 1000 }}>
        <span className="eyebrow">{t("legal.eyebrow")}</span>
        <h1 style={{ fontFamily: "var(--serif)", fontSize: 42, fontWeight: 600, marginTop: 14 }}>{t("legal.title")}</h1>

        <div style={{ display: "flex", gap: 8, marginTop: 30, marginBottom: 32, flexWrap: "wrap" }} role="tablist">
          {tabs.map(tb => (
            <button key={tb.id} role="tab" aria-selected={tab === tb.id} onClick={() => setTab(tb.id)}
              style={{ display: "flex", alignItems: "center", gap: 9, padding: "11px 18px", borderRadius: 100, cursor: "pointer", fontFamily: "var(--sans)", fontWeight: 600, fontSize: 14, border: "1px solid " + (tab === tb.id ? "var(--emerald)" : "var(--line-2)"), background: tab === tb.id ? "var(--emerald)" : "#fff", color: tab === tb.id ? "#fff" : "var(--ink-2)" }}>
              <Icon name={tb.icon} size={16} /> {tb.label}
            </button>
          ))}
        </div>

        <div className="card card-pad" style={{ padding: 40 }}>
          <LegalBody title={tabs.find(x => x.id === tab).label} updated={t("legal.updated")} sections={sections} />
        </div>
      </div>
    </main>
  );
}

function LegalBody({ title, updated, sections }) {
  return (
    <div>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", flexWrap: "wrap", gap: 10, borderBottom: "1px solid var(--line)", paddingBottom: 18, marginBottom: 8 }}>
        <h2 style={{ fontFamily: "var(--serif)", fontSize: 30, fontWeight: 600 }}>{title}</h2>
        <span className="muted" style={{ fontSize: 13 }}>{updated}</span>
      </div>
      {sections.map(([h, body], i) => (
        <div key={i} style={{ padding: "22px 0", borderBottom: i < sections.length - 1 ? "1px solid var(--line)" : "none" }}>
          <h3 style={{ fontSize: 17, fontWeight: 700, color: "var(--emerald)", marginBottom: 8 }}>{i + 1}. {h}</h3>
          <p style={{ color: "var(--ink-2)", fontSize: 15.5, lineHeight: 1.7 }}>{body}</p>
        </div>
      ))}
    </div>
  );
}

Object.assign(window, { LegalPage, LegalBody, LEGAL_DOCS });
