import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { AuthenticatedUserDTO } from '../dtos/AuthDto';
import type { LoginResult } from '@core/identity/application/use-cases/LoginUseCase';
import type { SignupResult } from '@core/identity/application/use-cases/SignupUseCase';

type AuthSessionResult = LoginResult | SignupResult;

export class AuthSessionPresenter implements UseCaseOutputPort<AuthSessionResult> {
  private model: AuthenticatedUserDTO | null = null;

  reset(): void {
    this.model = null;
  }

  present(result: AuthSessionResult): void {
    this.model = {
      userId: result.user.getId().value,
      email: result.user.getEmail() ?? '',
      displayName: result.user.getDisplayName() ?? '',
    };
  }

  getResponseModel(): AuthenticatedUserDTO {
    if (!this.model) {
      throw new Error('Presenter not presented');
    }
    return this.model;
  }

  get responseModel(): AuthenticatedUserDTO {
    if (!this.model) {
      throw new Error('Presenter not presented');
    }
    return this.model;
  }
}
