import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import '../../../../utils/toast_messages/toast_message_util.dart';
import 'package:flutter/material.dart';
import '../Child_Repo/Child_repo_screen.dart';

class ChildViewModelScreen extends ChangeNotifier{
  final ChildRepoScreen repo = ChildRepoScreen();

  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 =
          await repo.getAllWithPagination(page, size); // ✅ Use await

      print('with pagination res - $response');

      // ✅ Ensure response is a Map<String, dynamic>
      if (response is! Map<String, dynamic>) {
        throw Exception('Unexpected response format: $response');
      }

      // ✅ Extract 'content' and ensure it's a list
      final entities = (response['content'] as List)
          .cast<Map<String, dynamic>>() // ✅ Ensure list of maps
          .toList();
      return entities;
    } catch (e) {
      print(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");
      // Wait for API response
      final responseData =
          await repo.createEntity(entity) as Map<String, dynamic>;
      print('after value - $responseData');
      ToastMessageUtil.showToast(
          message: "Added Successfully", toastType: ToastType.success);

      return responseData; // Return the data AFTER it is received
    } catch (error) {
      print("error--$error");
      ToastMessageUtil.showToast(
          message: "Got Error", toastType: ToastType.error);

      throw Exception(
          'Failed to Create Entity: $error'); // Properly rethrow the error
    }
  } 
  Future<void> updateEntity(int entityId, Map<String, dynamic> entity) async {
    try {
      repo.updateEntity(entityId, entity).then((value) {
        ToastMessageUtil.showToast(
            message: "Updated Successfully", toastType: ToastType.success);
      }).onError(
        (error, stackTrace) {
          print("error--$error");
          ToastMessageUtil.showToast(
              message: "Got Error", toastType: ToastType.error);
        },
      );
      print(entity);
    } catch (e) {
      throw Exception('Failed to update entity: $e');
    }
  }

  Future<void> deleteEntity(int entityId) async {
    try {
      repo.deleteEntity(entityId).then((value) {
        ToastMessageUtil.showToast(
            message: "Deleted Successfully", toastType: ToastType.success);
      }).onError(
        (error, stackTrace) {
          print("error--$error");
          ToastMessageUtil.showToast(
              message: "Got Error", toastType: ToastType.error);
        },
      );
    } catch (e) {
      throw Exception('Failed to delete entity: $e');
    }
  }


}