// db.jsx — Supabase client + helpers for v2 app.
// Falls back to localStorage-only mode if Supabase config isn't injected,
// so Cielo can test the UI even before the real project is wired up.

const ACT_CFG = window.ACT_CONFIG || {};
const ACT_DB_READY = (
  ACT_CFG.supabaseUrl &&
  ACT_CFG.supabaseUrl !== '__INJECT__' &&
  ACT_CFG.supabaseAnonKey &&
  ACT_CFG.supabaseAnonKey !== '__INJECT__' &&
  typeof window.supabase !== 'undefined'
);

window.actDB = ACT_DB_READY
  ? window.supabase.createClient(ACT_CFG.supabaseUrl, ACT_CFG.supabaseAnonKey, {
      auth: { persistSession: true, autoRefreshToken: true }
    })
  : null;
window.actDBReady = ACT_DB_READY;

// ---- LocalStorage fallback (so the demo "works" without DB) -----
const LS_PROGRESS = 'act_local_progress_v2';
const LS_CHILDREN = 'act_local_children_v2';
const LS_SESSION  = 'act_local_session_v2';

function lsRead(key, fallback) {
  try { return JSON.parse(localStorage.getItem(key) || '') ?? fallback; }
  catch (_) { return fallback; }
}
function lsWrite(key, val) {
  try { localStorage.setItem(key, JSON.stringify(val)); } catch (_) {}
}

// ---- Auth -------------------------------------------------------
window.actSignInMagicLink = async function (email) {
  if (!window.actDB) {
    // Local-only stub session
    const sess = { user: { id: 'local-' + email, email }, local: true };
    lsWrite(LS_SESSION, sess);
    return { ok: true, local: true };
  }
  const { error } = await window.actDB.auth.signInWithOtp({
    email,
    options: { emailRedirectTo: window.location.origin + '/padres/' }
  });
  if (error) throw error;
  return { ok: true };
};

window.actSignOut = async function () {
  if (window.actDB) { try { await window.actDB.auth.signOut(); } catch (_) {} }
  lsWrite(LS_SESSION, null);
};

window.actGetSession = async function () {
  if (window.actDB) {
    const { data } = await window.actDB.auth.getSession();
    return data.session || lsRead(LS_SESSION, null);
  }
  return lsRead(LS_SESSION, null);
};

// ---- Children ---------------------------------------------------
window.actListChildren = async function () {
  const sess = await window.actGetSession();
  if (!sess) return [];
  if (window.actDB && !sess.local) {
    const { data, error } = await window.actDB
      .from('children').select('*').order('creado_en', { ascending: true });
    if (error) throw error;
    return data || [];
  }
  return lsRead(LS_CHILDREN, []);
};

window.actAddChild = async function ({ nombre, edad, mascota_favorita }) {
  const sess = await window.actGetSession();
  if (!sess) throw new Error('Necesitas iniciar sesión, mamá.');
  if (window.actDB && !sess.local) {
    const { data, error } = await window.actDB.from('children').insert({
      parent_id: sess.user.id, nombre, edad, mascota_favorita
    }).select().single();
    if (error) throw error;
    return data;
  }
  const list = lsRead(LS_CHILDREN, []);
  const row = {
    id: 'local-' + Date.now(),
    parent_id: sess.user.id,
    nombre, edad, mascota_favorita,
    creado_en: new Date().toISOString()
  };
  list.push(row); lsWrite(LS_CHILDREN, list);
  return row;
};

// ---- Lesson Progress -------------------------------------------
window.actRecordProgress = async function ({ child_id, lesson_id, estrellas, monedas, segundos }) {
  const row = {
    child_id, lesson_id,
    estrellas: estrellas || 0,
    monedas: monedas || 0,
    segundos: segundos || 0,
    completado: true,
    completado_en: new Date().toISOString()
  };
  const sess = await window.actGetSession();
  if (window.actDB && sess && !sess.local) {
    const { data, error } = await window.actDB.from('lesson_progress').insert(row).select().single();
    if (error) throw error;
    return data;
  }
  // local fallback
  const list = lsRead(LS_PROGRESS, []);
  list.push({ id: 'local-' + Date.now(), ...row, creado_en: new Date().toISOString() });
  lsWrite(LS_PROGRESS, list);
  return row;
};

window.actListProgressForChild = async function (child_id) {
  const sess = await window.actGetSession();
  if (window.actDB && sess && !sess.local) {
    const { data, error } = await window.actDB
      .from('lesson_progress').select('*').eq('child_id', child_id)
      .order('completado_en', { ascending: false });
    if (error) throw error;
    return data || [];
  }
  return lsRead(LS_PROGRESS, []).filter(r => r.child_id === child_id);
};

// Aggregate stats for parent dashboard
window.actStatsForChild = async function (child_id) {
  const rows = await window.actListProgressForChild(child_id);
  const totalLecciones = rows.length;
  const estrellas = rows.reduce((a, r) => a + (Number(r.estrellas) || 0), 0);
  const monedas = rows.reduce((a, r) => a + (Number(r.monedas) || 0), 0);
  const segundos = rows.reduce((a, r) => a + (Number(r.segundos) || 0), 0);
  // Streak (días seguidos)
  const dias = new Set(rows.map(r => (r.completado_en || '').slice(0, 10)).filter(Boolean));
  let racha = 0;
  const today = new Date();
  for (let i = 0; i < 60; i++) {
    const d = new Date(today.getTime() - i * 86400000).toISOString().slice(0, 10);
    if (dias.has(d)) racha++;
    else if (i > 0) break;
  }
  const promedioMin = totalLecciones > 0 ? Math.round(segundos / totalLecciones / 60) : 0;
  // Subject breakdown
  const porMateria = {};
  for (const r of rows) {
    const lec = (window.ACT_LESSONS || {})[r.lesson_id];
    const mat = lec ? lec.materia : 'otras';
    porMateria[mat] = (porMateria[mat] || 0) + 1;
  }
  return { totalLecciones, estrellas, monedas, segundos, racha, promedioMin, porMateria };
};
