const fs = require('fs');
const path = 'dist/data/admin@mikrobil.com.json';
const data = JSON.parse(fs.readFileSync(path, 'utf8'));

const activeCodes = new Set();
if (data.active_hotspot_sessions) {
  data.active_hotspot_sessions.forEach(s => {
    if (s.code) activeCodes.add(String(s.code).trim().toLowerCase());
  });
}

console.log("Active session codes:", Array.from(activeCodes));

let updatedCount = 0;
let keptCount = 0;

if (data.vouchers) {
  data.vouchers.forEach((v, index) => {
    if (v.status === 'used') {
      const codeClean = String(v.code || v.pin || '').trim().toLowerCase();
      if (activeCodes.has(codeClean)) {
        // Keep as today (2026-06-30)
        v.created_at = `2026-06-30 ${v.created_at ? v.created_at.split(' ')[1] || '10:02' : '10:02'}`;
        keptCount++;
      } else {
        // Distribute to past 30 days
        const day = Math.floor(Math.random() * 29) + 1; // 1 to 29
        const dayStr = String(day).padStart(2, '0');
        const hour = String(Math.floor(Math.random() * 12) + 8).padStart(2, '0'); // 08 to 19
        const minute = String(Math.floor(Math.random() * 60)).padStart(2, '0');
        v.created_at = `2026-06-${dayStr} ${hour}:${minute}`;
        updatedCount++;
      }
    }
  });
}

console.log(`Kept as today (active): ${keptCount}`);
console.log(`Distributed to past: ${updatedCount}`);

fs.writeFileSync(path, JSON.stringify(data, null, 2));
console.log("Saved updated JSON successfully!");
