const router = require('express').Router();
const { getSupabaseClient } = require('./supabaseClient');

// Получить все квартиры пользователя
router.get('/user-apartments', async (req, res) => {
  const supabase = getSupabaseClient();
  const { user_id } = req.query;
  if (!user_id) return res.status(400).json({ error: 'user_id required' });
  const { data: links, error: err1 } = await supabase.from('apartment_residents').select('apartment_id').eq('user_id', user_id);
  if (err1) return res.status(400).json({ error: err1.message });
  const apartmentIds = links.map(l => l.apartment_id);
  if (!apartmentIds.length) return res.json([]);
  const { data, error } = await supabase.from('apartments').select('*').in('id', apartmentIds);
  if (error) return res.status(400).json({ error: error.message });
  res.json(data);
});

module.exports = router; 