import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:lgcc/models/item_model.dart';

class WordpressService {

  String getBaseUrlType(String type) {
    return 'https://${type}wp.carpa.com/v5/items/?cache=false';
  }

  Future<List<ItemModel>> fetchItems(String type) async {
    final baseUrl = getBaseUrlType(type);
    final response = await http.get(Uri.parse('$baseUrl&f=list'));
    print('Fetching items from: $response');
    if (response.statusCode == 200) {
      final List<dynamic> jsonData = json.decode(response.body);
      return jsonData.map((item) => ItemModel.fromJson(item)).toList();
    } else {
      throw Exception('Failed to load items');
    }
  }

  Future<List<ResultModel>> fetchYearsParam(String type ) async {
    final baseUrl = getBaseUrlType(type);
    final response = await http.get(Uri.parse(baseUrl + '&years'));
    print('Fetching years from: $baseUrl');
    if (response.statusCode == 200) {
      final List<dynamic> jsonData = json.decode(response.body);
      return jsonData.map((item) => ResultModel.fromJson(item)).toList();
    } else {
      throw Exception('Failed to load years');
    }
    
  }
}
