const express = require("express");
const router = express.Router();
const controller = require("../controllers/households.controller");
const listsController = require("../controllers/lists.controller.v2");
const auth = require("../middleware/auth");
const {
  householdAccess,
  requireHouseholdAdmin,
  storeAccess,
} = require("../middleware/household");
const { upload, processImage } = require("../middleware/image");

// Public routes (authenticated only)
router.get("/", auth, controller.getUserHouseholds);
router.post("/", auth, controller.createHousehold);
router.post("/join/:inviteCode", auth, controller.joinHousehold);

// Household-scoped routes (member access required)
router.get("/:householdId", auth, householdAccess, controller.getHousehold);
router.patch(
  "/:householdId",
  auth,
  householdAccess,
  requireHouseholdAdmin,
  controller.updateHousehold
);
router.delete(
  "/:householdId",
  auth,
  householdAccess,
  requireHouseholdAdmin,
  controller.deleteHousehold
);
router.post(
  "/:householdId/invite/refresh",
  auth,
  householdAccess,
  requireHouseholdAdmin,
  controller.refreshInviteCode
);

// Member management routes
router.get(
  "/:householdId/members",
  auth,
  householdAccess,
  controller.getMembers
);
router.patch(
  "/:householdId/members/:userId/role",
  auth,
  householdAccess,
  requireHouseholdAdmin,
  controller.updateMemberRole
);
router.delete(
  "/:householdId/members/:userId",
  auth,
  householdAccess,
  controller.removeMember
);

// ==================== List Operations Routes ====================
// All list routes require household access AND store access

// Get grocery list
router.get(
  "/:householdId/stores/:storeId/list",
  auth,
  householdAccess,
  storeAccess,
  listsController.getList
);

// Get specific item by name
router.get(
  "/:householdId/stores/:storeId/list/item",
  auth,
  householdAccess,
  storeAccess,
  listsController.getItemByName
);

// Add item to list
router.post(
  "/:householdId/stores/:storeId/list/add",
  auth,
  householdAccess,
  storeAccess,
  upload.single("image"),
  processImage,
  listsController.addItem
);

// Mark item as bought/unbought
router.patch(
  "/:householdId/stores/:storeId/list/item",
  auth,
  householdAccess,
  storeAccess,
  listsController.markBought
);

// Update item details (quantity, notes)
router.put(
  "/:householdId/stores/:storeId/list/item",
  auth,
  householdAccess,
  storeAccess,
  listsController.updateItem
);

// Delete item
router.delete(
  "/:householdId/stores/:storeId/list/item",
  auth,
  householdAccess,
  storeAccess,
  listsController.deleteItem
);

// Get suggestions
router.get(
  "/:householdId/stores/:storeId/list/suggestions",
  auth,
  householdAccess,
  storeAccess,
  listsController.getSuggestions
);

// Get recently bought items
router.get(
  "/:householdId/stores/:storeId/list/recent",
  auth,
  householdAccess,
  storeAccess,
  listsController.getRecentlyBought
);

// Get item classification
router.get(
  "/:householdId/stores/:storeId/list/classification",
  auth,
  householdAccess,
  storeAccess,
  listsController.getClassification
);

// Set item classification
router.post(
  "/:householdId/stores/:storeId/list/classification",
  auth,
  householdAccess,
  storeAccess,
  listsController.setClassification
);

// Update item image
router.post(
  "/:householdId/stores/:storeId/list/update-image",
  auth,
  householdAccess,
  storeAccess,
  upload.single("image"),
  processImage,
  listsController.updateItemImage
);

module.exports = router;
