<?php
header("Content-Type: application/json");
require_once 'config/koneksi.php';

$input = json_decode(file_get_contents("php://input"), true);

$store_id = $input['store_id'] ?? '';

if (!$store_id) {
    echo json_encode([
        "success" => false,
        "message" => "store_id wajib"
    ]);
    exit;
}

/// 🔥 AMBIL LAST UPDATE DARI SEMUA MASTER
$query = "
SELECT MAX(updated_at) as last_update FROM (
    SELECT updated_at FROM products WHERE store_id = '$store_id'
    UNION ALL
    SELECT updated_at FROM categories WHERE store_id = '$store_id'
    UNION ALL
    SELECT updated_at FROM tables WHERE store_id = '$store_id'
    UNION ALL
    SELECT updated_at FROM users WHERE store_id = '$store_id'
) as master_data
";

$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

$version = $row['last_update'] ?? date('Y-m-d H:i:s');

echo json_encode([
    "success" => true,
    "version" => $version
]);