import { injectable, unmanaged } from 'inversify';
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
import type { LoginParamsDTO } from '@/lib/types/generated/LoginParamsDTO';
import type { SignupParamsDTO } from '@/lib/types/generated/SignupParamsDTO';
import type { ForgotPasswordDTO } from '@/lib/types/generated/ForgotPasswordDTO';
import type { ResetPasswordDTO } from '@/lib/types/generated/ResetPasswordDTO';
import { isProductionEnvironment } from '@/lib/config/env';
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';

/**
 * Auth Service
 *
 * Orchestrates authentication operations.
 * Returns raw API DTOs. No ViewModels or UX logic.
 */
@injectable()
export class AuthService implements Service {
  private apiClient: AuthApiClient;

  constructor(@unmanaged() apiClient?: AuthApiClient) {
    if (apiClient) {
      this.apiClient = apiClient;
    } else {
      const baseUrl = getWebsiteApiBaseUrl();
      const logger = new ConsoleLogger();
      const errorReporter = new EnhancedErrorReporter(logger, {
        showUserNotifications: false,
        logToConsole: true,
        reportToExternal: isProductionEnvironment(),
      });
      this.apiClient = new AuthApiClient(baseUrl, errorReporter, logger);
    }
  }

  async login(params: LoginParamsDTO): Promise<Result<SessionViewModel, DomainError>> {
    try {
      const dto = await this.apiClient.login(params);
      return Result.ok(new SessionViewModel(dto.user));
    } catch (error: unknown) {
      return Result.err({ type: 'unauthorized', message: (error as Error).message || 'Login failed' });
    }
  }

  async signup(params: SignupParamsDTO): Promise<Result<SessionViewModel, DomainError>> {
    try {
      const dto = await this.apiClient.signup(params);
      return Result.ok(new SessionViewModel(dto.user));
    } catch (error: unknown) {
      return Result.err({ type: 'validation', message: (error as Error).message || 'Signup failed' });
    }
  }

  async logout(): Promise<Result<void, DomainError>> {
    try {
      await this.apiClient.logout();
      return Result.ok(undefined);
    } catch (error: unknown) {
      return Result.err({ type: 'serverError', message: (error as Error).message || 'Logout failed' });
    }
  }

  async forgotPassword(params: ForgotPasswordDTO): Promise<Result<{ message: string; magicLink?: string }, DomainError>> {
    try {
      const result = await this.apiClient.forgotPassword(params);
      return Result.ok(result);
    } catch (error: unknown) {
      return Result.err({ type: 'serverError', message: (error as Error).message || 'Forgot password request failed' });
    }
  }

  async resetPassword(params: ResetPasswordDTO): Promise<Result<{ message: string }, DomainError>> {
    try {
      const result = await this.apiClient.resetPassword(params);
      return Result.ok(result);
    } catch (error: unknown) {
      return Result.err({ type: 'serverError', message: (error as Error).message || 'Reset password failed' });
    }
  }

  async getSession(): Promise<Result<any, DomainError>> {
    try {
      const dto = await this.apiClient.getSession();
      return Result.ok(dto);
    } catch (error: unknown) {
      return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get session' });
    }
  }
}