import { config } from "@/config";
import { RepositoryManager } from "@/contexts/common/domain";
import { createSequelizeAdapter } from "@/contexts/common/infrastructure/sequelize";

import { registerDealerRepository } from "@/contexts/sales/infrastructure/Dealer.repository";
import {
  initializeAdmin,
  initializeSampleDealer,
  initializeSampleUser,
} from "@/contexts/users/application/userServices";
import { registerUserRepository } from "@/contexts/users/infrastructure/User.repository";

export const insertUsers = async () => {
  const context = {
    defaults: config.defaults,
    adapter: createSequelizeAdapter(),
    repositoryManager: RepositoryManager.getInstance(),
  } as any;

  registerUserRepository(context);
  registerDealerRepository(context);

  initializeAdmin(context.adapter, context.repositoryManager.getRepository("User"));
  const user = await initializeSampleUser(
    context.adapter,
    context.repositoryManager.getRepository("User")
  );

  if (user) {
    initializeSampleDealer(
      user,
      context.adapter,
      context.repositoryManager.getRepository("Dealer")
    );
  }
};
