import { beforeEach, describe, expect, test, vi } from "vitest";
import type { TApiAuditLog } from "@/app/lib/api/with-api-logging";
import type { authenticatedApiClient } from "@/modules/api/v2/auth/authenticated-api-client";
import { UNKNOWN_DATA } from "@/modules/ee/audit-logs/types/audit-log";
import { DELETE, PUT } from "./route";

const {
  mockAuthenticatedApiClient,
  mockCanManageOrganizationUsers,
  mockDeleteTeam,
  mockGetApiKeyCreatorRole,
  mockGetTeam,
  mockHasOrganizationIdAndAccess,
  mockHandleApiError,
  mockSuccessResponse,
  mockUpdateTeam,
} = vi.hoisted(() => ({
  mockAuthenticatedApiClient: vi.fn(),
  mockCanManageOrganizationUsers: vi.fn(),
  mockDeleteTeam: vi.fn(),
  mockGetApiKeyCreatorRole: vi.fn(),
  mockGetTeam: vi.fn(),
  mockHasOrganizationIdAndAccess: vi.fn(),
  mockHandleApiError: vi.fn(),
  mockSuccessResponse: vi.fn(),
  mockUpdateTeam: vi.fn(),
}));

vi.mock("@/modules/api/v2/auth/authenticated-api-client", () => ({
  authenticatedApiClient: mockAuthenticatedApiClient,
}));

vi.mock("@/modules/api/v2/lib/response", () => ({
  responses: {
    successResponse: mockSuccessResponse,
  },
}));

vi.mock("@/modules/api/v2/lib/utils", () => ({
  handleApiError: mockHandleApiError,
}));

vi.mock("@/modules/api/v2/organizations/[organizationId]/lib/utils", () => ({
  hasOrganizationIdAndAccess: mockHasOrganizationIdAndAccess,
}));

vi.mock("@/modules/api/v2/organizations/[organizationId]/teams/[teamId]/lib/teams", () => ({
  deleteTeam: mockDeleteTeam,
  getTeam: mockGetTeam,
  updateTeam: mockUpdateTeam,
}));

vi.mock("@/modules/api/v2/organizations/[organizationId]/users/lib/utils", () => ({
  canManageOrganizationUsers: mockCanManageOrganizationUsers,
  getApiKeyCreatorRole: mockGetApiKeyCreatorRole,
}));

const organizationId = "org123";
const apiKeyId = "apiKey123";
const teamId = "team123";
const team = { id: teamId, organizationId, name: "Test Team" };

const buildRequest = (method: string) =>
  new Request(`http://localhost/api/v2/organizations/org123/teams/${teamId}`, { method });

