//
//  APIController.swift
//  AcaMate
//
//  Created by Sean Kim on 11/26/24.
//


import Foundation
import Combine

import Alamofire

public func loadAPIData<T: Decodable>(url: String, path: String,
                                      method: HTTPMethod = .get,
                                      parameters: [String: Any],
                                      headers: HTTPHeaders = [:],//["Accept": "application/json"],
                                      decodingType: T.Type) -> Future<Any, Error> {
    let encoding: ParameterEncoding = (method == .get) ? URLEncoding.default : JSONEncoding.default
    return Future { promise in
        printLog(parameters)
        AF.request("\(url)\(path)",
                   method: method,
                   parameters: parameters,
                   encoding: encoding,
                   headers: headers
        )
        .validate(statusCode: 200 ..< 300)
        .responseString { response in
            printLog(response)
        }
        .responseDecodable(of: decodingType) { response in
            switch response.result {
            case .success(let value):
                promise(.success(value))
            case .failure(let error):
                promise(.failure(error))
            }
        }
    }
}


