import { UniqueID } from "@/core/common/domain";
import { Account } from "../aggregates";
import { IAccountRepository } from "../repositories";
import { AccountService } from "./account.service";

const mockAccountRepository: IAccountRepository = {
  accountExists: jest.fn(),
  findAll: jest.fn(),
  findByEmail: jest.fn(),
  findById: jest.fn(),
  create: jest.fn(),
  update: jest.fn(),
};

const sampleAccount = {
  id: "c5743279-e1cf-4dd5-baae-6698c8c6183c",
  is_companyr: false,
  name: "Empresa XYZ",
  trade_name: "XYZ Trading",
  tin: "123456789",
  street: "Calle Principal 123",
  city: "Madrid",
  state: "Madrid",
  postal_code: "28001",
  country: "España",
  email: "contacto@xyz.com",
  phone: "+34 600 123 456",
  fax: "+34 600 654 321",
  website: "https://xyz.com",
  legal_record: "Registro Mercantil XYZ",
  default_tax: 21,
  status: "active",
  language_code: "es",
  currency_code: "EUR",
  logo: "https://xyz.com/logo.png",
};

const accountId = UniqueID.create(sampleAccount.id).data;

describe("AccountService", () => {
  let accountService: AccountService;

  beforeEach(() => {
    accountService = new AccountService(mockAccountRepository);
  });

  it("debería actualizar una cuenta existente", async () => {
    const existingAccount = new Account(
      {
        /* datos simulados */
      },
      "123"
    );
    (mockAccountRepository.findById as jest.Mock).mockResolvedValue(existingAccount);
    (mockAccountRepository.create as jest.Mock).mockResolvedValue(undefined);

    const result = await accountService.updateAccountById(accountId, { name: "Nuevo Nombre" });
    expect(result.isSuccess).toBe(true);
    expect(result.data.name).toBe("Nuevo Nombre");
    expect(mockAccountRepository.save).toHaveBeenCalled();
  });

  it("debería retornar error si la cuenta no existe", async () => {
    (mockAccountRepository.findById as jest.Mock).mockResolvedValue(null);

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