import Foundation
import GenAPI

/*
    This represents a video upload request to AWS S3,
    or any similar service that we might add in the future.

    The ID is used to reference the specific upload request
    when notifying our backend that the upload is complete,
    polling for moderation, and eventually attaching the
    video to the clip.
 */
public struct UploadRequest: Equatable, Identifiable {
    public let id: String
    public let url: String
    public let fields: [String: String]

    init(_ remote: Components.Schemas.UploadRequestSchema) throws {
        self.id = remote.id
        self.url = remote.url
        if let fields = remote.fields.value as? [String: String] {
            self.fields = fields
        } else {
            throw DecodingError.valueNotFound(String.self, .init(codingPath: [], debugDescription: "fields not found"))
        }
    }

    public init(id: String, url: String, additionalFields: [String: AnyJSON]) {
        self.id = id
        self.url = url

        var tempFields = [String: String]()

        for (key, anyJSON) in additionalFields {
            switch anyJSON {
            case .string(let stringValue):
                tempFields[key] = stringValue
            case .number, .bool, .array, .object, .null:
                continue
            }
        }

        self.fields = tempFields
    }
}
