import { describe, it, expect, vi, type Mock } from 'vitest';
import { GetCurrentUserSessionUseCase } from './GetCurrentUserSessionUseCase';
import type { AuthSession, IdentitySessionPort } from '../ports/IdentitySessionPort';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';

describe('GetCurrentUserSessionUseCase', () => {
  let sessionPort: {
    getCurrentSession: Mock;
    createSession: Mock;
    clearSession: Mock;
  };
  let logger: Logger;
  let output: UseCaseOutputPort<AuthSession | null> & { present: Mock };
  let useCase: GetCurrentUserSessionUseCase;

  beforeEach(() => {
    sessionPort = {
      getCurrentSession: vi.fn(),
      createSession: vi.fn(),
      clearSession: vi.fn(),
    };

    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    } as unknown as Logger;

    output = {
      present: vi.fn(),
    };

    useCase = new GetCurrentUserSessionUseCase(
      sessionPort as unknown as IdentitySessionPort,
      logger,
      output,
    );
  });

  it('returns the current auth session when one exists', async () => {
    const session: AuthSession = {
      user: {
        id: 'user-1',
        email: 'test@example.com',
        displayName: 'Test User',
        primaryDriverId: 'driver-1',
      },
      issuedAt: Date.now(),
      expiresAt: Date.now() + 1000,
      token: 'token-123',
    };

    sessionPort.getCurrentSession.mockResolvedValue(session);

    const result = await useCase.execute();

    expect(sessionPort.getCurrentSession).toHaveBeenCalledTimes(1);
    expect(result.isOk()).toBe(true);
    expect(output.present).toHaveBeenCalledWith(session);
  });

  it('returns null when there is no active session', async () => {
    sessionPort.getCurrentSession.mockResolvedValue(null);

    const result = await useCase.execute();

    expect(sessionPort.getCurrentSession).toHaveBeenCalledTimes(1);
    expect(result.isOk()).toBe(true);
    expect(output.present).toHaveBeenCalledWith(null);
  });
});