/**
 * MSW handlers for user management (account admin) endpoints.
 * @module mocks/handlers/users
 */
import { http, HttpResponse } from "msw";
import usersData from "../fixtures/users.json";
import userDetailData from "../fixtures/user-detail.json";
import { captureRequest } from "../request-log.js";

export const usersHandlers = [
  http.get("/api/users", ({ request }) => {
    captureRequest("GET", "/api/users");
    return HttpResponse.json(usersData);
  }),

  http.post("/api/users", async ({ request }) => {
    const body = await request.json();
    captureRequest("POST", "/api/users", body);
    return HttpResponse.json({ success: true });
  }),

  // User roles (must be before /api/users/:username to match first)
  http.put("/api/users/:username/roles/:role", async ({ request, params }) => {
    captureRequest("PUT", `/api/users/${params.username}/roles/${params.role}`);
    return HttpResponse.json({ success: true });
  }),

  http.delete("/api/users/:username/roles/:role", ({ request, params }) => {
    captureRequest("DELETE", `/api/users/${params.username}/roles/${params.role}`);
    return HttpResponse.json({ success: true });
  }),

  // User CRUD (after roles so roles match first)
  http.get("/api/users/:username", ({ request, params }) => {
    captureRequest("GET", `/api/users/${params.username}`);
    return HttpResponse.json(userDetailData);
  }),

  http.put("/api/users/:username", async ({ request, params }) => {
    const body = await request.json();
    captureRequest("PUT", `/api/users/${params.username}`, body);
    return HttpResponse.json({ success: true });
  }),

  http.delete("/api/users/:username", ({ request, params }) => {
    captureRequest("DELETE", `/api/users/${params.username}`);
    return HttpResponse.json({ success: true });
  }),
];
