/**
 * The user guide shown at `/guide`. Every section is written twice, English and Arabic, so the
 * reader can switch the guide language on the page itself whatever the interface language is.
 * Keep it about how the office works, not about the code — `GUIDE.md` is the developer one.
 */

export type GuideLang = "en" | "ar";

export type GuideSection = {
  id: string;
  group: Record<GuideLang, string>;
  title: Record<GuideLang, string>;
  intro: Record<GuideLang, string>;
  steps: Array<Record<GuideLang, string>>;
  href?: string;
  linkLabel?: Record<GuideLang, string>;
};

export const GUIDE_SECTIONS: GuideSection[] = [
  {
    id: "start",
    group: { en: "Getting started", ar: "البداية" },
    title: { en: "How the dashboard is organised", ar: "كيف تنظَّم لوحة التحكم" },
    intro: {
      en: "The sidebar follows the work: Sales, Stock & purchasing, the internal store, Field work, Finance, Insights and Admin. Each page shows data; the buttons at the top of the page open the forms in a popup.",
      ar: "القائمة الجانبية مرتّبة حسب العمل: المبيعات، المخزون والشراء، المحل الداخلي، العمل الميداني، المالية، التحليلات والإدارة. كل صفحة تعرض البيانات، والأزرار في أعلى الصفحة تفتح الاستمارات في نافذة منبثقة.",
    },
    steps: [
      { en: "You only see the pages your role allows. If a page is missing, ask an administrator for the permission.", ar: "ترى فقط الصفحات التي يسمح بها دورك. إذا كانت صفحة غائبة فاطلب الصلاحية من المدير." },
      { en: "The language and the light/dark theme are changed at the bottom of the sidebar.", ar: "تُغيَّر اللغة والوضع الفاتح/الداكن من أسفل القائمة الجانبية." },
      { en: "Changing or deleting an existing record always asks for a reason; it is kept in the activity log.", ar: "تعديل أو حذف أي سجل موجود يطلب سببًا دائمًا، ويُحفظ السبب في سجل النشاط." },
    ],
  },
  {
    id: "catalog",
    group: { en: "Sales", ar: "المبيعات" },
    title: { en: "Categories and products", ar: "الأصناف والمنتجات" },
    intro: {
      en: "Create the category first, then the product: name, reference, photo and the three prices (retail, semi-wholesale, wholesale).",
      ar: "أنشئ الصنف أولًا ثم المنتج: الاسم، المرجع، الصورة والأسعار الثلاثة (تجزئة، نصف جملة، جملة).",
    },
    steps: [
      { en: "A new product starts with no stock. That is on purpose: goods only enter the warehouse through a purchase.", ar: "يبدأ المنتج الجديد بدون مخزون، وهذا مقصود: لا تدخل البضاعة إلى المستودع إلا عبر عملية شراء." },
      { en: "Open a product to see its photo, its prices per account type, its stock history and the orders that contain it.", ar: "افتح المنتج لترى صورته وأسعاره حسب نوع الحساب وتاريخ مخزونه والطلبات التي تحتويه." },
      { en: "Deactivate a product instead of deleting it when it has history.", ar: "عطّل المنتج بدل حذفه إذا كان له تاريخ سابق." },
    ],
    href: "/products",
    linkLabel: { en: "Open Categories & products", ar: "فتح الأصناف والمنتجات" },
  },
  {
    id: "purchases",
    group: { en: "Stock & purchasing", ar: "المخزون والشراء" },
    title: { en: "Filling the stock: supplier then purchase", ar: "تعبئة المخزون: المورّد ثم الشراء" },
    intro: {
      en: "This is the full circle: create the product, record a purchase from a supplier, and the stock appears in the warehouse.",
      ar: "هذه هي الدورة الكاملة: أنشئ المنتج، سجّل عملية شراء من مورّد، فيظهر المخزون في المستودع.",
    },
    steps: [
      { en: "Add the supplier on the Suppliers page (company, contact, phone, tax identifiers).", ar: "أضف المورّد في صفحة المورّدين (الشركة، جهة الاتصال، الهاتف، المعرّفات الجبائية)." },
      { en: "On Purchases, click New purchase, add the product lines with quantity and unit cost, then choose the supplier and the date.", ar: "في صفحة المشتريات اضغط «شراء جديد»، أضف أسطر المنتجات بالكمية وسعر التكلفة، ثم اختر المورّد والتاريخ." },
      { en: "Tick “goods already received” to add them to stock right away, or leave the purchase as a draft and receive it later.", ar: "أشّر على «البضاعة مستلمة» لإضافتها إلى المخزون فورًا، أو اترك الشراء مسودة واستلمه لاحقًا." },
      { en: "Receiving writes a PURCHASE movement in the stock ledger and updates the product's last cost price.", ar: "الاستلام يسجّل حركة شراء في دفتر المخزون ويحدّث آخر سعر تكلفة للمنتج." },
      { en: "Pay the supplier from the purchase page or the supplier page; what is still owed is shown on both.", ar: "ادفع للمورّد من صفحة الشراء أو صفحة المورّد، والمبلغ المتبقي يظهر في كليهما." },
    ],
    href: "/purchases",
    linkLabel: { en: "Open Purchases", ar: "فتح المشتريات" },
  },
  {
    id: "stock",
    group: { en: "Stock & purchasing", ar: "المخزون والشراء" },
    title: { en: "The stock ledger", ar: "دفتر المخزون" },
    intro: {
      en: "Every quantity change is a line in the ledger: what moved, how much, which document caused it, who did it and the balance after the move.",
      ar: "كل تغيير في الكمية هو سطر في الدفتر: ما الذي تحرّك، وكم، وأي وثيقة سبّبته، ومن قام به، والرصيد بعد الحركة.",
    },
    steps: [
      { en: "Purchases bring stock in. Validated delivery notes and counter sales take it out.", ar: "المشتريات تُدخل المخزون، وسندات التسليم المصادق عليها والبيع بالمحل تُخرجه." },
      { en: "Loading a truck for a van-sale round moves stock out; what comes back is returned to the warehouse.", ar: "تحميل الشاحنة لجولة بيع يُخرج المخزون، وما يعود منه يُرجَع إلى المستودع." },
      { en: "Manual movements are only for corrections: loss, inventory count, customer return, adjustment. They always need a reason.", ar: "الحركات اليدوية للتصحيح فقط: ضياع، جرد، إرجاع زبون، تعديل. وتتطلب سببًا دائمًا." },
    ],
    href: "/stock",
    linkLabel: { en: "Open Stock movements", ar: "فتح حركات المخزون" },
  },
  {
    id: "customers",
    group: { en: "Sales", ar: "المبيعات" },
    title: { en: "Customer accounts", ar: "حسابات الزبائن" },
    intro: {
      en: "A customer has an account type — retail, semi-wholesale or wholesale — and that type decides the price they get everywhere in the application.",
      ar: "لكل زبون نوع حساب — تجزئة أو نصف جملة أو جملة — وهذا النوع هو الذي يحدّد السعر الذي يحصل عليه في كل مكان في التطبيق.",
    },
    steps: [
      { en: "Fill the phone number and pick the location on the map; both are shown on the customer card and used by the drivers.", ar: "املأ رقم الهاتف واختر الموقع على الخريطة؛ الاثنان يظهران في بطاقة الزبون ويستعملهما السائقون." },
      { en: "Billing details (company, trade register, tax ID, statistical ID, article) are printed on the documents.", ar: "معلومات الفوترة (الشركة، السجل التجاري، الرقم الجبائي، الرقم الإحصائي، المادة) تُطبع على الوثائق." },
      { en: "A new account waits to be accepted before it can order. The credit limit is set on the customer page.", ar: "الحساب الجديد ينتظر القبول قبل أن يطلب. ويُضبط سقف الدين في صفحة الزبون." },
    ],
    href: "/customers",
    linkLabel: { en: "Open Customers", ar: "فتح الزبائن" },
  },
  {
    id: "pricing",
    group: { en: "Sales", ar: "المبيعات" },
    title: { en: "Prices and quantity windows", ar: "الأسعار ونوافذ الكميات" },
    intro: {
      en: "For each product and each account type you set a price and the minimum and maximum quantity that type may order.",
      ar: "لكل منتج ولكل نوع حساب تضبط سعرًا والحد الأدنى والأقصى للكمية التي يمكن لهذا النوع طلبها.",
    },
    steps: [
      { en: "The price is applied automatically from the customer's account type — nobody types a price into an order.", ar: "يُطبَّق السعر تلقائيًا حسب نوع حساب الزبون، ولا أحد يكتب السعر في الطلب." },
      { en: "An order outside the quantity window is refused by the server, not only hidden in the screen.", ar: "الطلب خارج نافذة الكمية يُرفض من الخادم، لا أن يُخفى في الشاشة فقط." },
      { en: "A minimum order amount can be set per account type.", ar: "يمكن ضبط حد أدنى لمبلغ الطلب لكل نوع حساب." },
      { en: "Every price change is kept in the price change log on the product page.", ar: "كل تغيير في السعر يُحفظ في سجل تغيّر الأسعار في صفحة المنتج." },
    ],
    href: "/pricing",
    linkLabel: { en: "Open Pricing", ar: "فتح الأسعار" },
  },
  {
    id: "orders",
    group: { en: "Sales", ar: "المبيعات" },
    title: { en: "Orders", ar: "الطلبات" },
    intro: {
      en: "An order is created for a customer, accepted, scheduled on a round, delivered and then collected.",
      ar: "يُنشأ الطلب لزبون، ثم يُقبل، ثم يُجدول في جولة، ثم يُسلَّم، ثم يُحصَّل.",
    },
    steps: [
      { en: "Create an order on behalf of a customer and add as many products as needed; the prices follow the account type.", ar: "أنشئ طلبًا نيابة عن الزبون وأضف ما تشاء من المنتجات؛ الأسعار تتبع نوع الحساب." },
      { en: "Quantities can still be changed on the order page as long as the order is not closed — with a reason.", ar: "يمكن تعديل الكميات في صفحة الطلب ما دام الطلب غير مغلق — مع ذكر السبب." },
      { en: "From the order you issue a proforma invoice and a delivery note.", ar: "من الطلب تُصدر فاتورة أولية وسند تسليم." },
      { en: "A partial delivery records what was really delivered; a refusal is saved with its reason.", ar: "التسليم الجزئي يسجّل ما سُلّم فعلًا، والرفض يُحفظ مع سببه." },
    ],
    href: "/orders",
    linkLabel: { en: "Open Orders", ar: "فتح الطلبات" },
  },
  {
    id: "rounds",
    group: { en: "Field work", ar: "العمل الميداني" },
    title: { en: "The two kinds of round", ar: "نوعا الجولة" },
    intro: {
      en: "A delivery round goes out with booked orders. A van-sale (prospecting) round goes out with stock on the truck and sells on the road.",
      ar: "جولة التوزيع تخرج بطلبات محجوزة مسبقًا، وجولة البيع (الاستكشاف) تخرج ببضاعة على الشاحنة وتبيع في الطريق.",
    },
    steps: [
      { en: "Create the round with its date, area, driver and truck, then assign the booked orders as stops.", ar: "أنشئ الجولة بتاريخها ومنطقتها وسائقها وشاحنتها، ثم عيّن الطلبات المحجوزة كمحطات." },
      { en: "The order of the stops is yours to set — type a position or use the arrows, then save.", ar: "ترتيب المحطات بيدك — اكتب الرقم أو استعمل الأسهم ثم احفظ." },
      { en: "At a stop you record what was actually sold, add extra products, and take the payment in one step.", ar: "في المحطة تسجّل ما بيع فعلًا، وتضيف منتجات إضافية، وتأخذ الدفعة في خطوة واحدة." },
      { en: "For a van-sale round: load the truck from the warehouse, record each field result (paid, part paid or unpaid order), and when the driver is back record the remaining stock to restock the warehouse.", ar: "في جولة البيع: حمّل الشاحنة من المستودع، سجّل كل نتيجة ميدانية (مدفوع، مدفوع جزئيًا، أو طلب غير مدفوع)، وعند عودة السائق سجّل المخزون المتبقي لإرجاعه إلى المستودع." },
      { en: "The round shows what there is to collect, what was collected, the debt and the stock left on the truck.", ar: "تعرض الجولة ما يجب تحصيله وما حُصّل والدين والمخزون المتبقي على الشاحنة." },
    ],
    href: "/rounds",
    linkLabel: { en: "Open Rounds", ar: "فتح الجولات" },
  },
  {
    id: "fleet",
    group: { en: "Field work", ar: "العمل الميداني" },
    title: { en: "Drivers and trucks", ar: "السائقون والشاحنات" },
    intro: {
      en: "Drivers are cards kept by the office: name, phone, region, base location and the assigned truck. A driver has no account and never signs in.",
      ar: "السائقون بطاقات يحتفظ بها المكتب: الاسم، الهاتف، المنطقة، موقع الانطلاق والشاحنة المسندة. السائق ليس له حساب ولا يسجّل الدخول أبدًا.",
    },
    steps: [
      { en: "Open a driver card to see the recent rounds, what was collected and the activity feed.", ar: "افتح بطاقة السائق لترى الجولات الأخيرة وما حُصّل وسجل النشاط." },
      { en: "Trucks are edited on their own page, which also shows every round and delivery note they carried.", ar: "تُعدَّل الشاحنات في صفحتها الخاصة، وهي تعرض أيضًا كل جولة وسند تسليم حملته." },
    ],
    href: "/fleet",
    linkLabel: { en: "Open Drivers", ar: "فتح السائقين" },
  },
  {
    id: "documents",
    group: { en: "Finance", ar: "المالية" },
    title: { en: "Proformas and delivery notes", ar: "الفواتير الأولية وسندات التسليم" },
    intro: {
      en: "Both documents are numbered in sequence, printed on A4 and always printed in French/English — they are never printed in Arabic.",
      ar: "الوثيقتان مرقّمتان بالتسلسل وتُطبعان على A4 وتبقيان دائمًا بالفرنسية/الإنجليزية — ولا تُطبعان بالعربية أبدًا.",
    },
    steps: [
      { en: "A proforma can be created from an order, or on its own; when it is on its own you choose whether VAT and the tax stamp apply and you can add a discount.", ar: "يمكن إنشاء الفاتورة الأولية من طلب أو بشكل مستقل؛ وفي الحالة المستقلة تختار تطبيق الرسم على القيمة المضافة وطابع الضريبة ويمكنك إضافة تخفيض." },
      { en: "Validating a delivery note takes the goods out of the warehouse and starts the 15-day payment clock.", ar: "المصادقة على سند التسليم تُخرج البضاعة من المستودع وتبدأ مهلة الدفع البالغة 15 يومًا." },
      { en: "Once a proforma is validated, only an administrator — or a role with the special permission — can still change it.", ar: "بعد المصادقة على الفاتورة الأولية لا يمكن تعديلها إلا من المدير أو من دور يملك الصلاحية الخاصة." },
      { en: "All the delivery notes of one round can be printed together from the round page.", ar: "يمكن طباعة كل سندات التسليم الخاصة بجولة واحدة دفعة واحدة من صفحة الجولة." },
    ],
    href: "/delivery-notes",
    linkLabel: { en: "Open Delivery notes", ar: "فتح سندات التسليم" },
  },
  {
    id: "payments",
    group: { en: "Finance", ar: "المالية" },
    title: { en: "Payments and late payers", ar: "الدفعات والمتأخرون" },
    intro: {
      en: "A payment is recorded against a customer and is applied to the oldest unpaid document first.",
      ar: "تُسجَّل الدفعة باسم الزبون وتُطبَّق على أقدم وثيقة غير مدفوعة أولًا.",
    },
    steps: [
      { en: "Cash collected by a driver is handed in as a remittance, so what is in the field and what is in the safe stay separate.", ar: "النقد الذي يحصّله السائق يُسلَّم كتحويل، حتى يبقى ما في الميدان وما في الصندوق منفصلين." },
      { en: "Lateness is counted from the delivery-note date; a customer is flagged late 15 days after it.", ar: "يُحسب التأخّر من تاريخ سند التسليم، ويُعلَّم الزبون كمتأخّر بعد 15 يومًا منه." },
      { en: "The overview and the statistics pages show the unpaid balances by age.", ar: "تعرض صفحة النظرة العامة وصفحة الإحصائيات الأرصدة غير المدفوعة حسب العمر." },
    ],
    href: "/payments",
    linkLabel: { en: "Open Payments", ar: "فتح الدفعات" },
  },
  {
    id: "barcode",
    group: { en: "Getting started", ar: "البداية" },
    title: { en: "The barcode scanner", ar: "قارئ الرمز الشريطي" },
    intro: {
      en: "Any USB or Bluetooth counter scanner works, with nothing to install: the scanner behaves like a keyboard, so plug it in and it is ready.",
      ar: "أي قارئ للمحل بوصلة USB أو بلوتوث يعمل دون تثبيت أي برنامج: القارئ يتصرّف مثل لوحة المفاتيح، فما عليك إلا توصيله.",
    },
    steps: [
      { en: "Give the product its barcode first: open the product, press Edit and scan into the barcode box. The same box is on the create form.", ar: "امنح المنتج رمزه أولًا: افتح المنتج واضغط «تعديل» وامسح الرمز في خانة الرمز الشريطي. الخانة نفسها موجودة في استمارة الإنشاء." },
      { en: "Two products cannot share one barcode; the second one is refused and tells you which product already has it.", ar: "لا يمكن لمنتجين أن يتقاسما رمزًا واحدًا؛ يُرفض الثاني مع إخبارك بالمنتج الذي يحمله." },
      { en: "At the point of sale, scan anywhere on the screen: the product drops into the basket and its name appears in green.", ar: "في نقطة البيع امسح في أي مكان من الشاشة: يسقط المنتج في السلة ويظهر اسمه بالأخضر." },
      { en: "If the code is unknown, or the product is not on the shop shelf, the screen says so in red instead of adding it.", ar: "إذا كان الرمز غير معروف أو كان المنتج غير موجود على رف المحل، تُظهر الشاشة ذلك بالأحمر بدل إضافته." },
      { en: "You can also search a product by its barcode on the products page.", ar: "يمكنك أيضًا البحث عن منتج برمزه الشريطي في صفحة المنتجات." },
    ],
    href: "/store",
    linkLabel: { en: "Open Point of sale", ar: "فتح نقطة البيع" },
  },
  {
    id: "packaging",
    group: { en: "Sales", ar: "المبيعات" },
    title: { en: "Single pieces and cartons", ar: "القطع المفردة والكراتين" },
    intro: {
      en: "When you create a product you say how it is sold: as a single piece, or as a carton — and then how many pieces the carton holds.",
      ar: "عند إنشاء المنتج تحدّد كيف يُباع: قطعة مفردة أو كرتونة — ثم كم قطعة تحتويها الكرتونة.",
    },
    steps: [
      { en: "Stock, prices and orders are always counted in what you sell: one carton counts as one.", ar: "يُحسب المخزون والأسعار والطلبات دائمًا بما تبيعه: الكرتونة الواحدة تُحسب واحدة." },
      { en: "The product page also shows the total in pieces, so 25 cartons of 12 reads as 300 pieces.", ar: "تعرض صفحة المنتج أيضًا المجموع بالقطع، فـ25 كرتونة من 12 تظهر 300 قطعة." },
      { en: "The carton size is printed under the product name in the lists and on the point-of-sale tiles.", ar: "يظهر حجم الكرتونة تحت اسم المنتج في القوائم وفي بطاقات نقطة البيع." },
    ],
    href: "/products",
    linkLabel: { en: "Open Categories & products", ar: "فتح الأصناف والمنتجات" },
  },
  {
    id: "store",
    group: { en: "Internal store", ar: "المحل الداخلي" },
    title: { en: "The shop counter", ar: "محل البيع بالتجزئة" },
    intro: {
      en: "The internal store sells at retail to whoever walks in. Its shelf is fed from the main warehouse, and the transfer is written in the stock ledger.",
      ar: "المحل الداخلي يبيع بالتجزئة لمن يدخل. ويُزوَّد رفّه من المستودع الرئيسي، ويُسجَّل التحويل في دفتر المخزون.",
    },
    steps: [
      { en: "Move goods to the store from Store stock; what is left can be returned to the warehouse the same way.", ar: "انقل البضاعة إلى المحل من صفحة مخزون المحل، وما يتبقى يمكن إرجاعه إلى المستودع بالطريقة نفسها." },
      { en: "At the point of sale, the total to pay is shown large at the top; products that are out of stock are greyed out and cannot be added.", ar: "في نقطة البيع يظهر المبلغ الإجمالي كبيرًا في الأعلى، والمنتجات غير المتوفرة تظهر باهتة ولا يمكن إضافتها." },
      { en: "The basket sits next to the products, quantities can be edited there, and the sale prints a counter receipt.", ar: "السلة بجانب المنتجات ويمكن تعديل الكميات فيها، والبيع يطبع وصل بيع." },
      { en: "Shop expenses (rent, salaries, supplies…) are recorded on the Expenses page.", ar: "مصاريف المحل (الإيجار، الأجور، اللوازم…) تُسجَّل في صفحة المصاريف." },
    ],
    href: "/store",
    linkLabel: { en: "Open Point of sale", ar: "فتح نقطة البيع" },
  },
  {
    id: "users",
    group: { en: "Admin", ar: "الإدارة" },
    title: { en: "Users, roles and reasons", ar: "المستخدمون والأدوار والأسباب" },
    intro: {
      en: "A user is given a role, and the role carries the permission matrix: for each department, who may view, create, edit and delete.",
      ar: "يُسند لكل مستخدم دور، والدور يحمل مصفوفة الصلاحيات: لكل قسم من يمكنه الاطلاع والإنشاء والتعديل والحذف.",
    },
    steps: [
      { en: "A page a role may not view is also refused when its address is typed by hand.", ar: "الصفحة التي لا يملك الدور حق الاطلاع عليها تُرفض أيضًا عند كتابة عنوانها يدويًا." },
      { en: "Editing a validated invoice is a special permission that the administrator can grant to a role.", ar: "تعديل فاتورة مصادق عليها صلاحية خاصة يمكن للمدير منحها لدور." },
      { en: "Every change and deletion asks for a reason and is written to the activity log with the user and the time.", ar: "كل تعديل وحذف يطلب سببًا ويُكتب في سجل النشاط مع المستخدم والوقت." },
    ],
    href: "/users",
    linkLabel: { en: "Open Users & roles", ar: "فتح المستخدمين والأدوار" },
  },
  {
    id: "insights",
    group: { en: "Insights", ar: "التحليلات" },
    title: { en: "Statistics, analysis and the activity log", ar: "الإحصائيات والتحليل وسجل النشاط" },
    intro: {
      en: "The overview gives the day's snapshot with charts; statistics and data analysis go deeper over a period you choose, and everything can be exported to Excel.",
      ar: "تعطي النظرة العامة لقطة اليوم مع رسوم بيانية، والإحصائيات وتحليل البيانات تتعمّق أكثر في فترة تختارها، ويمكن تصدير كل شيء إلى إكسل.",
    },
    steps: [
      { en: "The activity log answers “who changed this, when and why”.", ar: "سجل النشاط يجيب عن «من غيّر هذا، ومتى، ولماذا»." },
      { en: "Notifications collect what needs attention: accounts to accept, late payers, low stock.", ar: "تجمع الإشعارات ما يحتاج انتباهًا: حسابات تنتظر القبول، متأخرون في الدفع، مخزون منخفض." },
    ],
    href: "/activity",
    linkLabel: { en: "Open Activity log", ar: "فتح سجل النشاط" },
  },
];

/** Matches a section against the search box; both languages are searched, whatever is displayed. */
export function guideMatches(section: GuideSection, query: string) {
  const needle = query.trim().toLowerCase();
  if (!needle) return true;
  const haystack = [
    section.title.en, section.title.ar,
    section.group.en, section.group.ar,
    section.intro.en, section.intro.ar,
    ...section.steps.flatMap((step) => [step.en, step.ar]),
  ]
    .join(" ")
    .toLowerCase();
  return needle.split(/\s+/).every((word) => haystack.includes(word));
}
