import { UniqueID } from "@/core/common/domain";
import { TransactionManager } from "@/core/common/infrastructure/database";
import { Result } from "@repo/rdx-utils";
import { AccountService } from "../domain";
import { UpdateAccountUseCase } from "./update-account.use-case";

const mockAccountService: AccountService = {
  updateAccountById: jest.fn(),
} as unknown as AccountService;

const mockTransactionManager: TransactionManager = {
  complete(work: (transaction: any) => Promise<any>): void {
    jest.fn();
  },
} as unknown as TransactionManager;

const id = UniqueID.create("", true).data;

describe("UpdateAccountUseCase", () => {
  let updateAccountUseCase: UpdateAccountUseCase;

  beforeEach(() => {
    updateAccountUseCase = new UpdateAccountUseCase(mockAccountService, mockTransactionManager);
  });

  it("debería actualizar una cuenta y retornar un DTO", async () => {
    const mockUpdatedAccount = { id: "123", name: "Nuevo Nombre" };
    (mockAccountService.updateAccountById as jest.Mock).mockResolvedValue(
      Result.ok(mockUpdatedAccount)
    );

    const result = await updateAccountUseCase.execute(id, { name: "Nuevo Nombre" });
    expect(result.isSuccess).toBe(true);
    expect(result.data.name).toBe("Nuevo Nombre");
  });

  it("debería retornar error si la actualización falla", async () => {
    (mockAccountService.updateAccountById as jest.Mock).mockResolvedValue(
      Result.fail(new Error("Account not found"))
    );

    const result = await updateAccountUseCase.execute(id, { name: "Nuevo Nombre" });
    expect(result.isFailure).toBe(true);
    expect(result.error.message).toBe("Account not found");
  });
});
