import { describe, expect, it, vi } from 'vitest';
import { Result } from '@core/shared/application/Result';
import { AnalyticsService } from './AnalyticsService';
import { RecordPageViewPresenter } from './presenters/RecordPageViewPresenter';
import { RecordEngagementPresenter } from './presenters/RecordEngagementPresenter';
import { GetDashboardDataPresenter } from './presenters/GetDashboardDataPresenter';
import { GetAnalyticsMetricsPresenter } from './presenters/GetAnalyticsMetricsPresenter';

describe('AnalyticsService', () => {
  it('recordPageView returns presenter response on success', async () => {
    const recordPageViewPresenter = new RecordPageViewPresenter();

    const recordPageViewUseCase = {
      execute: vi.fn(async () => {
        recordPageViewPresenter.present({ pageViewId: 'pv-1' });
        return Result.ok(undefined);
      }),
    };

    const service = new AnalyticsService(
      recordPageViewUseCase as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      recordPageViewPresenter,
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    const dto = await service.recordPageView({
      entityType: 'league' as any,
      entityId: 'l1',
      visitorType: 'anonymous' as any,
      sessionId: 's1',
    });

    expect(dto).toEqual({ pageViewId: 'pv-1' });
    expect(recordPageViewUseCase.execute).toHaveBeenCalled();
  });

  it('recordPageView throws on use case error', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'nope' } })) } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(
      service.recordPageView({ entityType: 'league' as any, entityId: 'l1', visitorType: 'anonymous' as any, sessionId: 's1' }),
    ).rejects.toThrow('nope');
  });

  it('recordPageView throws with fallback message when no details.message', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as any)) } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(
      service.recordPageView({ entityType: 'league' as any, entityId: 'l1', visitorType: 'anonymous' as any, sessionId: 's1' }),
    ).rejects.toThrow('Failed to record page view');
  });

  it('recordEngagement returns response on success', async () => {
    const recordEngagementPresenter = new RecordEngagementPresenter();
    const recordEngagementUseCase = {
      execute: vi.fn(async () => {
        recordEngagementPresenter.present({ eventId: 'e1', engagementWeight: 7 });
        return Result.ok(undefined);
      }),
    };

    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      recordEngagementUseCase as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      recordEngagementPresenter,
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    const dto = await service.recordEngagement({
      action: 'click' as any,
      entityType: 'league' as any,
      entityId: 'l1',
      actorType: 'anonymous',
      sessionId: 's1',
    });

    expect(dto).toEqual({ eventId: 'e1', engagementWeight: 7 });
  });

  it('recordEngagement throws with details message on error', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'nope' } })) } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(
      service.recordEngagement({
        action: 'click' as any,
        entityType: 'league' as any,
        entityId: 'l1',
        actorType: 'anonymous',
        sessionId: 's1',
      }),
    ).rejects.toThrow('nope');
  });

  it('recordEngagement throws with fallback message when no details.message', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as any)) } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(
      service.recordEngagement({
        action: 'click' as any,
        entityType: 'league' as any,
        entityId: 'l1',
        actorType: 'anonymous',
        sessionId: 's1',
      }),
    ).rejects.toThrow('Failed to record engagement');
  });

  it('getDashboardData returns response on success', async () => {
    const getDashboardDataPresenter = new GetDashboardDataPresenter();
    const getDashboardDataUseCase = {
      execute: vi.fn(async () => {
        getDashboardDataPresenter.present({
          totalUsers: 1,
          activeUsers: 2,
          totalRaces: 3,
          totalLeagues: 4,
        });
        return Result.ok(undefined);
      }),
    };

    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      getDashboardDataUseCase as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      getDashboardDataPresenter,
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(service.getDashboardData()).resolves.toEqual({
      totalUsers: 1,
      activeUsers: 2,
      totalRaces: 3,
      totalLeagues: 4,
    });
  });

  it('getDashboardData throws with details message on error', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'boom' } })) } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(service.getDashboardData()).rejects.toThrow('boom');
  });

  it('getDashboardData throws with fallback message when no details.message', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as any)) } as any,
      { execute: vi.fn() } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(service.getDashboardData()).rejects.toThrow('Failed to get dashboard data');
  });

  it('getAnalyticsMetrics returns response on success', async () => {
    const getAnalyticsMetricsPresenter = new GetAnalyticsMetricsPresenter();
    const getAnalyticsMetricsUseCase = {
      execute: vi.fn(async () => {
        getAnalyticsMetricsPresenter.present({
          pageViews: 10,
          uniqueVisitors: 0,
          averageSessionDuration: 0,
          bounceRate: 0,
        });
        return Result.ok(undefined);
      }),
    };

    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      getAnalyticsMetricsUseCase as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      getAnalyticsMetricsPresenter,
    );

    await expect(service.getAnalyticsMetrics()).resolves.toEqual({
      pageViews: 10,
      uniqueVisitors: 0,
      averageSessionDuration: 0,
      bounceRate: 0,
    });
  });

  it('getAnalyticsMetrics throws with details message on error', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR', details: { message: 'boom' } })) } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(service.getAnalyticsMetrics()).rejects.toThrow('boom');
  });

  it('getAnalyticsMetrics throws with fallback message when no details.message', async () => {
    const service = new AnalyticsService(
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn() } as any,
      { execute: vi.fn(async () => Result.err({ code: 'REPOSITORY_ERROR' } as any)) } as any,
      new RecordPageViewPresenter(),
      new RecordEngagementPresenter(),
      new GetDashboardDataPresenter(),
      new GetAnalyticsMetricsPresenter(),
    );

    await expect(service.getAnalyticsMetrics()).rejects.toThrow('Failed to get analytics metrics');
  });
});
