import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:userfront/features/profile/data/models/user_profile_model.dart';
import 'package:userfront/features/profile/data/repositories/profile_repository.dart';
import 'package:userfront/features/profile/domain/notifiers/profile_notifier.dart';

class _FakeProfileRepository extends ProfileRepository {
  _FakeProfileRepository({
    required this.initialProfile,
    required this.persistUpdate,
  }) : _profile = initialProfile;

  final UserProfile initialProfile;
  final bool persistUpdate;
  UserProfile _profile;

  int updateCount = 0;
  String? lastRequestedDepartment;

  @override
  Future<UserProfile> getMyProfile() async {
    return _profile;
  }

  @override
  Future<void> updateMyProfile({
    required String name,
    required String phone,
    required String department,
  }) async {
    updateCount += 1;
    lastRequestedDepartment = department;
    if (!persistUpdate) {
      return;
    }
    _profile = _profile.copyWith(
      name: name,
      phone: phone,
      department: department,
    );
  }
}

UserProfile _seedProfile({required String department}) {
  return UserProfile(
    id: 'user-1',
    email: 'qa@example.com',
    name: 'QA User',
    phone: '01012345678',
    department: department,
    affiliationType: 'employee',
    companyCode: 'BARON',
  );
}

void main() {
  test('재현: 저장소가 소속 변경을 반영하지 않으면 loadProfile 이후 이전 값으로 보인다', () async {
    final repository = _FakeProfileRepository(
      initialProfile: _seedProfile(department: 'Old Dept'),
      persistUpdate: false,
    );
    final container = ProviderContainer(
      overrides: [profileRepositoryProvider.overrideWithValue(repository)],
    );
    addTearDown(container.dispose);

    final initial = await container.read(profileProvider.future);
    expect(initial?.department, 'Old Dept');

    await container
        .read(profileProvider.notifier)
        .updateProfile(
          name: 'QA User',
          phone: '01012345678',
          department: 'New Dept',
        );

    expect(repository.updateCount, 1);
    expect(repository.lastRequestedDepartment, 'New Dept');

    await container.read(profileProvider.notifier).loadProfile();
    expect(container.read(profileProvider).value?.department, 'Old Dept');
  });

  test('소속 변경이 저장소에 반영되면 loadProfile 이후에도 변경값이 유지된다', () async {
    final repository = _FakeProfileRepository(
      initialProfile: _seedProfile(department: 'Old Dept'),
      persistUpdate: true,
    );
    final container = ProviderContainer(
      overrides: [profileRepositoryProvider.overrideWithValue(repository)],
    );
    addTearDown(container.dispose);

    final initial = await container.read(profileProvider.future);
    expect(initial?.department, 'Old Dept');

    await container
        .read(profileProvider.notifier)
        .updateProfile(
          name: 'QA User',
          phone: '01012345678',
          department: 'New Dept',
        );

    expect(repository.updateCount, 1);
    expect(repository.lastRequestedDepartment, 'New Dept');

    await container.read(profileProvider.notifier).loadProfile();
    expect(container.read(profileProvider).value?.department, 'New Dept');
  });
}
