<?php
// api/update_checker.php
session_start();
require_once 'db.php'; // Includes SYSTEM_VERSION

header('Content-Type: application/json');

$action = $_GET['action'] ?? '';

if ($action === 'check') {
    // In a real scenario, this would call a remote API:
    // $remote_url = "https://updates.justsale.co.tz/latest_version.json";
    // $remote_data = @file_get_contents($remote_url);
    // ...
    
    // For this demonstration, we'll simulate the check.
    // We'll use a mocked "latest version". 
    // If the user wants to test it, they can change this value or we can make it dynamic.
    
    // TO USER: This is where the actual update server URL would go.
    $latest_version = "3.0.0"; // New version
    
    $has_update = version_compare($latest_version, SYSTEM_VERSION, '>');

    $changelog = '<div style="font-family: sans-serif; line-height: 1.5; color: #333;"><h5 style="color: #4361ee; font-weight: 800; margin-bottom: 10px;">🚀 JUSTSALE POS v3.0.0 - BATCHES & EXPIRY IS HERE!</h5><p style="margin-bottom: 15px;">This version introduces powerful inventory tracking for batches and expiry dates, along with refined layouts for both desktop and mobile.</p><h6 style="font-weight: 800; margin-bottom: 5px; text-transform: uppercase; font-size: 11px; color: #666;">🔥 What’s New?</h6><ul style="padding-left: 20px; margin-bottom: 15px;"><li><strong>Product Batches</strong>: Create multiple batches for a single product with unique costs and selling prices.</li><li><strong>Expiry Date Tracking</strong>: Know exactly when items expire and manage stock efficiently.</li><li><strong>UI Refinements</strong>: Updated headers and footers across all modules for a smoother feel.</li><li><strong>Mobile POS Improvements</strong>: Enhanced barcode handling and batch selection during checkout.</li></ul><h6 style="font-weight: 800; margin-bottom: 5px; text-transform: uppercase; font-size: 11px; color: #666;">🛠️ How to Update</h6><ol style="padding-left: 20px; margin-bottom: 0;"><li><strong>Backup</strong>: Secure your database first.</li><li><strong>Upload</strong>: Replace files with the <code>v3.0.0</code> package.</li><li><strong>Finalize</strong>: Click the green <strong>"Finish Update"</strong> button below to sync your database.</li></ol></div>';

    echo json_encode([
        'success' => true,
        'current_version' => SYSTEM_VERSION,
        'latest_version' => $latest_version,
        'has_update' => $has_update,
        'changelog' => $changelog,
        'message' => $has_update ? "A new version ($latest_version) is available!" : "System is up to date."
    ]);
    exit;
}

echo json_encode(['success' => false, 'message' => 'Invalid action']);
?>


