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

describe('GetCurrentUserSessionUseCase', () => {
  let sessionPort: {
    getCurrentSession: Mock;
    createSession: Mock;
    clearSession: Mock;
  };
  let logger: Logger;
  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;

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

  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(result.unwrap()).toBe(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(result.unwrap()).toBe(null);
  });
});