import { Result } from '@core/shared/domain/Result';
import { describe, expect, it, vi } from 'vitest';
import { DashboardService } from './DashboardService';

describe('DashboardService', () => {
  it('getDashboardOverview returns presenter model on success', async () => {
    const mockResult = { currentDriver: null, myUpcomingRaces: [], otherUpcomingRaces: [], upcomingRaces: [], activeLeaguesCount: 0, nextRace: null, recentResults: [], leagueStandingsSummaries: [], feedSummary: { notificationCount: 0, items: [] }, friends: [] };
    const presenter = { present: vi.fn(), getResponseModel: vi.fn(() => ({ feed: [] })) };
    const useCase = { execute: vi.fn(async () => Result.ok(mockResult)) };

    const service = new DashboardService(
      { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() } as never,
      useCase as never,
      presenter as never,
    );

    await expect(service.getDashboardOverview('d1')).resolves.toEqual({ feed: [] });
    expect(useCase.execute).toHaveBeenCalledWith({ driverId: 'd1' });
    expect(presenter.present).toHaveBeenCalledWith(mockResult);
  });

  it('getDashboardOverview throws with details message on error', async () => {
    const service = new DashboardService(
      { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'boom' } })) } as never,
      { present: vi.fn(), getResponseModel: vi.fn() } as never,
    );

    await expect(service.getDashboardOverview('d1')).rejects.toThrow('Failed to get dashboard overview: boom');
  });

  it('getDashboardOverview throws with fallback message when no details.message', async () => {
    const service = new DashboardService(
      { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() } as never,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as never)) } as never,
      { present: vi.fn(), getResponseModel: vi.fn() } as never,
    );

    await expect(service.getDashboardOverview('d1')).rejects.toThrow('Failed to get dashboard overview: Unknown error');
  });
});