// Fija limites de los buckets de storage (P1 auditoria 2026-07-22).
// La config de buckets vive en el servicio Storage, no en migraciones SQL, asi que
// se aplica por la Storage API con service_role. Idempotente. Uso:
//   SUPABASE_SERVICE_KEY=... node scripts/set_bucket_limits.mjs
const URL = process.env.SUPABASE_URL || 'https://szzhhlevtrcambcoytiz.supabase.co';
const K = process.env.SUPABASE_SERVICE_KEY;
if (!K) { console.log('Falta SUPABASE_SERVICE_KEY'); process.exit(1); }

const MB5 = 5 * 1024 * 1024;
const buckets = [
  { id: 'menu', public: true, file_size_limit: MB5, allowed_mime_types: ['image/jpeg', 'image/png', 'image/webp', 'image/gif'] },
  { id: 'comprobantes', public: false, file_size_limit: MB5, allowed_mime_types: ['image/jpeg', 'image/png', 'image/webp', 'application/pdf'] },
];

for (const b of buckets) {
  const r = await fetch(`${URL}/storage/v1/bucket/${b.id}`, {
    method: 'PUT',
    headers: { apikey: K, Authorization: `Bearer ${K}`, 'Content-Type': 'application/json' },
    body: JSON.stringify(b),
  });
  console.log(`${b.id}: ${r.status} ${(await r.text()).slice(0, 80)}`);
}
