import { describe, it, expect, vi, type Mock } from 'vitest';
import {
  StartAuthUseCase,
  type StartAuthInput,
  type StartAuthResult,
  type StartAuthErrorCode,
} from './StartAuthUseCase';
import type { IdentityProviderPort } from '../ports/IdentityProviderPort';
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Result } from '@core/shared/application/Result';

describe('StartAuthUseCase', () => {
  let provider: {
    startAuth: Mock;
  };
  let logger: Logger & { error: Mock };
  let output: UseCaseOutputPort<StartAuthResult> & { present: Mock };
  let useCase: StartAuthUseCase;

  beforeEach(() => {
    provider = {
      startAuth: vi.fn(),
    };

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

    output = {
      present: vi.fn(),
    } as unknown as UseCaseOutputPort<StartAuthResult> & { present: Mock };

    useCase = new StartAuthUseCase(
      provider as unknown as IdentityProviderPort,
      logger,
      output,
    );
  });

  it('returns ok and presents redirect when provider call succeeds', async () => {
    const input: StartAuthInput = {
      provider: 'IRACING_DEMO',
      returnTo: 'https://app/callback',
    };

    const expected = { redirectUrl: 'https://auth/redirect', state: 'state-123' };

    provider.startAuth.mockResolvedValue(expected);

    const result: Result<void, ApplicationErrorCode<StartAuthErrorCode, { message: string }>> =
      await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();

    expect(provider.startAuth).toHaveBeenCalledWith({
      provider: input.provider,
      returnTo: input.returnTo,
    });

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = output.present.mock.calls[0]![0] as StartAuthResult;
    expect(presented).toEqual(expected);
  });

  it('wraps unexpected errors as REPOSITORY_ERROR and logs them', async () => {
    const input: StartAuthInput = {
      provider: 'IRACING_DEMO',
      returnTo: 'https://app/callback',
    };

    provider.startAuth.mockRejectedValue(new Error('Provider failure'));

    const result: Result<void, ApplicationErrorCode<StartAuthErrorCode, { message: string }>> =
      await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr();
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details.message).toBe('Provider failure');

    expect(output.present).not.toHaveBeenCalled();
    expect(logger.error).toHaveBeenCalled();
  });
});
