//
//  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: String],
                                      headers: HTTPHeaders = [:],
                                      decodingType: T.Type) -> Future<Any, Error> {
    return Future { promise in
        AF.request("\(url)\(path)",
                   method: method,
                   parameters: parameters,
                   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))
            }
        }
    }
}
