import 'dart:convert';

import 'package:http/http.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';

import '../../controllers/api_routes/api_manager.dart';
import '../../controllers/api_routes/urls.dart';
import '../../models/lookup.dart';

class IncidentStatusLookupProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      // OPTIMIZATION: Enable caching for incident status lookup
      Response response = await ApiManager.instance.get(
        URLs.incidentStatusLookup,
        useCache: true,
        enableToastMessage: false,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class IncidentTypeLookupProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      // OPTIMIZATION: Enable caching for incident type lookup
      Response response = await ApiManager.instance.get(
        URLs.incidentTypeLookup,
        useCache: true,
        enableToastMessage: false,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class IncidentRootCauseLookupProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      // OPTIMIZATION: Enable caching for incident root cause lookup
      Response response = await ApiManager.instance.get(
        URLs.incidentRootCauseLookup,
        useCache: true,
        enableToastMessage: false,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class IncidentPersonInvolvedLookupProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      // OPTIMIZATION: Enable caching for incident person involved lookup
      Response response = await ApiManager.instance.get(
        URLs.incidentPersonInvolvedLookup,
        useCache: true,
        enableToastMessage: false,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class ClinicalNonClinicalLookupProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      // OPTIMIZATION: Enable caching for clinical/non-clinical lookup
      Response response = await ApiManager.instance.get(
        URLs.clinicalNonClinicalLookup,
        useCache: true,
        enableToastMessage: false,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}

class GenderLookupProvider extends LoadingListNotifier<Lookup> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      // OPTIMIZATION: Enable caching for gender lookup
      Response response = await ApiManager.instance.get(
        URLs.genderLookup,
        useCache: true,
        enableToastMessage: false,
      );
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => Lookup.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
