import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { expect, it, vi } from "vitest";

import { ProfilePasswordDialog } from "@/components/dashboard/profile/profile-password-dialog";

const { changeProfilePassword } = vi.hoisted(() => ({ changeProfilePassword: vi.fn() }));
vi.mock("next-intl", () => ({ useTranslations: () => (key: string) => key }));
vi.mock("@/components/dashboard/profile/profile-api", () => ({ changeProfilePassword }));
vi.mock("@/components/dashboard/profile/profile-success-dialog", () => ({ ProfileSuccessDialog: ({ open, title }: { open: boolean; title: string }) => open ? <div>{title}</div> : null }));

it("submits the exact password payload before showing success", async () => {
  changeProfilePassword.mockResolvedValue({});
  render(<ProfilePasswordDialog open onOpenChange={vi.fn()} />);
  fireEvent.change(screen.getByLabelText("current"), { target: { value: "Password@123" } });
  fireEvent.change(screen.getByLabelText("new"), { target: { value: "NewPass@123" } });
  fireEvent.change(screen.getByLabelText("confirmNew"), { target: { value: "NewPass@123" } });
  fireEvent.click(screen.getByRole("button", { name: "save" }));
  await waitFor(() => expect(changeProfilePassword).toHaveBeenCalledWith({ current_password: "Password@123", password: "NewPass@123", password_confirmation: "NewPass@123" }));
  expect(screen.getByText("successTitle")).toBeInTheDocument();
});
