const { RouterOSClient } = require('routeros-client');
const fs = require('fs');

async function test() {
  const host = "sg-6.hostddns.us";
  const port = 47808;
  const user = "mymikrobil";
  const pass = "241020";

  try {
    const data = JSON.parse(fs.readFileSync('dist/data/admin@mikrobil.com.json', 'utf8'));
    const localVouchers = data.vouchers || [];
    console.log(`Local vouchers in DB: ${localVouchers.length}`);

    const client = new RouterOSClient({
      host: host,
      port: port,
      user: user,
      password: pass,
      timeout: 10,
      keepalive: false
    });
    await client.connect();
    console.log("Connected to MikroTik!");

    const mikrotikUsers = await client.api().menu("/ip/hotspot/user").get();
    console.log(`Total users on MikroTik: ${mikrotikUsers.length}`);

    const mikrotikUserMap = new Map();
    mikrotikUsers.forEach(u => {
      mikrotikUserMap.set(String(u.name), u);
    });

    const missing = [];
    localVouchers.forEach(v => {
      const code = String(v.code);
      if (!mikrotikUserMap.has(code)) {
        missing.push(v);
      }
    });

    console.log(`\nTotal missing vouchers: ${missing.length}`);
    if (missing.length > 0) {
      console.log("First 20 missing vouchers:");
      missing.slice(0, 20).forEach(v => {
        console.log(` - Code: ${v.code}, Profile: ${v.profileName}, Comment: ${v.comment}, Created At: ${v.created_at}`);
      });
    } else {
      console.log("All local vouchers are present on MikroTik!");
    }

    await client.close();
  } catch (err) {
    console.log("Error:", err.message);
  }
}

test();
