import api from "./axios";

/**
 * Get all households for the current user
 */
export const getUserHouseholds = () => api.get("/households");

/**
 * Get details of a specific household
 */
export const getHousehold = (householdId) => api.get(`/households/${householdId}`);

/**
 * Create a new household
 */
export const createHousehold = (name) => api.post("/households", { name });

/**
 * Update household name
 */
export const updateHousehold = (householdId, name) =>
  api.patch(`/households/${householdId}`, { name });

/**
 * Delete a household
 */
export const deleteHousehold = (householdId) =>
  api.delete(`/households/${householdId}`);

/**
 * Refresh household invite code
 */
export const refreshInviteCode = (householdId) =>
  api.post(`/households/${householdId}/invite/refresh`);

/**
 * Join a household using invite code
 */
export const joinHousehold = (inviteCode) =>
  api.post(`/households/join/${inviteCode}`);

/**
 * Get household members
 */
export const getHouseholdMembers = (householdId) =>
  api.get(`/households/${householdId}/members`);

/**
 * Update member role
 */
export const updateMemberRole = (householdId, userId, role) =>
  api.patch(`/households/${householdId}/members/${userId}/role`, { role });

/**
 * Remove member from household
 */
export const removeMember = (householdId, userId) =>
  api.delete(`/households/${householdId}/members/${userId}`);

function groupHeaders(groupId) {
  return {
    headers: {
      "x-group-id": String(groupId),
    },
  };
}

export const getGroupInviteLinks = (groupId) =>
  api.get("/api/groups/invites", groupHeaders(groupId));

export const createGroupInviteLink = (groupId, payload) =>
  api.post("/api/groups/invites", payload, groupHeaders(groupId));

export const revokeGroupInviteLink = (groupId, linkId) =>
  api.post("/api/groups/invites/revoke", { linkId }, groupHeaders(groupId));

export const reviveGroupInviteLink = (groupId, linkId, ttlDays) =>
  api.post("/api/groups/invites/revive", { linkId, ttlDays }, groupHeaders(groupId));

export const deleteGroupInviteLink = (groupId, linkId) =>
  api.post("/api/groups/invites/delete", { linkId }, groupHeaders(groupId));

export const getGroupJoinPolicy = (groupId) =>
  api.get("/api/groups/join-policy", groupHeaders(groupId));

export const setGroupJoinPolicy = (groupId, joinPolicy) =>
  api.post("/api/groups/join-policy", { joinPolicy }, groupHeaders(groupId));

export const getInviteLinkSummary = (token) =>
  api.get(`/api/invite-links/${encodeURIComponent(token)}`);

export const acceptInviteLink = (token) =>
  api.post(`/api/invite-links/${encodeURIComponent(token)}`);
