<?php
session_start();
require 'routeros_api.class.php';

if (!isset($_SESSION['ip'])) {
    die(json_encode(['error' => 'Unauthorized']));
}

$mt_ip = $_SESSION['ip'];
$safe_ip = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', $mt_ip);
$CACHE_DIR = __DIR__ . '/cache';

// Hapus semua cache untuk router ini
$pattern = $CACHE_DIR . '/ap_*_' . $safe_ip . '.cache';
$deleted = 0;
foreach (glob($pattern) as $file) {
    if (@unlink($file)) $deleted++;
}

echo json_encode([
    'ok' => true,
    'message' => 'Cache cleared: ' . $deleted . ' files',
    'deleted' => $deleted
]);
?>