//
//  API Response.swift
//  AcaMate
//
//  Created by Sean Kim on 11/29/24.
//

import Foundation

class APIResponse<T: Codable>: Codable {
    let status: Status
    let data: T?
}

class Status: Codable {
    let code: APICode
    let message: String
}

enum APICode: Codable, RawRepresentable {
    case success(String)
    case inputErr(String)
    case outputErr(String)
    case networkErr(String)
    case unknownErr(String)
    case anything(String)
    
    var rawValue: String {
        switch self {
        case .success(let value),
                .inputErr(let value),
                .outputErr(let value),
                .networkErr(let value),
                .unknownErr(let value),
                .anything(let value):
            return value
        }
    }
    
    init?(rawValue: String){
        if rawValue.hasPrefix("0") {self = .success(rawValue)}
        else if rawValue.hasPrefix("1") {self = .inputErr(rawValue)}
        else if rawValue.hasPrefix("2") {self = .outputErr(rawValue)}
        else if rawValue.hasPrefix("3") {self = .networkErr(rawValue)}
        else if rawValue == "999" {self = .unknownErr(rawValue)}
        else { self = .anything(rawValue)}
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let rawValue = try container.decode(String.self)
        self = APICode(rawValue: rawValue) ?? .anything(rawValue)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(self.rawValue)
    }
}

// /api/v1/in/app/version ----------------

class VersionData: Codable {
    let os_type, final_ver, dev_ver, force_ver: String
    let choice_update_yn: Bool
    
}

// /api/v1/in/user/login ----------------

class User_Token: Codable {
    let token: String?
    let refresh: String?
    //    let bids: [String]
}

// /api/v1/in/member/academy ----------------
class AcademyName: Codable {
    let bid: String?
    let name: String?
}

