const db = require("../config/database"); class ShoppingListRepository { getByHousehold(householdId) { return db.prepare(` SELECT shopping_lists.*, COUNT(shopping_list_items.id) as item_count, SUM(CASE WHEN shopping_list_items.checked = 1 THEN 1 ELSE 0 END) as checked_count FROM shopping_lists LEFT JOIN shopping_list_items ON shopping_lists.id = shopping_list_items.list_id WHERE shopping_lists.household_id = ? AND shopping_lists.archived = 0 GROUP BY shopping_lists.id ORDER BY shopping_lists.created_at DESC `).all(householdId); } getById(id) { return db.prepare(` SELECT * FROM shopping_lists WHERE id = ? `).get(id); } create(data) { return db.prepare(` INSERT INTO shopping_lists (household_id, name) VALUES (?, ?) `).run(data.household_id, data.name); } getItems(listId) { return db.prepare(` SELECT shopping_list_items.*, products.name as product_name, categories.name as category_name, categories.icon as category_icon FROM shopping_list_items JOIN products ON products.id = shopping_list_items.product_id LEFT JOIN categories ON categories.id = products.category_id WHERE shopping_list_items.list_id = ? ORDER BY shopping_list_items.checked ASC, shopping_list_items.sort_order ASC `).all(listId); } addItem(data) { return db.prepare(` INSERT INTO shopping_list_items (list_id, product_id, amount) VALUES (?, ?, ?) `).run(data.list_id, data.product_id, data.amount); } findItem(listId, itemId) { return db.prepare(` SELECT id FROM shopping_list_items WHERE id = ? AND list_id = ? `).get(itemId, listId); } toggleItem(itemId) { return db.prepare(` UPDATE shopping_list_items SET checked = CASE WHEN checked = 1 THEN 0 ELSE 1 END WHERE id = ? `).run(itemId); } deleteItem(itemId) { return db.prepare(` DELETE FROM shopping_list_items WHERE id = ? `).run(itemId); } archiveList(listId) { return db.prepare(` UPDATE shopping_lists SET archived = 1 WHERE id = ? `).run(listId); } } module.exports = new ShoppingListRepository();