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

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

// Tokens (standalone to avoid circular imports)
import {
    DASHBOARD_OVERVIEW_USE_CASE_TOKEN,
    LOGGER_TOKEN,
} from './DashboardTokens';

@Injectable()
export class DashboardService {
  constructor(
    @Inject(LOGGER_TOKEN) private readonly logger: Logger,
    @Inject(DASHBOARD_OVERVIEW_USE_CASE_TOKEN) private readonly dashboardOverviewUseCase: DashboardOverviewUseCase,
    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 NotFoundException(`Failed to get dashboard overview: ${message}`);
    }

    // Present the result
    this.presenter.present(result.unwrap());
    return this.presenter.getResponseModel();
  }
}