describe("PUT/DELETE /organizations/[organizationId]/teams/[teamId]", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockHasOrganizationIdAndAccess.mockResolvedValue(true);

    mockAuthenticatedApiClient.mockImplementation(
      async ({ handler }: Parameters<typeof authenticatedApiClient>[0]) =>
        await handler({
          request: buildRequest("DELETE"),
          auditLog: undefined,
          authentication: {
            type: "apiKey",
            apiKeyId,
            organizationId,
            workspacePermissions: [],
            organizationAccess: { accessControl: { read: true, write: true } },
          },
          parsedInput: {
            body: { name: "Renamed Team" },
            params: { organizationId, teamId },
          },
        })
    );
    mockHandleApiError.mockImplementation((_request, error) => Response.json({ error }, { status: 403 }));
    mockSuccessResponse.mockImplementation((body: unknown) => Response.json(body, { status: 200 }));
    mockGetTeam.mockResolvedValue({ ok: true, data: team });
  });

  describe("DELETE", () => {
    test("denies team deletion when the API key creator can no longer manage users", async () => {
      mockGetApiKeyCreatorRole.mockResolvedValue(null);
      mockCanManageOrganizationUsers.mockReturnValue(false);

      const response = await DELETE(buildRequest("DELETE"), {
        params: Promise.resolve({ organizationId, teamId }),
      });

      expect(mockGetApiKeyCreatorRole).toHaveBeenCalledWith(apiKeyId, organizationId);
      expect(mockCanManageOrganizationUsers).toHaveBeenCalledWith(null);
      expect(mockDeleteTeam).not.toHaveBeenCalled();
      expect(mockHandleApiError).toHaveBeenCalledWith(
        expect.anything(),
        expect.objectContaining({ type: "forbidden" }),
        undefined
      );
      expect(response.status).toBe(403);
    });

    test("deletes the team when the API key creator clears the user-management floor", async () => {
      mockGetApiKeyCreatorRole.mockResolvedValue("manager");
      mockCanManageOrganizationUsers.mockReturnValue(true);
      mockDeleteTeam.mockResolvedValue({ ok: true, data: team });

      const response = await DELETE(buildRequest("DELETE"), {
        params: Promise.resolve({ organizationId, teamId }),
      });

      expect(mockCanManageOrganizationUsers).toHaveBeenCalledWith("manager");
      expect(mockDeleteTeam).toHaveBeenCalledWith(organizationId, teamId);
      expect(response.status).toBe(200);
    });
  });

  describe("PUT", () => {
    test("denies team rename when the API key creator can no longer manage users", async () => {
      mockGetApiKeyCreatorRole.mockResolvedValue(null);
      mockCanManageOrganizationUsers.mockReturnValue(false);

      const response = await PUT(buildRequest("PUT"), {
        params: Promise.resolve({ organizationId, teamId }),
      });

      expect(mockGetApiKeyCreatorRole).toHaveBeenCalledWith(apiKeyId, organizationId);
      expect(mockCanManageOrganizationUsers).toHaveBeenCalledWith(null);
      expect(mockUpdateTeam).not.toHaveBeenCalled();
      expect(mockHandleApiError).toHaveBeenCalledWith(
        expect.anything(),
        expect.objectContaining({ type: "forbidden" }),
        undefined
      );
      expect(response.status).toBe(403);
    });

    test("renames the team when the API key creator clears the user-management floor", async () => {
      mockGetApiKeyCreatorRole.mockResolvedValue("manager");
      mockCanManageOrganizationUsers.mockReturnValue(true);
      mockUpdateTeam.mockResolvedValue({ ok: true, data: { ...team, name: "Renamed Team" } });

      const response = await PUT(buildRequest("PUT"), {
        params: Promise.resolve({ organizationId, teamId }),
      });

      expect(mockCanManageOrganizationUsers).toHaveBeenCalledWith("manager");
      expect(mockUpdateTeam).toHaveBeenCalledWith(organizationId, teamId, { name: "Renamed Team" });
      expect(response.status).toBe(200);
    });
  });

  describe("audit log", () => {
    let auditLog: TApiAuditLog;

    beforeEach(() => {
      auditLog = {
        action: "deleted",
        targetType: "team",
        userId: UNKNOWN_DATA,
        targetId: UNKNOWN_DATA,
        organizationId,
        status: "failure",
        userType: "api",
        apiUrl: `http://localhost/api/v2/organizations/org123/teams/${teamId}`,
      };
      mockGetApiKeyCreatorRole.mockResolvedValue("manager");
      mockCanManageOrganizationUsers.mockReturnValue(true);
      mockDeleteTeam.mockResolvedValue({ ok: true, data: team });
      mockAuthenticatedApiClient.mockImplementationOnce(
        async ({ handler }: Parameters<typeof authenticatedApiClient>[0]) =>
          await handler({
            request: buildRequest("DELETE"),
            auditLog,
            authentication: {
              type: "apiKey",
              apiKeyId,
              organizationId,
              workspacePermissions: [],
              organizationAccess: { accessControl: { read: true, write: true } },
            },
            parsedInput: { params: { organizationId, teamId } },
          })
      );
    });

    test("DELETE records the team as it was before deletion", async () => {
      await DELETE(buildRequest("DELETE"), { params: Promise.resolve({ organizationId, teamId }) });

      expect(auditLog.oldObject).toEqual(team);
    });

    test("DELETE records null, not a placeholder string, when the team could not be read first", async () => {
      mockGetTeam.mockResolvedValue({ ok: false, error: { type: "not_found", details: [] } });

      await DELETE(buildRequest("DELETE"), { params: Promise.resolve({ organizationId, teamId }) });

      expect(auditLog.oldObject).toBeNull();
    });
  });
});
