const loginBtn = document.getElementById('loginBtn');
const loginSection = document.getElementById('loginSection');
const adminSection = document.getElementById('adminSection');
const loginStatus = document.getElementById('loginStatus');

async function refreshLists(){
  const ts = await SSAPI.get('/tournaments').catch(()=>[]);
  const ms = await SSAPI.get('/matches').catch(()=>[]);
  document.getElementById('adminTournaments').textContent = JSON.stringify(ts,null,2);
  document.getElementById('adminMatches').textContent = JSON.stringify(ms,null,2);
}

loginBtn.onclick = async () => {
  const email = document.getElementById('email').value;
  const password = document.getElementById('password').value;
  try {
    const { token } = await SSAPI.post('/auth/login', { email, password });
    SSAPI.setToken(token);
    loginStatus.textContent = 'Connecté ✓';
    loginSection.style.display = 'none';
    adminSection.style.display = 'block';
    await refreshLists();
  } catch (e) {
    loginStatus.textContent = 'Erreur: ' + e.message;
  }
};

window.createTournament = async () => {
  const name = document.getElementById('t_name').value;
  const location = document.getElementById('t_loc').value;
  const start_date = document.getElementById('t_sd').value;
  const end_date = document.getElementById('t_ed').value;
  await SSAPI.post('/tournaments', { name, location, start_date, end_date });
  await refreshLists();
};

window.generateAmericano = async () => {
  const tournament_id = Number(document.getElementById('g_tid').value);
  const courts = document.getElementById('g_courts').value.split(',').map(s=>s.trim()).filter(Boolean);
  const start_at = document.getElementById('g_start').value || null;
  const interval_minutes = Number(document.getElementById('g_int').value || 20);
  const res = await SSAPI.post('/matches/generate', { tournament_id, courts, start_at, interval_minutes });
  alert('Matches créés: ' + res.created.length);
  await refreshLists();
};

window.scoreMatch = async () => {
  const id = Number(document.getElementById('m_id').value);
  const score_a = Number(document.getElementById('m_a').value);
  const score_b = Number(document.getElementById('m_b').value);
  await SSAPI.post(`/matches/${id}/score`, { score_a, score_b });
  await refreshLists();
};

// New: bulk enroll & auto teams
window.bulkEnroll = async () => {
  const tournament_id = Number(document.getElementById('b_tid').value);
  const ids = document.getElementById('b_ids').value.split(',').map(s=>Number(s.trim())).filter(Boolean);
  await SSAPI.post('/enrollments/bulk', { tournament_id, player_ids: ids });
  await refreshLists();
};
window.autoTeams = async () => {
  const tournament_id = Number(document.getElementById('b_tid').value);
  const res = await SSAPI.post('/teams/auto', { tournament_id });
  alert('Équipes créées: ' + (res.created_count || 0));
  await refreshLists();
};

// auto-show admin if token present
if (SSAPI.getToken()) {
  loginSection.style.display = 'none';
  adminSection.style.display = 'block';
  refreshLists();
}