// autogenerated

// swiftlint:disable all
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

extension V1.FinanceReports {
    public struct GET: Endpoint {
        public typealias Response = Data

        public var path: String {
            "/v1/financeReports"
        }

        public var parameters: Parameters = Parameters()

        public init() {

        }

        public func request(with baseURL: URL) throws -> URLRequest? {
            var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: true)
            components?.path = path

            components?.queryItems = [
                URLQueryItem(name: "filter[regionCode]",
                             value: parameters.filter[.regionCode]?.map { "\($0)" }.joined(separator: ",")),
                URLQueryItem(name: "filter[reportDate]",
                             value: parameters.filter[.reportDate]?.map { "\($0)" }.joined(separator: ",")),
                URLQueryItem(name: "filter[reportType]",
                             value: parameters.filter[.reportType]?.map { "\($0)" }.joined(separator: ",")),
                URLQueryItem(name: "filter[vendorNumber]",
                             value: parameters.filter[.vendorNumber]?.map { "\($0)" }.joined(separator: ","))
            ].filter { $0.value != nil }
            if components?.queryItems?.isEmpty ?? false {
                components?.queryItems = nil
            }

            var urlRequest = components?.url.map { URLRequest(url: $0) }
            urlRequest?.httpMethod = "GET"
            return urlRequest
        }

        /// - Returns: **200**, List of FinanceReports as `Data`
        /// - Throws: **400**, Parameter error(s) as `ErrorResponse`
        /// - Throws: **403**, Forbidden error as `ErrorResponse`
        public static func response(from data: Data, urlResponse: HTTPURLResponse) throws -> Response {
            var jsonDecoder: JSONDecoder {
                let decoder = JSONDecoder()
                return decoder
            }

            switch urlResponse.statusCode {
            case 200:
                return data

            case 400:
                throw try jsonDecoder.decode(ErrorResponse.self, from: data)

            case 403:
                throw try jsonDecoder.decode(ErrorResponse.self, from: data)

            default:
                throw try jsonDecoder.decode(ErrorResponse.self, from: data)
            }
        }
    }
}

extension V1.FinanceReports.GET {
    public struct Parameters: Hashable {
        public var filter: Filter = Filter()

        public struct Filter: Hashable {
            public subscript <T: Hashable>(_ relation: Relation<T>) -> T {
                get { values[relation]?.base as! T }
                set { values[relation] = AnyHashable(newValue) }
            }

            private var values: [AnyHashable: AnyHashable] = [:]

            public enum ReportType: Hashable, Codable, RawRepresentable {
                case financeDetail
                case financial
                case unknown(String)

                public var rawValue: String {
                    switch self {
                    case .financeDetail: return "FINANCE_DETAIL"
                    case .financial: return "FINANCIAL"
                    case .unknown(let rawValue): return rawValue
                    }
                }

                public init(rawValue: String) {
                    switch rawValue {
                    case "FINANCE_DETAIL": self = .financeDetail
                    case "FINANCIAL": self = .financial
                    default: self = .unknown(rawValue)
                    }
                }
            }

            public struct Relation<T>: Hashable {
                /// filter by attribute 'regionCode' **(required)**
                public static var regionCode: Relation<[String]?> {
                    .init(key: "filter[regionCode]")
                }

                /// filter by attribute 'reportDate' **(required)**
                public static var reportDate: Relation<[String]?> {
                    .init(key: "filter[reportDate]")
                }

                /// filter by attribute 'reportType' **(required)**
                public static var reportType: Relation<[ReportType]?> {
                    .init(key: "filter[reportType]")
                }

                /// filter by attribute 'vendorNumber' **(required)**
                public static var vendorNumber: Relation<[String]?> {
                    .init(key: "filter[vendorNumber]")
                }

                internal let key: String

                public func hash(into hasher: inout Hasher) {
                    hasher.combine(key)
                }
            }
        }
    }
}

// swiftlint:enable all
