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

import Foundation
public protocol APIResponseProtocol: Decodable {
    var status: Status { get }
}

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

public 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 ----------------
class Header: Codable {
    let header: String
}

// /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/app/retryAccess ----------------
class Access: Codable {
    let access: String
}

// /api/v1/in/user ----------------
class User: Codable {
    let uid, name, type, login_date: String
    let device_id, push_token, birth: String?
    let auto_login_yn: Bool
}

// /api/v1/in/user/login ----------------
// /api/v1/in/user/register ----------------
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?
}

