import 'package:get/get.dart';
import '../models/announcement_response.dart';
import '../services/api_service.dart';
import '../utils/api_urls.dart';

class AnnouncementController extends GetxController {
  final ApiService _apiClient = ApiService();
  RxList<AnnouncementResponse> announcementsList = <AnnouncementResponse>[].obs;
  RxBool isLoading = false.obs;

  @override
  void onInit() {
    super.onInit();
    getAnnouncements();
  }

  Future<void> getAnnouncements() async {
    isLoading.value = true;
    try {
      final response = await _apiClient.get(ApiUrls.announcementUrl);
      isLoading.value = false;
      if (response.statusCode == 200) {
        final List<dynamic> data = response.data;
        announcementsList.value = data.map((item) => AnnouncementResponse.fromJson(item)).toList();
      }
    } catch (e) {
      isLoading.value = false;
      Get.snackbar('Error', 'Failed to fetch announcements');
    }
  }
}