import '../Testf_Repo/Testf_repo_screen.dart';

class TestfViewModelScreen {
  final TestfRepoScreen repo = TestfRepoScreen();

  Future<List<Map<String, dynamic>>> getEntities() async {
    try {
      final response = await repo.getEntities();
      final entities = (response as List).cast<Map<String, dynamic>>();
      return entities;
    } catch (e) {
      throw Exception('Failed to get all entities: $e');
    }
  }

  Future<List<Map<String, dynamic>>> getAllWithPagination(
      int page, int size) async {
    try {
      final response = repo.getAllWithPagination(page, size);

      Map<String, dynamic> responseData = response as Map<String, dynamic>;

      final entities =
          (responseData['content'] as List).cast<Map<String, dynamic>>();
      return entities;
    } catch (e) {
      throw Exception('Failed to get all without pagination: $e');
    }
  }

  Future<Map<String, dynamic>> createEntity(Map<String, dynamic> entity) async {
    try {
      print("in post api$entity");
      final response = repo.createEntity(entity);

      print(entity);

      // Assuming the response is a Map<String, dynamic>
      Map<String, dynamic> responseData = response as Map<String, dynamic>;

      return responseData;
    } catch (e) {
      throw Exception('Failed to create entity: $e');
    }
  }

  Future<void> updateEntity(int entityId, Map<String, dynamic> entity) async {
    try {
      repo.updateEntity(entityId, entity);
      print(entity);
    } catch (e) {
      throw Exception('Failed to update entity: $e');
    }
  }

  Future<void> deleteEntity(int entityId) async {
    try {
      repo.deleteEntity(entityId);
    } catch (e) {
      throw Exception('Failed to delete entity: $e');
    }
  }
}
