<?php
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';

if ($_SESSION['role_id'] != 1) {
    $_SESSION['error'] = 'Only Super Admin can remove demo data.';
    header('Location: ' . getRedirectUrl());
    exit;
}

$pdo = getDB();
header('Content-Type: application/json');

try {
    // Disable FK checks to allow deletion in any order
    $pdo->exec("SET FOREIGN_KEY_CHECKS = 0");
    $pdo->beginTransaction();

    // Delete from tables that may reference products, customers, etc.
    // Order no longer matters since FK checks are disabled,
    // but we keep logical grouping for clarity.

    // 1. Returns
    $pdo->exec("DELETE FROM return_items WHERE is_demo = 1");
    $pdo->exec("DELETE FROM returns_ WHERE is_demo = 1");

    // 2. Warranty
    $pdo->exec("DELETE FROM warranty_claims WHERE is_demo = 1");
    $pdo->exec("DELETE FROM warranty_records WHERE is_demo = 1");

    // 3. Repair
    $pdo->exec("DELETE FROM repair_job_parts WHERE is_demo = 1");
    $pdo->exec("DELETE FROM repair_job_status_history WHERE is_demo = 1");
    $pdo->exec("DELETE FROM repair_jobs WHERE is_demo = 1");

    // 4. Sales
    $pdo->exec("DELETE FROM sales_invoice_items WHERE is_demo = 1");
    $pdo->exec("DELETE FROM sales_invoices WHERE is_demo = 1");

    // 5. Purchases
    $pdo->exec("DELETE FROM purchase_items WHERE is_demo = 1");
    $pdo->exec("DELETE FROM purchases WHERE is_demo = 1");

    // 6. GRN (new tables - may not have is_demo but safe to try)
    try { $pdo->exec("DELETE FROM grn_item_imeis WHERE is_demo = 1"); } catch (Exception $e) { /* column may not exist */ }
    try { $pdo->exec("DELETE FROM grn_items WHERE is_demo = 1"); } catch (Exception $e) { /* column may not exist */ }
    try { $pdo->exec("DELETE FROM grns WHERE is_demo = 1"); } catch (Exception $e) { /* column may not exist */ }

    // 7. Payments
    $pdo->exec("DELETE FROM payments WHERE is_demo = 1");

    // 8. Product tracking
    $pdo->exec("DELETE FROM product_imei WHERE is_demo = 1");
    $pdo->exec("DELETE FROM stock_movements WHERE is_demo = 1");

    // 9. Products (parent of many FKs - now safe because children are deleted)
    $pdo->exec("DELETE FROM products WHERE is_demo = 1");

    // 10. Expenses
    $pdo->exec("DELETE FROM expenses WHERE is_demo = 1");
    $pdo->exec("DELETE FROM expense_categories WHERE is_demo = 1");

    // 11. Suppliers
    $pdo->exec("DELETE FROM suppliers WHERE is_demo = 1");

    // 12. Customers
    $pdo->exec("DELETE FROM customers WHERE is_demo = 1");

    // 13. Brands
    $pdo->exec("DELETE FROM brands WHERE is_demo = 1");

    // 14. Categories
    $pdo->exec("DELETE FROM categories WHERE is_demo = 1");

    // 15. Notification templates (if demo)
    try { $pdo->exec("DELETE FROM notification_templates WHERE is_demo = 1"); } catch (Exception $e) { /* column may not exist */ }

    $pdo->commit();

    // Re-enable FK checks
    $pdo->exec("SET FOREIGN_KEY_CHECKS = 1");

    echo json_encode(['success' => true, 'message' => 'All demo data removed successfully.']);

} catch (Exception $e) {
    $pdo->rollBack();
    // Ensure FK checks are re-enabled even on error
    try { $pdo->exec("SET FOREIGN_KEY_CHECKS = 1"); } catch (Exception $e2) {}
    echo json_encode(['success' => false, 'message' => 'Error removing demo data: ' . $e->getMessage()]);
}
