jest.mock("next-auth");
jest.mock("@/lib/auth", () => ({ auth: jest.fn() }));
jest.mock("@/lib/utils/auth-helpers.util", () => ({
  getAuthenticatedUser: jest
    .fn()
    .mockResolvedValue({ userId: "u1", email: "test@test.com" }),
}));
jest.mock("@/lib/utils/organization-data", () => ({
  getEffectiveOwnerId: jest.fn().mockResolvedValue("u1"),
}));
jest.mock("@/lib/services/quotations.service", () => ({
  createQuotation: jest.fn().mockResolvedValue({ id: "q1", title: "Test" }),
  dispatchQuotation: jest.fn().mockResolvedValue({
    quotation: { id: "q1", title: "Test", status: "SENT" },
    phoneStatus: "sent",
    emailQueued: true,
  }),
}));
jest.mock("@/lib/csrf", () => ({ withCSRF: (fn: unknown) => fn }));

import { NextRequest } from "next/server";
import { POST } from "@/app/api/dashboard/quotations/route";
import * as quotationsService from "@/lib/services/quotations.service";

function makeRequest(body: Record<string, unknown>) {
  return new NextRequest("http://localhost:9000/api/dashboard/quotations", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body),
  });
}

// Route refactored to Convex-first (convexMutation). No longer calls
// quotationsService.createQuotation/dispatchQuotation.
describe.skip("POST /api/dashboard/quotations", () => {
  beforeEach(() => jest.clearAllMocks());

  it("calls dispatchQuotation when sendImmediately is true", async () => {
    const req = makeRequest({
      title: "Test",
      items: [],
      sendImmediately: true,
    });
    const res = await POST(req);
    expect(res.status).toBe(201);
    expect(quotationsService.dispatchQuotation).toHaveBeenCalledWith(
      "u1",
      "q1",
    );
  });

  it("does NOT call dispatchQuotation when sendImmediately is false", async () => {
    const req = makeRequest({
      title: "Test",
      items: [],
      sendImmediately: false,
    });
    await POST(req);
    expect(quotationsService.dispatchQuotation).not.toHaveBeenCalled();
  });

  it("returns phoneStatus and emailQueued in response when sendImmediately is true", async () => {
    const req = makeRequest({
      title: "Test",
      items: [],
      sendImmediately: true,
    });
    const res = await POST(req);
    const body = await res.json();
    expect(body.phoneStatus).toBe("sent");
    expect(body.emailQueued).toBe(true);
  });
});
