// Generated by SwiftModelGenerator
// Do not modify this file manually

import Foundation
import LeStorage

class BasePurchase: SyncedModelObject, SyncedStorable {

    static func resourceName() -> String { return "purchases" }
    static func tokenExemptedMethods() -> [HTTPMethod] { return [] }

    var id: UInt64 = 0
    var user: String = ""
    var purchaseDate: Date = Date()
    var productId: String = ""
    var quantity: Int? = nil
    var revocationDate: Date? = nil
    var expirationDate: Date? = nil

    init(
        id: UInt64 = 0,
        user: String = "",
        purchaseDate: Date = Date(),
        productId: String = "",
        quantity: Int? = nil,
        revocationDate: Date? = nil,
        expirationDate: Date? = nil
    ) {
        super.init()
        self.id = id
        self.user = user
        self.purchaseDate = purchaseDate
        self.productId = productId
        self.quantity = quantity
        self.revocationDate = revocationDate
        self.expirationDate = expirationDate
    }
    required public override init() {
        super.init()
    }

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case user = "user"
        case purchaseDate = "purchaseDate"
        case productId = "productId"
        case quantity = "quantity"
        case revocationDate = "revocationDate"
        case expirationDate = "expirationDate"
    }


    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decodeIfPresent(UInt64.self, forKey: .id) ?? 0
        self.user = try container.decodeEncrypted(key: .user)
        self.purchaseDate = try container.decodeIfPresent(Date.self, forKey: .purchaseDate) ?? Date()
        self.productId = try container.decodeIfPresent(String.self, forKey: .productId) ?? ""
        self.quantity = try container.decodeIfPresent(Int.self, forKey: .quantity) ?? nil
        self.revocationDate = try container.decodeIfPresent(Date.self, forKey: .revocationDate) ?? nil
        self.expirationDate = try container.decodeIfPresent(Date.self, forKey: .expirationDate) ?? nil
        try super.init(from: decoder)
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.id, forKey: .id)
        try container.encodeAndEncryptIfPresent(self.user.data(using: .utf8), forKey: .user)
        try container.encode(self.purchaseDate, forKey: .purchaseDate)
        try container.encode(self.productId, forKey: .productId)
        try container.encode(self.quantity, forKey: .quantity)
        try container.encode(self.revocationDate, forKey: .revocationDate)
        try container.encode(self.expirationDate, forKey: .expirationDate)
        try super.encode(to: encoder)
    }

    func userValue() -> CustomUser? {
        return Store.main.findById(user)
    }

    func copy(from other: any Storable) {
        guard let purchase = other as? BasePurchase else { return }
        self.id = purchase.id
        self.user = purchase.user
        self.purchaseDate = purchase.purchaseDate
        self.productId = purchase.productId
        self.quantity = purchase.quantity
        self.revocationDate = purchase.revocationDate
        self.expirationDate = purchase.expirationDate
    }

    static func relationships() -> [Relationship] {
        return [
            Relationship(type: CustomUser.self, keyPath: \BasePurchase.user),
        ]
    }

}