WHATSAPP BOT V23 — REPLACEMENT WARRANTY UPGRADE
==============================================

IMPORTANT: Keep a full cPanel files backup AND MySQL backup before deploying.

INSTALLATION (correct order)
----------------------------
1. In cPanel > phpMyAdmin, select the bot's current MySQL database.
2. Import: database/V23_REPLACEMENT_UPGRADE.sql
   Do this BEFORE replacing the live PHP files. The migration is additive and
   does NOT delete orders, customer records, balances, wallet or purchase ledger.
3. Upload this FULL ZIP's bot.shoaibsubscription.pk folder contents into the
   existing live website directory, replacing previous files.
4. Confirm Admin > Replacement History works with no migration warning.
5. Ensure the EXISTING NayaPay cron/nayapay_verify.php cron still runs.
   V23 integrates one daily replacement cleanup into that existing cron.
   If your existing cron is not running, schedule replacement cleanup via
   cPanel Cron Jobs (daily) with the correct local PHP binary and real path:
      0 3 * * * /usr/local/bin/php /home/YOUR_CPANEL_USER/public_html/cron/replacement_cleanup.php
   Change the example paths to match YOUR hosting setup; do not copy them blindly.

HOW IT WORKS
------------
* Each NEW delivered order gets a code V-00001, V-00002 etc. (5+ digits).
* Purchase date/order reference show in the customer WhatsApp delivery and
  admin Orders/Replacement History pages.
* Each delivered account email is captured separately, including bulk orders.
* Send REPLACEMENT, then send:
    V-00042
    Email: broken.account@example.com
  For bulk orders, list two different emails under the SAME code.
* The bot checks SAME WhatsApp customer, actually DELIVERED order, exact email,
  days elapsed since delivered_at, and whether it was already replaced.
* Days 0–17 inclusive: can auto-replace ONE eligible original email once.
* Day 18 onward: contacts the admin at 03193457316.
* Missing Order ID: bot asks for ID and email in same message.
* One claimed email cannot be claimed again; a new account email is also logged
  by a permanent SHA-256 fingerprint to stop recycled account abuse.
* If unused valid replacement stock is temporarily unavailable, request is
  saved as pending; once stock is added the customer may retry same request.
* Only an ACCOUNT that actually belongs to that order can be replaced.
* Manual/invited delivery emails are tracked but referred to admin, because
  they cannot necessarily be auto-replaced by sending a random stock account.
* NO new category-selection or replacement stock pool is created. Existing
  product_stock is used, matching the original product.

20-DAY RETENTION
----------------
Detailed V23 account tracking and associated replacement history auto-expire
20 days after delivery. The original financial orders, wallet transactions,
customer ledger and permanent hashed anti-duplicate claim records are kept.
Existing V22 legacy replacement_history rows without an order link are not
removed, to avoid deleting legacy business history unexpectedly.

IMPORTANT FOR OLD ORDERS
------------------------
Direct-delivery stock used to be DELETED after delivery in V22. Therefore it
is NOT possible to reconstruct which exact email belonged to those historic
orders from code alone. V23 account-level warranty verification is accurate
for deliveries AFTER installation. Do not guess old account ownership.
Old orders can still be addressed manually by admin, if supporting evidence
exists elsewhere.

TEST AFTER DEPLOY
-----------------
1. Create a small test product with two UNIQUE email accounts in stock.
2. Purchase quantity 2. Check both emails appear in Replacement History.
3. Replace only email #1 with its order ID. Exactly ONE new stock account
   should be delivered and original email #1 marked Replaced.
4. Try email #1 again. Bot must refuse duplicate replacement.
5. Replace original email #2 with the SAME order ID later. It should work.
6. Try same email but WRONG customer / WRONG order ID. It must be refused.
7. Try missing order ID. The bot must request the ID.
8. For a live 18+ day warranty, inspect its age in admin and confirm it gets
   the admin contact rather than any replacement stock.

SECURITY / DEPLOYMENT NOTE
--------------------------
This full ZIP may contain production configs, log data, OAuth secrets,
account credentials, or other private content inherited from the old site.
Treat it as PRIVATE. Do not post it publicly. Rotate leaked secrets as needed.

Verification performed locally: PHP syntax lint and pure-function helper tests.
This environment does NOT have your live MySQL database or WhatsApp provider;
real transactional replacement tests must be performed after installation.
