import 'package:dio/dio.dart';
import '../../../../data/network/base_network_service.dart';
import '../../../../data/network/network_api_service.dart';
import '../../../../resources/api_constants.dart';
import 'package:base_project/data/response/api_response.dart';

class Adv3RepoScreen {

  final String baseUrl = ApiConstants.baseUrl;
  final BaseNetworkService _helper = NetworkApiService();
  final String _endpointPath = '/Adv3/Adv3';

  Future<ApiResponse<List<Map<String, dynamic>>>> getEntities() async {
    try {
      final response =
          await _helper.getGetApiResponse('$baseUrl$_endpointPath');
      print('Response received: $response');
      List<Map<String, dynamic>> entities = const [];
      if (response is List) {
        entities = response
            .whereType<Map>()
            .map((e) => Map<String, dynamic>.from(e as Map))
            .toList();
      } else if (response is Map<String, dynamic>) {
        final dynamic content = response['content'];
        if (content is List) {
          entities = content
              .whereType<Map>()
              .map((e) => Map<String, dynamic>.from(e as Map))
              .toList();
        }
      }
      return ApiResponse.success(entities);
    } catch (e) {
      print(' error got $e');
      return ApiResponse.error('Failed to get all entities: $e');
    }
  } 

  Future<ApiResponse<List<Map<String, dynamic>>>> getAllWithPagination(
      int page, int size) async {
    try {
      final response = await _helper.getGetApiResponse(
          '$baseUrl$_endpointPath/getall/page?page=$page&size=$size');

      if (response is Map<String, dynamic> && response['content'] is List) {
        final List<Map<String, dynamic>> entities =
            (response['content'] as List).cast<Map<String, dynamic>>().toList();
        return ApiResponse.success(entities);
      } else {
        return ApiResponse.error('Invalid response format');
      }
    } catch (e) {
      return ApiResponse.error('Failed to get all without pagination: $e');
    }
  }

  Future<ApiResponse<Map<String, dynamic>>> createEntity(
      Map<String, dynamic> entity) async {
    try {
      print("in post api$entity");
      final response =
          await _helper.getPostApiResponse('$baseUrl$_endpointPath', entity);
      return ApiResponse.success(response as Map<String, dynamic>);
    } catch (e) {
      return ApiResponse.error('Failed to create entity: $e');
    }
  }

  Future<ApiResponse<Map<String, dynamic>>> updateEntity(
      int entityId, Map<String, dynamic> entity) async {
    try {
      final response = await _helper.getPutApiResponse(
          '$baseUrl$_endpointPath/$entityId', entity);
      return ApiResponse.success(response as Map<String, dynamic>);
    } catch (e) {
      return ApiResponse.error('Failed to update entity: $e');
    }
  }

  Future<ApiResponse<void>> deleteEntity(int entityId) async {
    try {
      await _helper.getDeleteApiResponse('$baseUrl$_endpointPath/$entityId');
      return ApiResponse.success(null);
    } catch (e) {
      return ApiResponse.error('Failed to delete entity: $e');
    }
  }
 Future<dynamic> getdatagrid_fieldGrid() async {
    try {
      String apiUrl = "$baseUrl/Country_ListFilter1/Country_ListFilter1";
      final response = await _helper.getGetApiResponse(apiUrl);
      return response;
    } catch (e) {
      throw Exception('Failed to Upload datagrid Field: $e');
    }
  }

 Future<dynamic> getdatagrid_field2Grid() async {
    try {
      String apiUrl = "$baseUrl/Country_ListFilter1/Country_ListFilter1";
      final response = await _helper.getGetApiResponse(apiUrl);
      return response;
    } catch (e) {
      throw Exception('Failed to Upload datagrid Field2: $e');
    }
  }









 Future<dynamic> getdy2() async {
    try {
      String apiUrl = "$baseUrl/Country_ListFilter1/Country_ListFilter1";
      final response = await _helper.getGetApiResponse(apiUrl);
      return response;
    } catch (e) {
      throw Exception('Failed to Get: $e');
    }
  }

 Future<dynamic> getdy1() async {
    try {
      String apiUrl = "$baseUrl/Country_ListFilter1/Country_ListFilter1";
      final response = await _helper.getGetApiResponse(apiUrl);
      return response;
    } catch (e) {
      throw Exception('Failed to Get: $e');
    }
  }

 Future<dynamic> getdymlti1() async {
    try {
      String apiUrl = "$baseUrl/Country_ListFilter1/Country_ListFilter1";
      final response = await _helper.getGetApiResponse(apiUrl);
      return response;
    } catch (e) {
      throw Exception('Failed to Get: $e');
    }
  }

 Future<dynamic> getdymlt2() async {
    try {
      String apiUrl = "$baseUrl/Country_ListFilter1/Country_ListFilter1";
      final response = await _helper.getGetApiResponse(apiUrl);
      return response;
    } catch (e) {
      throw Exception('Failed to Get: $e');
    }
  }

}
