import { Provider } from '@nestjs/common';

import { CookieIdentitySessionAdapter } from '@adapters/identity/session/CookieIdentitySessionAdapter';
import { LoginUseCase } from '@core/identity/application/use-cases/LoginUseCase';
import { LogoutUseCase } from '@core/identity/application/use-cases/LogoutUseCase';
import { SignupUseCase } from '@core/identity/application/use-cases/SignupUseCase';
import type { IdentitySessionPort } from '@core/identity/application/ports/IdentitySessionPort';
import type { IAuthRepository } from '@core/identity/domain/repositories/IAuthRepository';
import type { IPasswordHashingService } from '@core/identity/domain/services/PasswordHashingService';
import type { LoginResult } from '@core/identity/application/use-cases/LoginUseCase';
import type { LogoutResult } from '@core/identity/application/use-cases/LogoutUseCase';
import type { SignupResult } from '@core/identity/application/use-cases/SignupUseCase';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';

import {
  AUTH_REPOSITORY_TOKEN,
  PASSWORD_HASHING_SERVICE_TOKEN,
  USER_REPOSITORY_TOKEN,
} from '../../persistence/identity/IdentityPersistenceTokens';

import { AuthSessionPresenter } from './presenters/AuthSessionPresenter';
import { CommandResultPresenter } from './presenters/CommandResultPresenter';

// Define the tokens for dependency injection
export { AUTH_REPOSITORY_TOKEN, USER_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN };
export const LOGGER_TOKEN = 'Logger';
export const IDENTITY_SESSION_PORT_TOKEN = 'IdentitySessionPort';
export const LOGIN_USE_CASE_TOKEN = 'LoginUseCase';
export const SIGNUP_USE_CASE_TOKEN = 'SignupUseCase';
export const LOGOUT_USE_CASE_TOKEN = 'LogoutUseCase';

export const AUTH_SESSION_OUTPUT_PORT_TOKEN = 'AuthSessionOutputPort';
export const COMMAND_RESULT_OUTPUT_PORT_TOKEN = 'CommandResultOutputPort';

export const AuthProviders: Provider[] = [
  {
    provide: IDENTITY_SESSION_PORT_TOKEN,
    useFactory: (logger: Logger) => new CookieIdentitySessionAdapter(logger),
    inject: [LOGGER_TOKEN],
  },
  {
    provide: AuthSessionPresenter,
    useClass: AuthSessionPresenter,
  },
  {
    provide: CommandResultPresenter,
    useClass: CommandResultPresenter,
  },
  {
    provide: AUTH_SESSION_OUTPUT_PORT_TOKEN,
    useExisting: AuthSessionPresenter,
  },
  {
    provide: COMMAND_RESULT_OUTPUT_PORT_TOKEN,
    useExisting: CommandResultPresenter,
  },
  {
    provide: LOGIN_USE_CASE_TOKEN,
    useFactory: (
      authRepo: IAuthRepository,
      passwordHashing: IPasswordHashingService,
      logger: Logger,
      output: UseCaseOutputPort<LoginResult>,
    ) => new LoginUseCase(authRepo, passwordHashing, logger, output),
    inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN, AUTH_SESSION_OUTPUT_PORT_TOKEN],
  },
  {
    provide: SIGNUP_USE_CASE_TOKEN,
    useFactory: (
      authRepo: IAuthRepository,
      passwordHashing: IPasswordHashingService,
      logger: Logger,
      output: UseCaseOutputPort<SignupResult>,
    ) => new SignupUseCase(authRepo, passwordHashing, logger, output),
    inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN, AUTH_SESSION_OUTPUT_PORT_TOKEN],
  },
  {
    provide: LOGOUT_USE_CASE_TOKEN,
    useFactory: (sessionPort: IdentitySessionPort, logger: Logger, output: UseCaseOutputPort<LogoutResult>) =>
      new LogoutUseCase(sessionPort, logger, output),
    inject: [IDENTITY_SESSION_PORT_TOKEN, LOGGER_TOKEN, COMMAND_RESULT_OUTPUT_PORT_TOKEN],
  },
];
