// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';

class MockAuthenticationRepository extends Mock
    implements AuthenticationRepository<int> {}

class MockAccount extends Mock implements Account {}

class TestAuthenticationCubit extends AuthenticationCubit<int> {
  TestAuthenticationCubit({required super.authenticationRepository});

  @override
  FutureOrResult<void> onDelete() async => const Ok(null);

  @override
  FutureOrResult<int?> onReauthenticate(
    Result<Account, AppException> result,
  ) async =>
      const Ok(null);

  @override
  FutureOrResult<int?> onRefresh(Result<Account, AppException> result) async =>
      const Ok(null);

  @override
  FutureOrResult<int?> onSignInFromCache(
    AuthenticationSession<int> session,
  ) async =>
      const Ok(null);

  @override
  FutureOrResult<void> onSignOut() async => const Ok(null);
}

void main() {
  group('AuthenticationCubit<T>', () {
    final MockAccount account = MockAccount();
    final AuthenticationSession<int> session = AuthenticationSession<int>(
      latestEvent: const UnknownAuthenticationEvent(),
      account: account,
      data: 10,
    );
    late AuthenticationRepository<int> authenticationRepository;

    setUp(() {
      authenticationRepository = MockAuthenticationRepository();
      when(() => authenticationRepository.sessionStream()).thenAnswer(
        (_) => const Stream.empty(),
      );
    });

    test('initial auth state is `unknown`', () {
      expect(
        TestAuthenticationCubit(
          authenticationRepository: authenticationRepository,
        ).state,
        const AuthenticationState<Never>.unknown(),
      );
    });

    group('ListenForAuthenticationChanges', () {
      blocTest<AuthenticationCubit<int>, AuthenticationState<int>>(
        'emits authenticated when stream contains session',
        setUp: () {
          when(() => authenticationRepository.sessionStream()).thenAnswer(
            (_) => Stream.fromIterable([session]),
          );
        },
        build: () => TestAuthenticationCubit(
          authenticationRepository: authenticationRepository,
        ),
        seed: () => const AuthenticationState.unknown(),
        expect: () => [AuthenticationState<int>.authenticated(session)],
      );

      blocTest<AuthenticationCubit<int>, AuthenticationState<int>>(
        'emits unauthenticated when account stream is empty',
        setUp: () {
          when(() => authenticationRepository.sessionStream()).thenAnswer(
            (_) => Stream.fromIterable(
              [const AuthenticationSession(latestEvent: SignedOutEvent())],
            ),
          );
        },
        build: () => TestAuthenticationCubit(
          authenticationRepository: authenticationRepository,
        ),
        seed: () => const AuthenticationState.unknown(),
        expect: () => [const AuthenticationState<int>.unauthenticated()],
      );
    });

    group('SignOut', () {
      blocTest<AuthenticationCubit<int>, AuthenticationState<int>>(
        'invokes signOut',
        setUp: () {
          when(
            () => authenticationRepository.signOut(),
          ).thenAnswer((_) async => const Ok(null));
        },
        build: () => TestAuthenticationCubit(
          authenticationRepository: authenticationRepository,
        ),
        act: (cubit) => cubit.signOut(),
        verify: (_) {
          verify(() => authenticationRepository.signOut()).called(1);
        },
      );
    });
  });
}
