import {
  IUseCase,
  IUseCaseError,
  UseCaseError,
} from "@/contexts/common/application";
import { IRepositoryManager } from "@/contexts/common/domain";
import { IInfrastructureError } from "@/contexts/common/infrastructure";
import { ISequelizeAdapter } from "@/contexts/common/infrastructure/sequelize";
import { ILogin_DTO, Result, ensureEmailIsValid } from "@shared/contexts";
import { AuthUser } from "../domain";
import { findUserByEmail } from "./authServices";

export type LoginResponseOrError =
  | Result<never, IUseCaseError>
  | Result<AuthUser, never>;

export class LoginUseCase
  implements IUseCase<ILogin_DTO, Promise<LoginResponseOrError>>
{
  private _adapter: ISequelizeAdapter;
  private _repositoryManager: IRepositoryManager;

  constructor(props: {
    adapter: ISequelizeAdapter;
    repositoryManager: IRepositoryManager;
  }) {
    this._adapter = props.adapter;
    this._repositoryManager = props.repositoryManager;
  }

  private getRepositoryByName<T>(name: string) {
    return this._repositoryManager.getRepository<T>(name);
  }

  async execute(request: ILogin_DTO): Promise<LoginResponseOrError> {
    const { email, password } = request;

    // Validaciones de datos

    const emailOrError = ensureEmailIsValid(email);
    if (emailOrError.isFailure) {
      return Result.fail(
        UseCaseError.create(
          UseCaseError.INVALID_INPUT_DATA,
          "Email or password is not valid",
          emailOrError.error,
        ),
      );
    }

    // Crear auth
    try {
      const user = await findUserByEmail(
        emailOrError.object,
        this._adapter,
        this.getRepositoryByName("Auth"),
      );
      if (user === null || !user.verifyPassword(password)) {
        return Result.fail(
          UseCaseError.create(
            UseCaseError.INVALID_INPUT_DATA,
            "Email or password is not valid",
          ),
        );
      }
      return Result.ok<AuthUser>(user);
    } catch (error: unknown) {
      const _error = error as IInfrastructureError;
      return Result.fail(
        UseCaseError.create(
          UseCaseError.REPOSITORY_ERROR,
          "Error al buscar el usuario",
          _error,
        ),
      );
    }
  }
}
