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 OfficeRepoScreen {

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

  Future<ApiResponse<List<Map<String, dynamic>>>> getEntities() async {
    try {
      final response =
          await _helper.getGetApiResponse('$baseUrl$_endpointPath');
      return ApiResponse.success(response as List<Map<String, dynamic>>);
    } catch (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');
    }
  }






}
