function page_cleanup(c){
  c.innerHTML = '<div id="cleanupRoot"><p style="text-align:center;padding:40px">Loading...</p></div>';
  loadCleanupPage();
}

function loadCleanupPage(){
  Promise.all([
    api('/api/admin/cloud-configs').catch(function(){ return []; }),
    api('/api/admin/verify/status').catch(function(){ return {}; }),
    api('/api/admin/system-configs').catch(function(){ return {}; })
  ]).then(function(res){
    var accounts = res[0] || [];
    var vStatus = res[1] || {};
    var configs = res[2] || {};
    renderCleanupPage(accounts, vStatus, configs);
  });
}

function renderCleanupPage(accounts, vStatus, configs){
  var lastVerify = vStatus.last_run ? new Date(vStatus.last_run).toLocaleString() : 'never';
  var lastCleanup = configs.cleanup_last_run || 'never';
  var h = '';

  h += '<div class="card"><div class="card-title">Storage Cleanup & Management</div>';
  h += '<p style="color:var(--sub);font-size:12px">Cloud verification, space refresh, scheduled cleanup</p></div>';

  // Quick Actions
  h += '<div class="card"><div class="card-title">Quick Actions</div>';
  h += '<div style="display:flex;flex-wrap:wrap;gap:10px;margin-bottom:12px">';
  h += '<button class="btn btn-pri" onclick="runVerifyAll()">Verify All</button>';
  h += '<button class="btn btn-pri" onclick="runStorageRefresh()">Refresh All Space</button>';
  h += '<button class="btn btn-warn" onclick="runFullCleanup()">Full Cleanup</button>';
  h += '<button class="btn btn-outline" onclick="runEmptyTrash()">Empty Trash</button>';
  h += '</div>';
  h += '<div id="actionStatus" style="font-size:12px;color:var(--sub);margin-top:8px"></div>';
  h += '<div style="font-size:12px;color:var(--sub);margin-top:4px">Last verify: ' + lastVerify + ' | Last cleanup: ' + lastCleanup + '</div></div>';

  // Account Table
  h += '<div class="card"><div class="card-title">Account Verification</div>';
  h += '<div style="overflow-x:auto"><table style="width:100%;border-collapse:collapse;font-size:13px">';
  h += '<thead><tr style="background:var(--bg);border-bottom:2px solid var(--border);text-align:left">';
  h += '<th style="padding:8px 10px">Type</th><th style="padding:8px 10px">Nickname</th>';
  h += '<th style="padding:8px 10px">Status</th><th style="padding:8px 10px">Space</th>';
  h += '<th style="padding:8px 10px">Action</th></tr></thead><tbody>';

  accounts.forEach(function(cfg){
    var v = cfg.verification_status;
    var vIcon = v==='valid'?'OK':(v==='invalid'?'FAIL':'PENDING');
    var space = cfg.storage_used ? (cfg.storage_used + ' / ' + (cfg.storage_total||'?')) : '-';
    h += '<tr style="border-bottom:1px solid var(--border)">';
    h += '<td style="padding:8px 10px">' + (cfg.cloud_type||'?') + '</td>';
    h += '<td style="padding:8px 10px">' + (cfg.nickname||'-') + '</td>';
    h += '<td style="padding:8px 10px">' + vIcon + '</td>';
    h += '<td style="padding:8px 10px;font-size:12px;color:var(--sub)">' + space + '</td>';
    h += '<td style="padding:8px 10px"><button class="btn btn-sm btn-outline" onclick="runSingleVerify('+cfg.id+',this)">Verify</button></td></tr>';
  });

  if(accounts.length===0){
    h += '<tr><td colspan="5" style="padding:20px;text-align:center;color:var(--sub)">No accounts found</td></tr>';
  }
  h += '</tbody></table></div></div>';

  // Cleanup Config
  h += '<div class="card"><div class="card-title">Cleanup Config</div>';
  h += '<div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;font-size:13px">';
  h += cfgRow('Cleanup Enabled', 'cleanup_enabled', configs, 'checkbox');
  h += cfgRow('File Retention (days)', 'cleanup_file_retention_days', configs, 'number', '7');
  h += cfgRow('Log Retention (days)', 'cleanup_log_retention_days', configs, 'number', '30');
  h += cfgRow('Empty Trash After', 'cleanup_empty_trash', configs, 'checkbox');
  h += cfgRow('Space Threshold Cleanup', 'cleanup_space_threshold_enabled', configs, 'checkbox');
  h += cfgRow('Threshold %', 'cleanup_space_threshold_percent', configs, 'number', '90');
  h += cfgRow('Delete %', 'cleanup_space_threshold_delete_percent', configs, 'number', '10');
  h += '</div>';
  h += '<button class="btn btn-pri" style="margin-top:12px" onclick="saveCleanupConfig()">Save Config</button></div>';

  document.getElementById('cleanupRoot').innerHTML = h;
}

