jest.mock("../../src/services/apiClient", () => ({
  __esModule: true,
  default: { post: jest.fn() },
}));

import apiClient from "../../src/services/apiClient";
import { changePassword, updateTelephone } from "../../src/services/profileApi";

const post = apiClient.post as jest.Mock;

describe("profileApi", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("updateTelephone : POST le telephone et resout sur success", async () => {
    post.mockResolvedValue({ data: { success: true, data: { telephone: "0612" } } });

    await updateTelephone("0612");

    expect(post).toHaveBeenCalledWith("/mobile/profile_update.php", { telephone: "0612" });
  });

  it("updateTelephone : leve le message metier sur success=false", async () => {
    post.mockResolvedValue({ data: { success: false, message: "format invalide" } });

    await expect(updateTelephone("xx")).rejects.toThrow("format invalide");
  });

  it("changePassword : POST current + new et resout sur success", async () => {
    post.mockResolvedValue({ data: { success: true, data: { updated: true } } });

    await changePassword("old", "Newpass1!");

    expect(post).toHaveBeenCalledWith("/mobile/password_change.php", {
      current_password: "old",
      new_password: "Newpass1!",
    });
  });

  it("changePassword : leve le message metier sur success=false", async () => {
    post.mockResolvedValue({ data: { success: false, message: "mot de passe actuel incorrect" } });

    await expect(changePassword("bad", "Newpass1!")).rejects.toThrow(
      "mot de passe actuel incorrect"
    );
  });
});
