<?php
require_once dirname(__DIR__) . '/_app_guard.php';
app_guard_validate();
header("Content-Type: application/json");

$db = new PDO("sqlite:db.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$mac = strtoupper(trim($_POST["mac"] ?? ""));
$mac = preg_replace("/[^A-F0-9]/", "", $mac);
$mac = implode(":", str_split(substr($mac,0,12),2));

$stmt = $db->prepare("SELECT dns FROM usuarios WHERE mac=? LIMIT 1");
$stmt->execute([$mac]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$row){
    echo json_encode(["ok"=>false]);
    exit;
}

echo json_encode([
    "ok"=>true,
    "dns"=>rtrim($row["dns"],"/")
]);
