//
//  AV_usermodel.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/4/11.
//

import Foundation
import UIKit
import WCDBSwift

// 类型绑定
let userModelTableName = "UserModel"
class UserModel: TableCodable {
    /// 主键自增id
    var identifier: Int?
    
    var uid: String = ""
    var name: String = ""
//    var introduce: String = ""
    var date: String = ""
    var imageBase64: String? // 用于存储图像的 Base64 字符串
    var notificationEnabled: Bool = false
    var type:String = ""
    
    
    enum CodingKeys: String, CodingTableKey {
        typealias Root = UserModel
        
        static let objectRelationalMapping = TableBinding(CodingKeys.self)
        
        case identifier
        case uid
        case name
//        case introduce
        case date
        case imageBase64 = "image" // 将图像的 Base64 字符串存储在 imageBase64 属性中
        case notificationEnabled
        case type
        
        // Column constraints for primary key, unique, not null, default value and so on. It is optional.
        static var columnConstraintBindings: [UserModel.CodingKeys: ColumnConstraint]? {
            return [
                .identifier:ColumnConstraint()
            ]
        }
    }
    
    // 将 UIImage 转换为 Base64 字符串，并存储在 imageBase64 属性中
    func setImage(image: UIImage) {
        if let imageData = image.pngData() {
            self.imageBase64 = imageData.base64EncodedString()
        }
    }
    
    // 从 imageBase64 属性中读取 Base64 字符串，并转换为 UIImage
    func getImage() -> UIImage? {
        if let base64String = self.imageBase64,
           let imageData = Data(base64Encoded: base64String),
           let image = UIImage(data: imageData) {
            return image
        }
        return nil
    }
    
  
}
