import 'dart:convert';
import 'package:http/http.dart';
import 'package:http/http.dart' as http;
import 'package:logger/logger.dart';
import '../../../Helper/Logger.dart';
import '../../../Model/Response/Appcategory/Appcategoryresponse.dart';
import '../../../Model/Response/Appcategory/app_category_response.dart';


class AppCategoryProvider{

  Future<GetAppCategories?> getAppCategory(String url) async {
    try {
      final response = await http.get(
        Uri.parse(url),
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
        },
      );

      logger.i('Category API URL: $url');
      logger.i('Status Code: ${response.statusCode}');
      logger.i('Response Body: ${response.body}');

      if (response.statusCode == 200) {
        final Map<String, dynamic> json = jsonDecode(response.body);
        return GetAppCategories.fromJson(json);
      } else {
        logger.e('Failed to load categories: ${response.statusCode}');
        return null;
      }
    } catch (e) {
      logger.e('Error in getAppCategory: $e');
      return null;
    }
  }

}



class SubCategoryProvider {
  final logger = Logger();

  Future<List<SubCategory>> fetchSubCategories() async {
    const url = 'https://fiesta.nearle.app/live/api/v1/mob/utils/getsubcategories';
    try {
      final response = await http.get(Uri.parse(url));

      if (response.statusCode == 200) {
        final data = jsonDecode(response.body);
        if (data['status'] == true && data['details'] != null) {
          final List list = data['details'];
          return list.map((e) => SubCategory.fromJson(e)).toList();
        } else {
          logger.w('No subcategories found or invalid response');
          return [];
        }
      } else {
        logger.e('Failed with status: ${response.statusCode}');
        return [];
      }
    } catch (e) {
      logger.e('Error fetching subcategories: $e');
      return [];
    }
  }
}
