<?php
// Compatibility receiver for older ESP32 code.
// Recommended endpoint: /dustbin/api/update_bin.php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require __DIR__ . '/api/update_bin.php';
    exit;
}

if (isset($_GET['filled']) || isset($_GET['weight']) || isset($_GET['latest'])) {
    $file = __DIR__ . '/api/latest_bin_data.json';
    $data = file_exists($file) ? json_decode(file_get_contents($file), true) : [];

    if (isset($_GET['filled'])) {
        echo isset($data['fill_percent']) ? round($data['fill_percent']) : 0;
        exit;
    }

    if (isset($_GET['weight'])) {

        exit;
    }

    header('Content-Type: application/json');
    echo json_encode($data ?: ['status' => 'waiting']);
    exit;
}

header('Location: index.html');
exit;
?>
