import 'package:injectable/injectable.dart';
import 'package:krow/core/data/models/staff/staff.dart';
import 'package:krow/core/data/models/staff_role.dart';
import 'package:krow/features/profile/profile_main/data/profile_api_provider.dart';
import 'package:krow/features/profile/profile_main/data/profile_local_provider.dart';
import 'package:krow/features/profile/profile_main/domain/profile_repository.dart';

@Injectable(as: ProfileRepository)
class ProfileRepositoryImpl extends ProfileRepository{
  final ProfileApiProvider _apiProvider;
  final ProfileLocalProvider _cacheProvider;

  ProfileRepositoryImpl(this._apiProvider, this._cacheProvider);

  @override
  Future<List<StaffRole>> getUserProfileRoles() async{
    var roles = await  _apiProvider.fetchUserProfileRoles();
    await cacheProfileRoles(roles);
    return roles;
  }

  @override
  Future<void> cacheProfileRoles(List<StaffRole> roles) async{
    await _cacheProvider.saveRoles(roles);
  }

  @override
  Future<List<StaffRole>?> getCachedProfileRoles() {
    return _cacheProvider.loadRoles();
  }

  @override
  Stream<Staff> getUserProfile() {
    return _apiProvider.getMeWithCache();
  }
}