import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/profession.dart';
import 'package:prosapp_web_app/services/api_service.dart';

class ProfessionsProvider extends ChangeNotifier {
  List<Profession> professions = [];
  bool isLoading = true;
  final _api = ApiService.instance;

  ProfessionsProvider() {
    getProfessions();
  }

  getProfessions() async {
    try {
      final data = await _api.get('/professions');
      if (data is List) {
        professions = data.map((e) => Profession(name: e['name'] as String)).toList();
      } else if (data is Map && data['professions'] is List) {
        professions = (data['professions'] as List)
            .map((e) => Profession(name: e as String))
            .toList();
      }
    } catch (e) {
      print('Error obteniendo profesiones: $e');
    } finally {
      isLoading = false;
      notifyListeners();
    }
  }
}
