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

import { CookieIdentitySessionAdapter } from '@adapters/identity/session/CookieIdentitySessionAdapter';
import type { IdentitySessionPort } from '@core/identity/application/ports/IdentitySessionPort';
import { ForgotPasswordUseCase } from '@core/identity/application/use-cases/ForgotPasswordUseCase';
import { LoginUseCase } from '@core/identity/application/use-cases/LoginUseCase';
import { LogoutUseCase } from '@core/identity/application/use-cases/LogoutUseCase';
import { ResetPasswordUseCase } from '@core/identity/application/use-cases/ResetPasswordUseCase';
import { SignupSponsorUseCase } from '@core/identity/application/use-cases/SignupSponsorUseCase';
import { SignupUseCase } from '@core/identity/application/use-cases/SignupUseCase';
import type { MagicLinkNotificationPort } from '@core/identity/domain/ports/MagicLinkNotificationPort';
import type { AuthRepository } from '@core/identity/domain/repositories/AuthRepository';
import type { CompanyRepository } from '@core/identity/domain/repositories/CompanyRepository';
import type { MagicLinkRepository } from '@core/identity/domain/repositories/MagicLinkRepository';
import type { PasswordHashingService } from '@core/identity/domain/services/PasswordHashingService';
import type { Logger } from '@core/shared/domain/Logger';

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

import { ConsoleMagicLinkNotificationAdapter } from '@adapters/notifications/ports/ConsoleMagicLinkNotificationAdapter';
import { AuthSessionPresenter } from './presenters/AuthSessionPresenter';
import { CommandResultPresenter } from './presenters/CommandResultPresenter';
import { ForgotPasswordPresenter } from './presenters/ForgotPasswordPresenter';
import { ResetPasswordPresenter } from './presenters/ResetPasswordPresenter';

// Define the tokens for dependency injection
export { AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, USER_REPOSITORY_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 SIGNUP_SPONSOR_USE_CASE_TOKEN = 'SignupSponsorUseCase';
export const LOGOUT_USE_CASE_TOKEN = 'LogoutUseCase';
export const FORGOT_PASSWORD_USE_CASE_TOKEN = 'ForgotPasswordUseCase';
export const RESET_PASSWORD_USE_CASE_TOKEN = 'ResetPasswordUseCase';

export const AUTH_SESSION_OUTPUT_PORT_TOKEN = 'AuthSessionOutputPort';
export const COMMAND_RESULT_OUTPUT_PORT_TOKEN = 'CommandResultOutputPort';
export const FORGOT_PASSWORD_OUTPUT_PORT_TOKEN = 'ForgotPasswordOutputPort';
export const RESET_PASSWORD_OUTPUT_PORT_TOKEN = 'ResetPasswordOutputPort';
export const SIGNUP_SPONSOR_OUTPUT_PORT_TOKEN = 'SignupSponsorOutputPort';
export const MAGIC_LINK_NOTIFICATION_PORT_TOKEN = 'MagicLinkNotificationPort';

export const AuthProviders: Provider[] = [
  {
    provide: IDENTITY_SESSION_PORT_TOKEN,
    useFactory: (logger: Logger) => new CookieIdentitySessionAdapter(logger),
    inject: [LOGGER_TOKEN],
  },
  AuthSessionPresenter,
  CommandResultPresenter,
  {
    provide: AUTH_SESSION_OUTPUT_PORT_TOKEN,
    useExisting: AuthSessionPresenter,
  },
  {
    provide: COMMAND_RESULT_OUTPUT_PORT_TOKEN,
    useExisting: CommandResultPresenter,
  },
  {
    provide: LOGIN_USE_CASE_TOKEN,
    useFactory: (
      authRepo: AuthRepository,
      passwordHashing: PasswordHashingService,
      logger: Logger,
    ) => new LoginUseCase(authRepo, passwordHashing, logger),
    inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: SIGNUP_USE_CASE_TOKEN,
    useFactory: (
      authRepo: AuthRepository,
      passwordHashing: PasswordHashingService,
      logger: Logger,
    ) => new SignupUseCase(authRepo, passwordHashing, logger),
    inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: SIGNUP_SPONSOR_USE_CASE_TOKEN,
    useFactory: (
      authRepo: AuthRepository,
      companyRepo: CompanyRepository,
      passwordHashing: PasswordHashingService,
      logger: Logger,
    ) => new SignupSponsorUseCase(authRepo, companyRepo, passwordHashing, logger),
    inject: [AUTH_REPOSITORY_TOKEN, COMPANY_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: LOGOUT_USE_CASE_TOKEN,
    useFactory: (sessionPort: IdentitySessionPort, logger: Logger) =>
      new LogoutUseCase(sessionPort, logger),
    inject: [IDENTITY_SESSION_PORT_TOKEN, LOGGER_TOKEN],
  },
  ForgotPasswordPresenter,
  ResetPasswordPresenter,
  {
    provide: FORGOT_PASSWORD_OUTPUT_PORT_TOKEN,
    useExisting: ForgotPasswordPresenter,
  },
  {
    provide: RESET_PASSWORD_OUTPUT_PORT_TOKEN,
    useExisting: ResetPasswordPresenter,
  },
  {
    provide: SIGNUP_SPONSOR_OUTPUT_PORT_TOKEN,
    useExisting: AuthSessionPresenter,
  },
  {
    provide: MAGIC_LINK_NOTIFICATION_PORT_TOKEN,
    useFactory: (logger: Logger) => new ConsoleMagicLinkNotificationAdapter(logger),
    inject: [LOGGER_TOKEN],
  },
  {
    provide: FORGOT_PASSWORD_USE_CASE_TOKEN,
    useFactory: (
      authRepo: AuthRepository,
      magicLinkRepo: MagicLinkRepository,
      notificationPort: MagicLinkNotificationPort,
      logger: Logger,
    ) => new ForgotPasswordUseCase(authRepo, magicLinkRepo, notificationPort, logger),
    inject: [AUTH_REPOSITORY_TOKEN, MAGIC_LINK_REPOSITORY_TOKEN, MAGIC_LINK_NOTIFICATION_PORT_TOKEN, LOGGER_TOKEN],
  },
  {
    provide: RESET_PASSWORD_USE_CASE_TOKEN,
    useFactory: (
      authRepo: AuthRepository,
      magicLinkRepo: MagicLinkRepository,
      passwordHashing: PasswordHashingService,
      logger: Logger,
    ) => new ResetPasswordUseCase(authRepo, magicLinkRepo, passwordHashing, logger),
    inject: [AUTH_REPOSITORY_TOKEN, MAGIC_LINK_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
  },
];