<?php
$workspace_path = "c:\\Users\\User\\Documents\\Antigravity Projects\\Apperal Managment System -Vanila- PHP";

// Mock the environment so api/invoices.php doesn't reject us or run the switch block
session_start();
$_SESSION['user_id'] = 1;
$_SESSION['user_role'] = 'owner';
$_SESSION['role'] = 'owner';
$_SESSION['company_id'] = 1;

// Go to api folder
chdir($workspace_path . "\\api");

// Include database to fetch the legacy invoice
require_once '../includes/db.php';

$id = 4;
$invoice = fetch("SELECT * FROM production_invoices WHERE id = ?", [$id]);

if ($invoice) {
    echo "--- Legacy Invoice ID {$id} ---\n";
    echo "Raw details:\n{$invoice['product_details']}\n\n";
    
    // We can define the action as something not in the switch block, or list, but we can also just include a mock action
    $_GET['action'] = 'none'; // this prevents the switch from executing or echoing anything since 'none' does not exist in the switch and break is hit
    
    // Include invoices.php to get access to parse_products
    ob_start();
    require_once 'invoices.php';
    ob_end_clean();
    
    if (function_exists('parse_products')) {
        $parsed = parse_products($invoice['product_details'], $invoice['units_completed'], $invoice['unit_price']);
        echo "Parsed products array:\n";
        print_r($parsed);
    } else {
        echo "parse_products function not defined!\n";
    }
} else {
    echo "Invoice ID {$id} not found.\n";
}
?>
