import { DashboardOverviewUseCase } from '@core/racing/application/use-cases/DashboardOverviewUseCase';
import { Inject, Injectable } from '@nestjs/common';
import { DashboardOverviewDTO } from './dtos/DashboardOverviewDTO';
import { DashboardOverviewPresenter } from './presenters/DashboardOverviewPresenter';

// Core imports
import type { Logger } from '@core/shared/application/Logger';

// Tokens
import { DASHBOARD_OVERVIEW_USE_CASE_TOKEN, LOGGER_TOKEN, DASHBOARD_OVERVIEW_OUTPUT_PORT_TOKEN } from './DashboardProviders';

@Injectable()
export class DashboardService {
  constructor(
    @Inject(LOGGER_TOKEN) private readonly logger: Logger,
    @Inject(DASHBOARD_OVERVIEW_USE_CASE_TOKEN) private readonly dashboardOverviewUseCase: DashboardOverviewUseCase,
    @Inject(DASHBOARD_OVERVIEW_OUTPUT_PORT_TOKEN) private readonly presenter: DashboardOverviewPresenter,
  ) {}

  async getDashboardOverview(driverId: string): Promise<DashboardOverviewDTO> {
    this.logger.debug('[DashboardService] Getting dashboard overview:', { driverId });

    const result = await this.dashboardOverviewUseCase.execute({ driverId });

    if (result.isErr()) {
      const error = result.error;
      const message = error?.details?.message || 'Unknown error';
      throw new Error(`Failed to get dashboard overview: ${message}`);
    }

    return this.presenter.getResponseModel();
  }
}