import 'package:cheminova/utils/api_urls.dart';
import 'package:dio/dio.dart';

import '../models/annauncement_model.dart';
import '../utils/common_api_service.dart';

class AnnouncementService {
  final Dio _dio = Dio();


  Future<List<AnnouncementModel>?> fetchAnnouncements(String token) async {
    try {
      String url = ApiUrls.AnnaouncementUrl; // Base URL to fetch product manuals

      final response = await commonApiService<List<AnnouncementModel>>(
        method: "GET",
        url: url,
        additionalHeaders: {  // Pass the token here
          'Authorization': 'Bearer $token',
        },
        fromJson: (json) {
          if (json['announcements'] != null) {
            // If the productManuals key is present, map the response to a list of ProductManualModel objects
            final List<AnnouncementModel> productManuals = (json['announcements'] as List)
                .map((manualJson) => AnnouncementModel.fromJson(manualJson))
                .toList();
            return productManuals; // Return the list of product manuals
          } else {
            return [];
          }
        },
      );
      return response;
    } catch (e) {

      print("fkfgghgh ,${e.toString()}");
      //print(e.toString());
      return null;
    }
  }


// Future<List<AnnouncementModel>> fetchAnnouncements(String token) async {
  //   final String url = ApiUrls.AnnaouncementUrl;
  //   try {
  //     _dio.options.headers['Authorization'] = 'Bearer $token';
  //     final Response response = await _dio.get(url);
  //
  //     if (response.statusCode == 200) {
  //
  //   List<dynamic> data = response.data;
  //       return data.map((announcement) => AnnouncementModel.fromJson(announcement)).toList();
  //     } else {
  //       throw Exception('Failed to load announcements');
  //     }
  //   } catch (e) {
  //     throw Exception('Error occurred while fetching announcements: $e');
  //   }
  // }
}