function cfgRow(label, key, configs, type, placeholder){
  var val = configs[key] || '';
  if(type==='checkbox'){
    var chk = val==='true'?'checked':'';
    return '<div><label style="display:flex;align-items:center;gap:8px;cursor:pointer"><input type="checkbox" class="cleanupCfg" data-key="'+key+'" '+chk+'><span>'+label+'</span></label></div>';
  }
  return '<div><label style="font-size:12px;color:var(--sub)">'+label+'</label><input class="form-control cleanupCfg" data-key="'+key+'" value="'+val+'" placeholder="'+placeholder+'" style="max-width:120px"></div>';
}

function runVerifyAll(){
  var el = document.getElementById('actionStatus');
  el.innerHTML = 'Verifying all accounts...';
  api('/api/admin/verify/run', {method:'POST'}).then(function(r){
    el.innerHTML = 'Done: '+(r.ok||0)+'/'+r.total+' OK, '+r.failed+' failed';
    setTimeout(loadCleanupPage, 2000);
  }).catch(function(e){
    el.innerHTML = 'Verify failed: '+(e.message||'network error');
  });
}

function runSingleVerify(id, btn){
  btn.disabled = true; btn.textContent = '...';
  api('/api/admin/verify/single/'+id, {method:'POST'}).then(function(r){
    btn.textContent = r.success ? 'OK' : 'FAIL';
  }).catch(function(){ btn.textContent = 'ERR'; })
  .finally(function(){ setTimeout(loadCleanupPage, 1500); });
}

function runStorageRefresh(){
  var el = document.getElementById('actionStatus');
  el.innerHTML = 'Refreshing space info...';
  api('/api/admin/storage/refresh', {method:'POST'}).then(function(){
    el.innerHTML = 'Space refreshed';
    setTimeout(loadCleanupPage, 2000);
  }).catch(function(e){
    el.innerHTML = 'Refresh failed: '+(e.message||'network error');
  });
}

function runFullCleanup(){
  var el = document.getElementById('actionStatus');
  el.innerHTML = 'Running full cleanup...';
  api('/api/admin/cleanup/run', {method:'POST'}).then(function(r){
    el.innerHTML = (r.message||'Cleanup done');
    setTimeout(loadCleanupPage, 2000);
  }).catch(function(e){
    el.innerHTML = 'Cleanup failed: '+(e.message||'network error');
  });
}

function runEmptyTrash(){
  var el = document.getElementById('actionStatus');
  if(!confirm('Empty all trash? This cannot be undone.')) return;
  el.innerHTML = 'Emptying trash...';
  api('/api/admin/cleanup/empty-trash', {method:'POST'}).then(function(r){
    el.innerHTML = r.emptied ? 'Trash emptied' : (r.message||'Done');
  }).catch(function(e){
    el.innerHTML = 'Failed: '+(e.message||'network error');
  });
}

function saveCleanupConfig(){
  var entries = [];
  document.querySelectorAll('.cleanupCfg').forEach(function(el){
    var key = el.dataset.key;
    var val = el.type==='checkbox' ? (el.checked?'true':'false') : el.value.trim();
    entries.push({key:key, value:val});
  });
  api('/api/admin/system-configs', {
    method:'PUT', headers:{'Content-Type':'application/json'},
    body:JSON.stringify({entries:entries})
  }).then(function(){
    toast('Config saved');
  }).catch(function(e){
    toast('Save failed: '+(e.message||''));
  });
}
